import pdb
import time
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
import torch.utils.model_zoo as model_zoo
import torch.optim as optim
from torch.autograd import Variable
import numpy as np
from src.codebase.utils.utils import log
from src.codebase_pytorch.utils.hooks import print_outputs, print_grads
from src.codebase_pytorch.utils.scheduler import ReduceLROnPlateau
from src.codebase_pytorch.utils.scheduler import LambdaLR
from src.codebase_pytorch.models.model import Model

__all__ = ['SqueezeNet', 'squeezenet1_0', 'squeezenet1_1']

model_urls = {
    'squeezenet1_0': 'https://download.pytorch.org/models/squeezenet1_0-a815701f.pth',
    'squeezenet1_1': 'https://download.pytorch.org/models/squeezenet1_1-f364aa15.pth',
}

model_paths = {
    'squeezenet1_0': '/n/regal/rush_lab/awang/models/squeezenet_v1_0.ckpt',
    'squeezenet1_1': '/n/regal/rush_lab/awang/models/squeezenet_v1_1.ckpt'
}

class Fire(nn.Module):

    def __init__(self, inplanes, squeeze_planes,
                 expand1x1_planes, expand3x3_planes):
        super(Fire, self).__init__()
        self.inplanes = inplanes
        self.squeeze = nn.Conv2d(inplanes, squeeze_planes, kernel_size=1)
        self.squeeze_activation = nn.ReLU(inplace=True)
        self.expand1x1 = nn.Conv2d(squeeze_planes, expand1x1_planes,
                                   kernel_size=1)
        self.expand1x1_activation = nn.ReLU(inplace=True)
        self.expand3x3 = nn.Conv2d(squeeze_planes, expand3x3_planes,
                                   kernel_size=3, padding=1)
        self.expand3x3_activation = nn.ReLU(inplace=True)

    def forward(self, x):
        x = self.squeeze_activation(self.squeeze(x))
        return torch.cat([
            self.expand1x1_activation(self.expand1x1(x)),
            self.expand3x3_activation(self.expand3x3(x))
        ], 1)

class SqueezeNet(Model):

    def __init__(self, version=1.0, num_classes=1000, use_cuda=0):
        super(SqueezeNet, self).__init__()
        if version not in [1.0, 1.1]:
            raise ValueError("Unsupported SqueezeNet version {version}:"
                             "1.0 or 1.1 expected".format(version=version))
        self.num_classes = num_classes
        self.use_cuda = use_cuda

        if version == 1.0:
            self.features = nn.Sequential(
                nn.Conv2d(3, 96, kernel_size=7, stride=2),
                nn.ReLU(inplace=True),
                nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                Fire(96, 16, 64, 64),
                Fire(128, 16, 64, 64),
                Fire(128, 32, 128, 128),
                nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                Fire(256, 32, 128, 128),
                Fire(256, 48, 192, 192),
                Fire(384, 48, 192, 192),
                Fire(384, 64, 256, 256),
                nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                Fire(512, 64, 256, 256),
            )
        else:
            self.features = nn.Sequential(
                nn.Conv2d(3, 64, kernel_size=3, stride=2),
                nn.ReLU(inplace=True),
                nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                Fire(64, 16, 64, 64),
                Fire(128, 16, 64, 64),
                nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                Fire(128, 32, 128, 128),
                Fire(256, 32, 128, 128),
                nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                Fire(256, 48, 192, 192),
                Fire(384, 48, 192, 192),
                Fire(384, 64, 256, 256),
                Fire(512, 64, 256, 256),
            )
        # Final convolution is initialized differently form the rest
        final_conv = nn.Conv2d(512, self.num_classes, kernel_size=1)
        self.classifier = nn.Sequential(
            nn.Dropout(p=0.5),
            final_conv,
            nn.ReLU(inplace=True),
            nn.AvgPool2d(13)
        )

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                if m is final_conv:
                    init.normal(m.weight.data, mean=0.0, std=0.01)
                else:
                    init.kaiming_uniform(m.weight.data)
                if m.bias is not None:
                    m.bias.data.zero_()

    def forward(self, x):
        x = self.features(x)
        x = self.classifier(x)
        return x.view(x.size(0), self.num_classes)

    def train_model(self, args, tr_data, val_data, fh):
        '''
        Train the model according to the parameters specified in args.
        '''
        self.train()
        lr = args.lr
        if args.optimizer == 'sgd':
            optimizer = optim.SGD(self.parameters(), lr=lr, momentum=args.momentum, weight_decay=args.weight_decay)
            log(fh, "\tOptimizing with SGD with learning rate %.3f" % lr)
        elif args.optimizer == 'adam':
            optimizer = optim.Adam(self.parameters(), lr=lr)
            log(fh, "\tOptimizing with adam with learning rate %.3f" % lr)
        elif args.optimizer == 'adagrad':
            optimizer = optim.Adagrad(self.parameters(), lr=lr,
                    weight_decay=args.weight_decay)
            log(fh, "\tOptimizing with adagrad with learning rate %.3f" % lr)
        else:
            raise NotImplementedError
        scheduler = ReduceLROnPlateau(optimizer,
                'max', factor=.5, patience=3, threshold=1e-1)
        #lr_lambda = lambda epoch: (1 - float(epoch) / args.n_epochs)
        #scheduler = LambdaLR(optimizer, lr_lambda)

        if args.load_model_from:
            self.load_state_dict(torch.load(args.load_model_from))

        start_time = time.time()
        val_loss, val_acc = self.evaluate(val_data)
        best_loss, best_acc = val_loss, val_acc
        last_acc = val_acc
        log(fh, "\tInitial val loss: %.3f, \tval acc: %.2f \t(%.3f s)" %
                (val_loss, val_acc, time.time() - start_time))
        if args.save_model_to:
            torch.save(self.state_dict(), args.save_model_to)
            log(fh, "\t\tSaved model to %s" % args.save_model_to)

        for epoch in xrange(args.n_epochs):
            log(fh, "\tEpoch %d, \tlearning rate: %.3f" % (epoch+1, scheduler.get_lr()[0]))
            total_loss, total_correct = 0., 0.
            start_time = time.time()
            for batch_idx in xrange(tr_data.n_batches):
                ins, targs = tr_data[batch_idx]
                if self.use_cuda:
                    ins, targs = ins.cuda(), targs.cuda()
                ins, targs = Variable(ins), Variable(targs)
                optimizer.zero_grad()
                outs = self(ins)
                loss = F.cross_entropy(outs, targs)
                total_loss += loss.data[0]
                preds = outs.data.max(1)[1]
                total_correct += preds.eq(targs.data).cpu().sum()
                loss.backward()
                optimizer.step()

            val_loss, val_acc = self.evaluate(val_data)
            log(fh, "\t\tTraining loss: %.3f \taccuracy: %.2f"
                    % (total_loss / tr_data.n_batches,
                        100. * total_correct / tr_data.n_ins))
            log(fh, "\t\tVal loss: %.3f \taccuracy: %.2f \t(%.3f s)"
                    % (val_loss, val_acc, time.time()-start_time))
            if val_acc > best_acc:
                if args.save_model_to:
                    torch.save(self.state_dict(), args.save_model_to)
                    log(fh, "\t\tSaved model to %s" % args.save_model_to)
                best_acc = val_acc
            if abs(last_acc - val_acc) < .01:
                #pdb.set_trace()
                #self.final_conv.register_forward_hook(print_outputs)
                #self.final_conv.register_backward_hook(print_grads)
                pass
            last_acc = val_acc
            scheduler.step(epoch)

        if args.save_model_to:
            self.load_state_dict(torch.load(args.save_model_to))
        _, val_acc = self.evaluate(val_data)
        log(fh, "\tFinished training in %.3f s, \tBest validation accuracy: %.2f" %
                (time.time() - start_time, val_acc))

    def evaluate(self, data):
        '''
        Evaluate model on data, usually either validation or test
        '''
        self.eval()
        total_loss, total_correct = 0., 0.
        for batch_idx in xrange(data.n_batches):
            ins, targs = data[batch_idx]
            if self.use_cuda:
                ins, targs = ins.cuda(), targs.cuda()
            ins, targs = Variable(ins, volatile=True), Variable(targs)
            outs = self(ins)
            total_loss += F.cross_entropy(outs, targs, size_average=False).data[0]
            preds = outs.data.max(1)[1]
            total_correct += preds.eq(targs.data).cpu().sum()
        return total_loss / data.n_ins, \
                100. * total_correct / data.n_ins

    def predict(self, data):
        '''
        Get predictions for data
        '''
        self.eval()
        predictions = []
        for batch_idx in xrange(data.n_batches):
            ins, targs = data[batch_idx]
            if self.use_cuda:
                ins, targs = ins.cuda(), targs.cuda()
            ins, targs = Variable(ins, volatile=True), Variable(targs)
            outs = self(ins)
            predictions.append(outs.data.max(1)[1].cpu().numpy())
        return np.vstack(predictions)

    def get_gradient(self, ins, targs):
        self.eval()
        if self.use_cuda:
            ins, targs = ins.cuda(), targs.cuda()
        ins, targs = Variable(ins, requires_grad=True), Variable(targs)
        outs = self(ins)
        loss = F.cross_entropy(outs, targs)
        loss.backward(retain_variables=True)
        return ins.grad.data.cpu().numpy()

def squeezenet1_0(pretrained=False, **kwargs):
    r"""SqueezeNet model architecture from the `"SqueezeNet: AlexNet-level
    accuracy with 50x fewer parameters and <0.5MB model size"
    <https://arxiv.org/abs/1602.07360>`_ paper.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = SqueezeNet(version=1.0, **kwargs)
    if pretrained:
        #model.load_state_dict(model_zoo.load_url(model_urls['squeezenet1_0']))
        model.load_state_dict(torch.load(model_paths['squeezenet1_0']))
    return model

def squeezenet1_1(pretrained=False, **kwargs):
    r"""SqueezeNet 1.1 model from the `official SqueezeNet repo
    <https://github.com/DeepScale/SqueezeNet/tree/master/SqueezeNet_v1.1>`_.
    SqueezeNet 1.1 has 2.4x less computation and slightly fewer parameters
    than SqueezeNet 1.0, without sacrificing accuracy.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = SqueezeNet(version=1.1, **kwargs)
    if pretrained:
        #model.load_state_dict(model_zoo.load_url(model_urls['squeezenet1_1']))
        model.load_state_dict(torch.load(model_paths['squeezenet1_1']))
    return model


from django.urls import path
from . import views

urlpatterns = [
    path("", views.Home, name="home"),
    path('matchesps', views.matches_per_season_charts, name='matches_ps'),
    path('winsperseason', views.wins_per_season_charts, name='wins_ps'),
    path('extraruns', views.extra_runs_charts, name='extra_runs'),
    path('economy', views.economy_charts, name='economy'),
    path('batting_average', views.batting_average_charts, name='batting_avg'),
    path("json/matchesps/", views.matches_per_season, name="matches"),
    path("json/winsperseason/", views.wins_per_season, name="wins"),
    path("json/extraruns/", views.extra_runs, name="extra"),
    path("json/economy/", views.economy, name="econ"),
    path("json/batting_average/", views.batting_average, name="batting_average"),

]


import gym
import numpy as np
import imageio
import os
import sys
from pyglet.window import key

from data import LEFT, RIGHT, GO, ACTIONS

samples_each_classes = 200

def action_to_id(a):
    if all(a == [-1, 0, 0]): return LEFT
    elif all(a == [1, 0, 0]): return RIGHT
    else:
        return GO

if __name__=='__main__':

    if len(sys.argv) < 2:
        sys.exit("Usage : python record_dataset.py path")

    env = gym.make('CarRacing-v0').env
    env.reset()

    folder = sys.argv[1]
    images = os.path.join(folder, "images")
    labels = os.path.join(folder, "labels.txt")
    os.makedirs(images, exist_ok=True)

    a = np.array([0.0, 0.0, 0.0])

    def key_press(k, mod):
        global restart
        if k==key.LEFT:  a[0] = -1.0
        if k==key.RIGHT: a[0] = +1.0
        if k==key.UP:    a[1] = +1.0
        if k==key.DOWN:  a[2] = +0.8   # set 1.0 for wheels to block to zero rotation
    def key_release(k, mod):
        if k==key.LEFT  and a[0]==-1.0: a[0] = 0
        if k==key.RIGHT and a[0]==+1.0: a[0] = 0
        if k==key.UP:    a[1] = 0
        if k==key.DOWN:  a[2] = 0

    env.viewer.window.on_key_press = key_press
    env.viewer.window.on_key_release = key_release
    env.reset()
    for i in range(100):
        env.step([0, 0, 0])
        env.render()

    file_labels = open(labels, 'w')
    samples_saved = {a: 0 for a in ACTIONS}

    i = 0
    while True:
        s, r, done, info = env.step(a)
        action_id = action_to_id(a)
        if samples_saved[action_id] < samples_each_classes:
            samples_saved[action_id] += 1
            samples_each_classes
            imageio.imwrite(os.path.join(folder, 'images', 'img-%s.jpg' % i ), s)
            file_labels.write('%s %s\n' % ('img-%s.jpg' % i, action_id))
            file_labels.flush()
            i += 1
            print(samples_saved)
        env.render()
    env.close()


'''
Single Layer Perceptron
Author: Yuya Jeremy Ong (yuyajeremyong@gmail.com)
'''
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F

class SLP(nn.Module):
    def __init__(self, input_shape, output_shape, hidden_shape=40, device=torch.device('cpu')):
        super(SLP, self).__init__()

        self.device = device
        self.input_shape = input_shape[0]
        self.hidden_shape = hidden_shape
        self.output_shape = output_shape

        self.linear = nn.Linear(self.input_shape, self.hidden_shape)
        self.out = nn.Linear(self.hidden_shape, self.output_shape)

    def forward(self, x):
        x = torch.from_numpy(x).float().to(self.device)
        x = F.relu(self.linear(x))
        x = self.out(x)

        return x


# Embedded file name: scripts/client/gui/Scaleform/daapi/view/lobby/profile/ProfileUtils.py
import BigWorld
from debug_utils import LOG_ERROR, LOG_DEBUG
from gui.shared.formatters import text_styles
from gui.shared.utils.functions import getClanRoleString
from helpers import i18n
from gui.shared import g_itemsCache
from gui.shared.gui_items.dossier.stats import _MaxVehicleStatsBlock
from gui.Scaleform.locale.PROFILE import PROFILE
from gui.Scaleform.locale.MENU import MENU
from helpers.i18n import makeString

class HeaderItemsTypes(object):
    VALUE_PREFIX = 'value_'
    COMMON = 'common'
    RATIO = 'ratio'
    VALUES = 'values'

class ProfileUtils(object):
    UNAVAILABLE_VALUE = -1
    UNAVAILABLE_SYMBOL = '--'
    VIEW_TYPE_TABLES = 0
    VIEW_TYPE_CHARTS = 1

    def __init__(self):
        super(ProfileUtils, self).__init__()

    @staticmethod
    def packProfileDossierInfo(targetData):
        outcome = ProfileUtils.packProfileCommonInfo(targetData)
        vehicle = g_itemsCache.items.getItemByCD(targetData.getMaxXpVehicle())
        outcome['maxXPByVehicle'] = vehicle.shortUserName if vehicle is not None else ''
        outcome['marksOfMastery'] = targetData.getMarksOfMastery()[3]
        outcome['totalUserVehiclesCount'] = len(targetData.getVehicles())
        return outcome

    @staticmethod
    def packProfileCommonInfo(targetData):
        outcome = {}
        outcome['battlesCount'] = targetData.getBattlesCount()
        outcome['lossesCount'] = targetData.getLossesCount()
        outcome['winsCount'] = targetData.getWinsCount()
        outcome['hitsEfficiency'] = ProfileUtils.getValueOrUnavailable(targetData.getHitsEfficiency())
        outcome['maxXP'] = targetData.getMaxXp()
        outcome['avgXP'] = ProfileUtils.getValueOrUnavailable(targetData.getAvgXP())
        return outcome

    @staticmethod
    def getIconPath(icon):
        return '../maps/icons/library/dossier/' + icon

    @staticmethod
    def formatFloatPercent(value):
        if value != ProfileUtils.UNAVAILABLE_VALUE:
            value = value * 100
            value = BigWorld.wg_getNiceNumberFormat(value) + '%'
        return str(value)

    @staticmethod
    def getFormattedWinsEfficiency(targetData):
        winsEfficiency = targetData.getWinsEfficiency()
        formattedWinsEfficiency = ProfileUtils.formatFloatPercent(ProfileUtils.getValueOrUnavailable(winsEfficiency))
        return formattedWinsEfficiency

    @staticmethod
    def formatEfficiency(coeff2, valueReceiveFunction):
        if coeff2 > 0:
            return BigWorld.wg_getNiceNumberFormat(valueReceiveFunction())
        else:
            return ProfileUtils.UNAVAILABLE_VALUE

    @staticmethod
    def packLditItemData(text, description, tooltip, icon, additionalData = None, viewType = HeaderItemsTypes.COMMON):
        finalText = text
        enabled = True
        if text == -1 or text == '-1':
            enabled = False
            finalText = ProfileUtils.UNAVAILABLE_SYMBOL
        return {'type': viewType,
         'text': finalText,
         'description': i18n.makeString(description),
         'iconPath': ProfileUtils.getIconPath(icon),
         'tooltip': tooltip,
         'additionalData': additionalData,
         'enabled': enabled}

    @staticmethod
    def getValueOrUnavailable(targetValue):
        if targetValue is not None:
            return targetValue
        else:
            return ProfileUtils.UNAVAILABLE_VALUE

    @staticmethod
    def getDrawCount(battlesCount, lossesCount, winsCount):
        return battlesCount - (winsCount + lossesCount)

    @staticmethod
    def getLabelDataObject(label, data):
        return {'label': i18n.makeString(label),
         'data': data}

    @staticmethod
    def getLabelViewTypeDataObject(label, data, viewType):
        if viewType != ProfileUtils.VIEW_TYPE_TABLES and viewType != ProfileUtils.VIEW_TYPE_CHARTS:
            LOG_ERROR('Unknown view type', viewType)
            return None
        else:
            result = ProfileUtils.getLabelDataObject(label, data)
            result['viewType'] = viewType
            return result

    @staticmethod
    def createToolTipData(bodyParamsList):
        result = {}
        bodyData = None
        if bodyParamsList is not None:
            bodyData = {}
            for i in range(0, len(bodyParamsList)):
                bodyData[HeaderItemsTypes.VALUE_PREFIX + str(i)] = text_styles.titleFont(str(bodyParamsList[i]))

        result['body'] = bodyData
        result['header'] = {}
        result['note'] = None
        return result

    @staticmethod
    def getTotalBattlesHeaderParam(targetData, description, tooltip):
        battlesCount = targetData.getBattlesCount()
        lossesCount = targetData.getLossesCount()
        winsCount = targetData.getWinsCount()
        drawsCount = ProfileUtils.getDrawCount(battlesCount, lossesCount, winsCount)
        drawsStr = BigWorld.wg_getIntegralFormat(drawsCount) if drawsCount >= 0 else ProfileUtils.UNAVAILABLE_SYMBOL
        battlesToolTipData = [BigWorld.wg_getIntegralFormat(winsCount), BigWorld.wg_getIntegralFormat(lossesCount), drawsStr]
        return ProfileUtils.packLditItemData(BigWorld.wg_getIntegralFormat(battlesCount), description, tooltip, 'battles40x32.png', {'tooltipData': ProfileUtils.createToolTipData(battlesToolTipData)}, HeaderItemsTypes.VALUES)

    @staticmethod
    def getVehicleRecordTooltipData(getValueMethod):
        return ProfileUtils.getRecordTooltipDataByVehicle(g_itemsCache.items.getItemByCD(getValueMethod()))

    @staticmethod
    def getRecordTooltipDataByVehicle(vehicle):
        if vehicle is not None:
            return ProfileUtils.createToolTipData([vehicle.shortUserName])
        else:
            return ProfileUtils.createToolTipData(None)

    @staticmethod
    def getAvailableValueStr(value):
        if value != -1:
            return value
        return ProfileUtils.UNAVAILABLE_SYMBOL

    @staticmethod
    def getAvgDamageBlockedValue(targetData):
        value = ProfileUtils.formatEfficiency(targetData.getBattlesCountVer3(), targetData.getAvgDamageBlocked)
        return ProfileUtils.getAvailableValueStr(value)

class DetailedStatisticsUtils(object):

    @staticmethod
    def getDetailedDataObject(label, value, tooltip, tooltipData = None):
        dataObject = ProfileUtils.getLabelDataObject(label, value)
        dataObject['tooltip'] = tooltip
        dataObject['tooltipData'] = tooltipData
        return dataObject

    @staticmethod
    def getStatistics(targetData, isCurrentuser):
        battlesCount = targetData.getBattlesCount()
        lossesCount = targetData.getLossesCount()
        winsCount = targetData.getWinsCount()
        drawsCount = battlesCount - (winsCount + lossesCount)
        drawsStr = BigWorld.wg_getIntegralFormat(drawsCount) if drawsCount >= 0 else ProfileUtils.UNAVAILABLE_SYMBOL
        battlesToolTipData = [BigWorld.wg_getIntegralFormat(winsCount), BigWorld.wg_getIntegralFormat(lossesCount), drawsStr]
        formattedBattlesCount = BigWorld.wg_getIntegralFormat(battlesCount)
        winsEfficiency = targetData.getWinsEfficiency()
        formattedWinsEfficiency = ProfileUtils.formatFloatPercent(ProfileUtils.getValueOrUnavailable(winsEfficiency))
        dmgDealt = targetData.getDamageDealt()
        dmgReceived = targetData.getDamageReceived()
        damageBlockedByArmor = targetData.getDamageBlockedByArmor()
        potentialDamageReceived = targetData.getPotentialDamageReceived()
        pResDmg = potentialDamageReceived - damageBlockedByArmor
        avgXP = ProfileUtils.getValueOrUnavailable(targetData.getAvgXP())
        avgDmg = ProfileUtils.getValueOrUnavailable(targetData.getAvgDamage())
        maxXP = targetData.getMaxXp()
        maxXP_formattedStr = BigWorld.wg_getIntegralFormat(maxXP)
        maxXpVehicle = None
        maxDamageVehicle = None
        maxFragsVehicle = None
        if isinstance(targetData, _MaxVehicleStatsBlock):
            maxXpVehicle = g_itemsCache.items.getItemByCD(targetData.getMaxXpVehicle())
            maxDamageVehicle = g_itemsCache.items.getItemByCD(targetData.getMaxDamageVehicle())
            maxFragsVehicle = g_itemsCache.items.getItemByCD(targetData.getMaxFragsVehicle())
        maxExpToolTipData = ProfileUtils.getRecordTooltipDataByVehicle(maxXpVehicle)
        maxDestroyedToolTipData = ProfileUtils.getRecordTooltipDataByVehicle(maxFragsVehicle)
        maxDmg = targetData.getMaxDamage()
        maxDamageToolTipData = None
        if targetData.getBattlesCountVer3() > 0:
            maxDmg_formattedStr = BigWorld.wg_getIntegralFormat(maxDmg)
            maxDamageToolTipData = ProfileUtils.getRecordTooltipDataByVehicle(maxDamageVehicle)
            maxDamageTooltip = PROFILE.PROFILE_PARAMS_TOOLTIP_MAXDAMAGE
        else:
            maxDmg_formattedStr = ProfileUtils.UNAVAILABLE_VALUE
            maxDamageTooltip = PROFILE.PROFILE_PARAMS_TOOLTIP_UNAVAILABLEMAXDAMAGE
        return (ProfileUtils.getLabelDataObject(PROFILE.SECTION_STATISTICS_BODYPARAMS_LABEL_COMMON, [DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_SCORES_TOTALBATTLES, formattedBattlesCount, PROFILE.PROFILE_PARAMS_TOOLTIP_BATTLESCOUNT, ProfileUtils.createToolTipData(battlesToolTipData)),
          DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_SCORES_TOTALWINS, formattedWinsEfficiency, PROFILE.PROFILE_PARAMS_TOOLTIP_WINS),
          DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_SCORES_SURVIVAL, ProfileUtils.formatFloatPercent(ProfileUtils.getValueOrUnavailable(targetData.getSurvivalEfficiency())), PROFILE.PROFILE_PARAMS_TOOLTIP_SURVIVAL),
          DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_SCORES_HITS, ProfileUtils.formatFloatPercent(ProfileUtils.getValueOrUnavailable(targetData.getHitsEfficiency())), PROFILE.PROFILE_PARAMS_TOOLTIP_HITS),
          None,
          DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_DETAILED_DAMAGECOEFFICIENT, ProfileUtils.formatEfficiency(dmgReceived, targetData.getDamageEfficiency), PROFILE.PROFILE_PARAMS_TOOLTIP_DAMAGECOEFF, ProfileUtils.createToolTipData((BigWorld.wg_getIntegralFormat(dmgDealt), BigWorld.wg_getIntegralFormat(dmgReceived)))),
          DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_DETAILED_DESTRUCTIONCOEFFICIENT, ProfileUtils.formatEfficiency(targetData.getDeathsCount(), targetData.getFragsEfficiency), PROFILE.PROFILE_PARAMS_TOOLTIP_DESTROYCOEFF, ProfileUtils.createToolTipData((BigWorld.wg_getIntegralFormat(targetData.getFragsCount()), BigWorld.wg_getIntegralFormat(targetData.getDeathsCount())))),
          DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_SCORES_ARMORUSING, ProfileUtils.formatEfficiency(pResDmg, targetData.getArmorUsingEfficiency), PROFILE.PROFILE_PARAMS_TOOLTIP_ARMORUSING, ProfileUtils.createToolTipData([ProfileUtils.getAvgDamageBlockedValue(targetData)]))]), ProfileUtils.getLabelDataObject(PROFILE.SECTION_STATISTICS_BODYPARAMS_LABEL_AVERAGE, [DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_SCORES_AVGEXPERIENCE_SHORT, BigWorld.wg_getIntegralFormat(avgXP), PROFILE.PROFILE_PARAMS_TOOLTIP_AVGEX_SHORT),
          None,
          DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_DETAILED_AVGDAMAGE, BigWorld.wg_getIntegralFormat(avgDmg), PROFILE.PROFILE_PARAMS_TOOLTIP_AVGDMG_SHORT),
          DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_DETAILED_AVGRECEIVEDDAMAGE, BigWorld.wg_getIntegralFormat(ProfileUtils.getValueOrUnavailable(targetData.getAvgDamageReceived())), PROFILE.PROFILE_PARAMS_TOOLTIP_AVGRECEIVEDDAMAGE),
          DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_SCORES_AVGASSISTEDDAMAGE_SHORTSELF if isCurrentuser else PROFILE.SECTION_STATISTICS_SCORES_AVGASSISTEDDAMAGE_SHORTOTHER, ProfileUtils.formatEfficiency(targetData.getBattlesCountVer2(), targetData.getDamageAssistedEfficiency), PROFILE.PROFILE_PARAMS_TOOLTIP_AVGASSISTEDDAMAGE_SHORTSELF if isCurrentuser else PROFILE.PROFILE_PARAMS_TOOLTIP_AVGASSISTEDDAMAGE_SHORTOTHER),
          None,
          DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_DETAILED_AVGDETECTEDENEMIES, BigWorld.wg_getNiceNumberFormat(ProfileUtils.getValueOrUnavailable(targetData.getAvgEnemiesSpotted())), PROFILE.PROFILE_PARAMS_TOOLTIP_AVGDETECTEDENEMIES),
          DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_DETAILED_AVGDESTROYEDVEHICLES, BigWorld.wg_getNiceNumberFormat(ProfileUtils.getValueOrUnavailable(targetData.getAvgFrags())), PROFILE.PROFILE_PARAMS_TOOLTIP_AVGDESTROYEDVEHICLES)]), ProfileUtils.getLabelDataObject(PROFILE.SECTION_STATISTICS_BODYPARAMS_LABEL_RECORD, [DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_SCORES_MAXEXPERIENCE, maxXP_formattedStr, PROFILE.PROFILE_PARAMS_TOOLTIP_MAXEXP, maxExpToolTipData), DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_SCORES_MAXDAMAGE, maxDmg_formattedStr, maxDamageTooltip, maxDamageToolTipData), DetailedStatisticsUtils.getDetailedDataObject(PROFILE.SECTION_STATISTICS_DETAILED_MAXDESTROYEDVEHICLES, BigWorld.wg_getIntegralFormat(targetData.getMaxFrags()), PROFILE.PROFILE_PARAMS_TOOLTIP_MAXDESTROYED, maxDestroyedToolTipData)]))

def getProfileCommonInfo(userName, dossier, clanInfo, clanEmblemId):
    clanNameProp = 'clanName'
    clanNameDescrProp = 'clanNameDescr'
    clanJoinTimeProp = 'clanJoinTime'
    clanPositionProp = 'clanPosition'
    clanEmblemProp = 'clanEmblem'
    import cgi
    lastBattleTimeUserString = None
    if dossier['total']['lastBattleTime']:
        lbt = dossier['total']['lastBattleTime']
        lastBattleTimeUserString = '%s %s' % (BigWorld.wg_getLongDateFormat(lbt), BigWorld.wg_getShortTimeFormat(lbt))
    value = {'name': userName,
     'registrationDate': '%s' % BigWorld.wg_getLongDateFormat(dossier['total']['creationTime']),
     'lastBattleDate': lastBattleTimeUserString,
     clanNameProp: '',
     clanNameDescrProp: '',
     clanJoinTimeProp: '',
     clanPositionProp: '',
     clanEmblemProp: 'img://' + clanEmblemId if clanEmblemId is not None else None}

    def getGoldFmt(str):
        return str

    if clanInfo is not None:
        value[clanNameProp] = cgi.escape(clanInfo[1])
        value[clanNameDescrProp] = cgi.escape(clanInfo[0])
        value[clanJoinTimeProp] = makeString(MENU.PROFILE_HEADER_CLAN_JOINDATE) % getGoldFmt(BigWorld.wg_getLongDateFormat(clanInfo[4]))
        value[clanPositionProp] = getGoldFmt(getClanRoleString(clanInfo[3]))
    return value


import numpy as np
import matplotlib.pyplot as pl
import hazel
import h5py
import sys

filename = sys.argv[1]
x_coordinate = int(sys.argv[2])
y_coordinate = int(sys.argv[3])
x_range = int(sys.argv[4])
y_range = int(sys.argv[5])
l_left = int(sys.argv[6])
l_right = int(sys.argv[7])
local = int(sys.argv[8])
output = sys.argv[9]

#Here we read the data from the cube:
from astropy.io import fits

dataset = fits.open(filename)
stokes = dataset[0].data
ll = dataset[1].data

n_wvl = l_right - l_left

if (local):
	loc_continuum = np.amax(stokes[:,:,0,l_left:l_right],axis=2)
	stokes /= loc_continuum[:,:,np.newaxis,np.newaxis]

else:
	qs = np.mean(np.amax(stokes[:,:,0,l_left:l_right],axis=2))
	stokes[:,:,:,:] /= qs

# Save the data for the inversion
# First the wavelength axis
np.savetxt('10830_gregor.wavelength', ll[l_left:l_right], header='lambda')

# Now the wavelength dependent weights
f = open('10830_gregor.weights', 'w')
f.write('# WeightI WeightQ WeightU WeightV\n')
for i in range(n_wvl):
    f.write('1.0    1.0   1.0   1.0\n')
f.close()

noise = 2E-3
# And the Stokes parameters
n_pixel = x_range*y_range

stokes_3d = stokes[x_coordinate:x_coordinate+x_range,y_coordinate:y_coordinate+y_range,:,l_left:l_right]
stokes_3d = stokes_3d.reshape(x_range*y_range,4,n_wvl)
stokes_3d = stokes_3d.transpose(0,2,1)
sigma_3d = np.zeros((n_pixel,n_wvl,4), dtype=np.float64)
sigma_3d[:,:,:] = noise
los_3d = np.zeros((n_pixel,3), dtype=np.float64)
los_3d[:,0] = 0.0
los_3d[:,1] = 0.0
los_3d[:,2] = 90.0
boundary_3d = np.zeros((n_pixel,n_wvl,4), dtype=np.float64)
boundary_3d[:,:,0] = 1.0

f = h5py.File(output, 'w')
db_stokes = f.create_dataset('stokes', stokes_3d.shape, dtype=np.float64)
db_sigma = f.create_dataset('sigma', sigma_3d.shape, dtype=np.float64)
db_los = f.create_dataset('LOS', los_3d.shape, dtype=np.float64)
db_boundary = f.create_dataset('boundary', boundary_3d.shape, dtype=np.float64)
db_stokes[:] = stokes_3d
db_sigma[:] = sigma_3d
db_los[:] = los_3d
db_boundary[:] = boundary_3d
f.close()


from collections import OrderedDict
import os
from argparse import Namespace
from typing import List, Tuple

import pkg_resources
import torch
from tqdm import tqdm
from hrnet_pose import models
from hrnet_pose.config import cfg, update_config
from hrnet_pose.core.inference import get_final_preds
from PIL import Image
from torchvision import transforms

INPUT_SIZE = 256, 192
IMAGE_SIZE = 176, 256
TRESHOLD = 0.15
SCALE_FACTOR = 4

DEFAULT_ARGS = Namespace(
    cfg=pkg_resources.resource_filename(
        'hrnet_pose', 'yaml/coco/hrnet/w48_256x192_adam_lr1e-3.yaml'),
    dataDir='.',
    logDir='.',
    modelDir='.',
    prevModelDir='.'
)

def transform_preds(p):
    # rotate -90 deg and scale to image dimensions
    p[..., 1] = (INPUT_SIZE[1] - 1 - p[..., 1]*SCALE_FACTOR) * IMAGE_SIZE[0] // INPUT_SIZE[1]
    p[..., 0] = p[..., 0] * SCALE_FACTOR * IMAGE_SIZE[1] // INPUT_SIZE[0]

class PoseEstimator:

    def __init__(self, args, use_cuda):
        update_config(cfg, args)
        self.model = models.pose_hrnet.get_pose_net(cfg, False)
        if use_cuda:
            self.model = self.model.cuda()
            self.model.load_state_dict(torch.load(cfg.TEST.MODEL_FILE), strict=False)
        else:
            self.model.load_state_dict(torch.load(cfg.TEST.MODEL_FILE, map_location=torch.device('cpu')), strict=False)
        self.model.eval()
        self.use_cuda = use_cuda
        self.transform = transforms.Compose([
            transforms.Resize(INPUT_SIZE),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
            transforms.Lambda(lambda t: torch.rot90(t, dims=(1, 2)))
        ])

    def infer(self, image: Image):
        """Takes an input RGB image <= INPUT_SIZE and returns an (17 x 2)-array"""
        batch = self.transform(image).unsqueeze(0)
        if self.use_cuda:
            batch = batch.cuda()
        with torch.no_grad():
            output = self.model(batch).detach().cpu().numpy()
        preds, maxvals = get_final_preds(output)
        transform_preds(preds)
        points = preds[0].astype(int)
        maxvals = maxvals[0]
        invalid = maxvals[..., 0] < TRESHOLD
        points[invalid] = -1
        return points

    def infer_batch(self, images):
        batch = torch.stack([self.transform(image) for image in images])
        if self.use_cuda:
            batch = batch.cuda()
        with torch.no_grad():
            output = self.model(batch).detach().cpu().numpy()
        preds, maxvals = get_final_preds(output)
        transform_preds(preds)
        points = preds.astype(int)
        invalid = maxvals[..., 0] < TRESHOLD
        points[invalid] = -1
        return points

if __name__ == '__main__':
    # most important parameters
    import sys
    input_folder, output_path = sys.argv[1:]
    pose_estimator = 'assets/pretrains/pose_hrnet_w48_256x192.pth'

    batch_size = 128

    args = DEFAULT_ARGS
    args.opts = ['TEST.MODEL_FILE', pose_estimator]

    model = PoseEstimator(args, use_cuda=True)

    with open(output_path, 'w') as result_file:
        processed_names = set()
        print('name:keypoints_y:keypoints_x', file=result_file)
        batch = OrderedDict()
        for image_name in tqdm(os.listdir(input_folder)):
            if '.ipynb_checkpoints' in image_name:
                continue

            image = Image.open(os.path.join(input_folder, image_name))
            batch[image_name] = image
            if len(batch) >= batch_size:
                for iname, pose_cords in zip(batch.keys(), model.infer_batch(list(batch.values()))):
                    print("%s: %s: %s" % (iname, list(pose_cords[:, 0]), list(
                        pose_cords[:, 1])), file=result_file)
                result_file.flush()
                batch = OrderedDict()
        if batch:
            for iname, pose_cords in zip(batch.keys(), model.infer_batch(list(batch.values()))):
                print("%s: %s: %s" % (iname, list(pose_cords[:, 0]), list(
                    pose_cords[:, 1])), file=result_file)
            result_file.flush()


# !/usr/bin/env python3
# -*- coding:utf-8 -*-
import jieba
import json

# 讀取 json 的程式
def jsonTextReader(jsonFilePath):
    with open(jsonFilePath, 'r', encoding='utf-8') as f:
        jsonFile = json.load(f)
        return jsonFile['text']

# 將字串轉為「句子」列表的程式
def text2Sentence(inputSTR):
    # replace '...' and '…' with ''
    unused = ['...', '…']
    for sep in unused:
        inputSTR = inputSTR.replace(sep, '')

    # replace '，', '、', '。', '「', '」' with separator '\n'
    separators = ['，', '、', '。', '「', '」']
    for sep in separators:
        inputSTR = inputSTR.replace(sep, '\n')

    # if ',' is not located in number, replace it with '\n'
    currentIndex = 0
    while True:
        # find next ','
        currentIndex = inputSTR.find(',', currentIndex)

        # ',' not find
        if currentIndex == -1:
            break

        # ',' is located in numbers
        if str.isdigit(inputSTR[currentIndex - 1]) and str.isdigit(inputSTR[currentIndex + 1]):
            currentIndex += 1
            continue

        inputSTR = inputSTR[:currentIndex] + '\n' + inputSTR[currentIndex + 1:]
        currentIndex += 1

    return inputSTR.strip('\n').split('\n')

def cutSentence(strList):
    resultList = []
    for s in strList:
        resultList.append("/".join(jieba.cut(s, cut_all=False)))
    return resultList

if __name__ == "__main__":
    # 設定要讀取的 news.json 路徑
    newsJsonPath = './example/news.json'

    # 將 news.json 利用 [讀取 json] 的程式打開
    text = jsonTextReader(newsJsonPath)

    # 將讀出來的內容字串傳給 [將字串轉為「句子」 列表」]的程式，存為 newsLIST
    newsLIST = text2Sentence(text)

    print("\n".join(cutSentence(newsLIST)))


# Problem available at: https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/542/week-4-june-22nd-june-28th/3371/

# Question:
'''
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
'''

class Solution:
    def findDuplicate(self, nums: List[int]) -> int:
        counter = Counter(nums)

        for k in counter.keys():
            if counter[k] >1:
                return k

# =========================================================
class Solution:
    def findDuplicate(self, nums: List[int]) -> int:
        set_ = set()

        for num in nums:
            if num in set_:
                return num

            else:
                set_.add(num)


import random
#Characters (each should be a matrix with the format [name], [hp, stamina, mana], [ presence (impacts social), heart (impacts relationships), stability (impacts wound penalties, reduces trauma)], [grace (impacts accuracy and combos), poise (impacts armor and wound penalties), skill (impacts crits and damage)], [affinity (impacts spell damage or healing and # of enemies hit), control (affects spell crits and penalties of a spell failure), concentration (affects wound penalties and accuracy)], [items in inventory], [traumas and permanent traits], [battle effects])
class Enemy:
  _STAT_KEYS = ["SocialPresence", "SocialHeart", "SocialStability", "PhysicalGrace", "PhysicalSkill", "PhysicalPoise", "MagicalAffinity", "MagicalControl", "MagicalConcentration"]
  def __init__(self, name, maxHP, HP, timeGiven, SocialPresence, SocialHeart, SocialStability, PhysicalGrace, PhysicalSkill, PhysicalPoise, MagicalAffinity, MagicalControl, MagicalConcentration,move1, move2, move3, moveChanceDistr, effects=[],):
    self.Name = name
    self.MaxHP = maxHP
    self.HP = HP
    self.TimeGiven = timeGiven
    self.Stats = {
      "SocialPresence": SocialPresence,
      "SocialHeart": SocialHeart,
      "SocialStability": SocialStability,
      "PhysicalGrace": PhysicalGrace,
      "PhysicalSkill": PhysicalSkill,
      "PhysicalPoise": PhysicalPoise,
      "MagicalAffinity": MagicalAffinity,
      "MagicalControl": MagicalControl,
      "MagicalConcentration": MagicalConcentration
    }
    self.AC = PhysicalGrace * 2
    self.Damage = PhysicalSkill
    self.move1 = move1
    self.move2 = move2
    self.move3 = move3
    self.moves = [self.move1, self.move2, self.move3]
    scale = 100 / sum(moveChanceDistr)
    self.moveChanceDistr = []
    for i in range(3):
      self.moveChanceDistr.append(scale*moveChanceDistr[i])
    self.Effects = effects

  def turn(self, party, enemies):
    """party and enemies are from the point of view of the player"""
    self.applyEffects()
    move_roll = random.randint(1, 100)
    for i in range(3):
      if move_roll <= self.moveChanceDistr[i]:
        move = self.moves[i]
      else:
        move_roll -= self.moveChanceDistr[i]
    move.use(self, enemies, party, False)
    # target selection code moved to combat/move.py, in Move.enemy_target_select(), which is called by move.use()

  def dealDamage(self, damage):
    self.HP -= damage
    if self.HP < 0:
      self.HP = 0
    elif self.HP > self.MaxHP:
      self.HP = self.MaxHP
    #The die check will take place in battleFinished()
  def heal(self, healAmt):
    self.HP += healAmt
    if self.HP < 0:
      self.HP = 0
    elif self.HP > self.MaxHP:
      self.HP = self.MaxHP

  def setHP(self, newHP):
    self.HP = newHP

  #Makes a copy for individual enemies
  def copy(self):
    copy = Enemy(self.Name, self.MaxHP, self.HP, self.TimeGiven, self.Stats["SocialPresence"], self.Stats["SocialHeart"], self.Stats["SocialStability"], self.Stats["PhysicalGrace"], self.Stats["PhysicalSkill"], self.Stats["PhysicalPoise"], self.Stats["MagicalAffinity"], self.Stats["MagicalControl"], self.Stats["MagicalConcentration"], self.move1, self.move2, self.move3, self.moveChanceDistr.copy(), self.Effects.copy())
    return copy

  #Effects fields: Name, Damage, Duration, isWounds, Reducts, TimeReduct
  def applyEffects(self):
    """Applies effects to self and returns a copy of self.PermStats with the applied effects."""
    newStats = self.Stats.copy()
    newStatsKeys = newStats.keys()

    i = 0
    while i < len(self.Effects):
      effect = self.Effects[i]
      effect.duration -= 1
      if effect.duration == 0:
        del self.Effects[i] #If the effect is removed, a new effect will now be at the same index- Should not increment i
      elif effect.duration < 0:
        effect.duration = -1
      else:
        self.HP -= effect.Damage
        for j in range(9):
          newStats[newStatsKeys[j]] -= effect.Reducts[j]
        i += 1
    return newStats
  def getStatsWithEffects(self):
    """Only returns modified copy of stats."""
    newStats = self.Stats.copy()
    newStatsKeys = newStats.keys()
    newHP = self.HP

    for i in range(len(self.Effects)):
      effect = self.Effects[i]
      newHP -= effect.Damage
      for j in range(9):
        newStats[newStatsKeys[j]] -= effect.Reducts[j]
    return newStats


# -*- coding: utf-8 -*-
"""
Created on Sun Nov  29 12:08:01 2020

@author: hp
"""

import numpy as np
import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
from matplotlib import ticker
#from StockQutoes import sq

def retrieve_parameters(tickerSymbol, startDate, endDate):
    return tickerSymbol, startDate, endDate

#use matplotlib to plot the graphs
def closing_price_vs_time(data, tickerSymbol, startDate, endDate):
    fig, ax = plt.subplots()
    time = data.index
    closing_price = data['Close']
    plt.figure(figsize=(14,7))
    plt.title("Closing price vs Date of {} within date range {} to {}".format(tickerSymbol.upper(), startDate, endDate))
    plt.xlabel('Date')
    plt.ylabel('Closing Price')
    plt.plot(time, closing_price, color = "blue", label = "Closing Price")
    fig.autofmt_xdate()
    plt.legend()
    download_plot(tickerSymbol.upper() + '_CLOSE.png')
    plt.show()

    change_parameter_message()

def adjClose_vs_time(data, tickerSymbol, startDate, endDate):
    fig, ax = plt.subplots()
    xAxis = data.index
    closeParametery2 = data['Adj Close']
    plt.figure(figsize=(14,7))
    plt.title('Adj Close vs Time Graph for {} between {} and {}'.format(tickerSymbol.upper(), startDate, endDate))
    plt.xlabel('Date')
    plt.ylabel('Adj Close')
    plt.plot(xAxis, closeParametery2, color = 'blue', label='Adj Close')    #
    fig.autofmt_xdate()
    plt.legend()
    download_plot(tickerSymbol.upper() + '_ADJC.png')
    plt.show()

    change_parameter_message()

def open_vs_close(data, tickerSymbol, startDate, endDate):
    fig, ax = plt.subplots()
    xAxis = data.index
    openParametery1 = data['Open']
    closeParametery2 = data['Close']
    plt.figure(figsize=(14,7))
    plt.xlabel('Date')
    plt.ylabel('Open and Close Prices')
    plt.title('Open vs Close vs Date plot for {} within the date range {} to {}'.format(tickerSymbol.upper(), startDate, endDate))
    plt.plot(xAxis, openParametery1, color = "r", label = "Open")
    plt.plot(xAxis, closeParametery2, color = "blue",  label="Close")
    fig.autofmt_xdate()
    plt.legend()
    download_plot(tickerSymbol.upper() + '_OC.png')
    plt.show()

    change_parameter_message()

def moving_average(data, tickerSymbol, startDate, endDate):
    movingWindow = int(input("Please enter the moving window: "))
    closeParameter = data['Close']
    movingAverage = closeParameter.rolling(window=movingWindow).mean()
    print("Moving Average: ", movingAverage)
    plt.figure(figsize=(14,7))
    plt.ylabel('Closing Price')
    plt.title("Moving Average vs Closing Price vs Date of {} within date range {} to {} for moving window {}".format(tickerSymbol.upper(), startDate, endDate, movingWindow))
    closeParameter.plot(label="Closing Price")
    movingAverage.plot(label="Moving Average")
    plt.legend()
    download_plot(tickerSymbol.upper() + '_MA.png')
    plt.show()

    change_parameter_message()

def weighted_moving_average(data, tickerSymbol, startDate, endDate):
    moving_window = input("Please provide a moving window: ")
    data['Close_moving_avg_n']=data.iloc[:,3].rolling(window=int(moving_window)).mean()
    print(data)
    int_moving_window=int(moving_window)
    weights=np.arange(1,int_moving_window+1)
    print(weights)
    data['WMA'] = data['Close'].rolling(int_moving_window).apply(lambda prices: np.dot(prices, weights)/weights.sum(), raw=True)
    print(data)
    plt.figure(figsize=(16,9))
    data[['Close','Close_moving_avg_n','WMA']].plot(linestyle='dashed')
    plt.title('Close Price vs Close price Moving Average vs Close Weighted Moving Average of {} for moving window {}'.format(tickerSymbol.upper(), moving_window))
    plt.ylabel('Price')
    download_plot(tickerSymbol.upper() + '_WMA.png')
    plt.show()

    change_parameter_message()

def moving_average_convergence_divergence(data, tickerSymbol, startDate, endDate):
    short_window = int(input("Enter the short window: "))
    long_window = int(input("Enter the long window: "))
    signalPeriod = int(input("Enter the signal period: "))
    closing_price = data['Close']
    print(closing_price)
    short = closing_price.ewm(span = short_window, adjust = False).mean()
    long = closing_price.ewm(span = long_window, adjust = False).mean()
    macd = short - long
    signal = closing_price.ewm(span = signalPeriod, adjust = False).mean()
    print(macd)
    print(signal)

    fig, ax = plt.subplots()
    plt.figure(figsize=(14,7))
    plt.title('Moving Average Convergence Divergence for {} within date range {} to {}'.format(tickerSymbol.upper(), startDate, endDate))
    plt.xlabel('Date')
    plt.ylabel('Closing Price')
    plt.plot(data.index, macd, color='blue', label='MACD')
    plt.plot(data.index, signal, color='r', label='Signal')
    fig.autofmt_xdate()
    plt.legend()
    download_plot(tickerSymbol.upper() + '_MACD.png')
    plt.show()

    change_parameter_message()

def linear_trend_lines(data, tickerSymbol, startDate, endDate):
    #divide x axis into n parts of data between 0 and 1
    date = np.linspace(0, 1, len(data.index))
    closingPrice = data['Close']
    #Create Polynomial fit of degree 1
    poly_fit = np.polyfit(date, closingPrice, 1)
    #Fit the 1 degree equation into ID
    polyid = np.poly1d(poly_fit)
    #get the y axis trend
    trend = polyid(date)

    #Plot the linear line graph
    plt.figure(figsize=(14,7))
    plt.title("Linear trendline for {} from {} - {}".format(tickerSymbol, startDate, endDate))
    plt.scatter(data.index, data.Close, label="Closing Price")
    plt.plot(data.index, trend, "r", label="Linear Trend for Closing Price")
    plt.xlabel("Date")
    plt.ylabel("Closing Price")
    plt.legend()
    download_plot(tickerSymbol.upper() + '_LTL.png')
    plt.show()

    change_parameter_message()

def change_parameter_message():
    print("\nThe plot is visible in the 'Plots' tab \n")
    print("If you want to view technical indicator graphs for another Ticker Symbol and date ramge,",
          "Enter 8 to exit the data visualization sub-menu, 0 to exit main menu, and enter 3 to input",
          "your new parameters. Else, continue inputting other options to plot graphs for existing parameters.")

def save_plot(filename):
    plt.savefig(filename)

def download_plot(filename):
    user_answer = input("Do you want to save the map in .png format? \nIf yes, please press Y else press N: ")
    if(user_answer == 'Y'):
        save_plot(filename)


import boto3
from boto3.dynamodb.conditions import Key, Attr

dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table("library")

def lambda_handler(event, context):
    query = event.get("query", "all")
    #Without filter
    if query == "all":
        response = table.scan()
        data = response["Items"]
    else:
    #With filter

        response = table.query(
            KeyConditionExpression=Key('books').eq(query)
            )
        data = response["Items"]

    return "\n".join([str(value) for value in data])

def main():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("-a", dest="command",  required=False, type=str, help="Command")
    parser.add_argument("-q", dest="query",    required=False, type=str, help="Query")
    args = parser.parse_args()

    if args.command == "fetch":
        event = {
            "query": args.query
        }
        response = lambda_handler(event, None)
        print(response)
    else:
        print(f"{args.command} not mapped")

if __name__ == "__main__":
    main()


import os
import re

from trac.core import Component, implements
from trac.config import BoolOption, IntOption, Option
from trac.admin.api import IAdminPanelProvider
from trac.util.translation import _
from trac.web.chrome import ITemplateProvider, add_stylesheet

class LogViewerPage(Component):

    implements(IAdminPanelProvider, ITemplateProvider)

    _autoload = BoolOption('logviewer', 'autoload', False,
        doc="""Whether the log file content should be automatically
        loaded when the module is called, i.e. even before the form
        was submitted. This is a boolean option (true/false), and
        defaults to false.
        """)

    _autolevel = IntOption('logviewer', 'autolevel', 3,
        doc="""Which log level shall be used on autoload (only applies
        when autoload is enabled). This integer value defaults to 3
        (warnings). Possible values: 1=critical, 2=error, 3=warning,
        4=info, 5=debug
        """)

    _autoup = BoolOption('logviewer', 'autoup', True,
        doc="""Include log events of higher levels than autolevel on
        autoload? This boolean option defaults to true - and only
        applies on autoload
        """)

    _autotail = IntOption('logviewer', 'autotail', 1000,
        doc="""Only applies to autoload: Restrict the evaluated lines
        to the last N lines. Defaults to 1000.
        """)

    _defaultlevel = IntOption('logviewer', 'defaultlevel', 3,
        doc="""Preset for the log level dropdown (if autoload is disabled).
        This integer value defaults to 3 (warnings). Possible values:
        1=critical, 2=error, 3=warning, 4=info, 5=debug
        """)

    _defaultup = BoolOption('logviewer', 'defaultup', True,
        doc="""Check the box to include log events of higher levels
        when autoload is disabled? This boolean option defaults to true.
        """)

    _defaulttail = IntOption('logviewer', 'defaulttail', 100,
        doc="""Preset for the Tail input (restrict query to the last N
        lines of the logfile to load). This must be a number (integer),
        and by default is empty (not set)
        """)

    # IAdminPageProvider methods

    def get_admin_panels(self, req):
        if 'TRAC_ADMIN' in req.perm:
            yield 'general', _('General'), 'logviewer', _('Log Viewer')

    def render_admin_panel(self, req, cat, page, path_info):
        data = {
            'err': [],
            'msg': [],
            'log': [],
        }
        autoload = self.config.getbool('logviewer', 'autoload')
        try:
            logfile = self._get_logfile_name()
            if not logfile:
                self.log.debug('No log file configured.')
                data['err'].append(
                    'There is no log file configured for this environment.')
        except IOError:
            self.log.debug('Got IOError - configured log file does not exist!')
            data['err'].append('The configured log file does not exist.')

        if logfile:
            params = {}
            if req.method == 'POST':
                params['level'] = req.args.get('level')
                params['up'] = req.args.get('up')
                params['invert'] = req.args.get('invertsearch')
                params['regexp'] = req.args.get('regexp')
                params['tail'] = req.args.getint('tail')
                params['filter'] = req.args.get('filter')
                data['log'] = self._do_process(params, logfile)
                data['level'] = req.args.getint('level')
                data['up'] = req.args.getint('up')
                data['invert'] = req.args.getint('invertsearch')
                data['regexp'] = req.args.getint('regexp')
                data['filter'] = req.args.get('filter', '')
                data['tail'] = req.args.get('tail', '')
            elif autoload:
                data['level'] = self.config.getint('logviewer', 'autolevel')
                data['up'] = self.config.getbool('logviewer', 'autoup')
                data['invert'] = 0
                data['regexp'] = 0
                data['filter'] = ''
                data['tail'] = self.config.get('logviewer', 'autotail')
                data['log'] = self._do_process(data, logfile)
            else:
                data['level'] = self.config.getint('logviewer', 'defaultlevel')
                data['up'] = int(self.config.getbool('logviewer', 'defaultup'))
                data['invert'] = 0
                data['regexp'] = 0
                data['filter'] = ''
                data['tail'] = self.config.getint('logviewer', 'defaulttail') \
                               or 100

        data['us_message'] = data['msg']
        data['us_error'] = data['err']
        data['us_log'] = data['log']

        add_stylesheet(req, 'logviewer/css/logviewer.css')

        return 'logviewer.html', data, None

    # ITemplateProvider methods

    def get_htdocs_dirs(self):
        from pkg_resources import resource_filename
        return [('logviewer', resource_filename(__name__, 'htdocs'))]

    def get_templates_dirs(self):
        from pkg_resources import resource_filename
        return [resource_filename(__name__, 'templates')]

    # Internal methods

    def _do_process(self, params, logfile):
        self.log.debug('Processing form data')
        log = self._get_log(logfile, params)
        return log

    def _get_logfile_name(self):
        if self.config.get('logging', 'log_type').lower() != 'file':
            return None
        name = self.config.get('logging', 'log_file')
        fpath, fname = os.path.split(name)
        if not fpath:
            name = os.path.join(self.env.path, 'log', name)
        if not os.path.exists(name):
            raise IOError
        self.log.debug('Logfile name: %s', name)
        return name

    def _get_log(self, logname, params):
        up = params['up']
        invert = params['invert']
        regexp = params['regexp']
        level = int(params['level'])
        tfilter = params['filter']
        tail = int(params['tail'] or 0)
        levels = ['', 'CRITICAL:', 'ERROR:', 'WARNING:', 'INFO:', 'DEBUG:']
        classes = ['', 'log_crit', 'log_err',
                   'log_warn', 'log_info', 'log_debug']
        log = []
        logline = {}
        try:
            with open(logname, 'r') as f:
                lines = f.readlines()
            linecount = len(lines)
            if tail and linecount - tail > 0:
                start = linecount - tail
            else:
                start = 0
            for i in range(start, linecount):
                line = lines[i].decode('utf-8', 'replace')
                if tfilter:
                    if regexp:
                        if not invert and not re.search(tfilter, line):
                            continue
                        if invert and re.search(tfilter, line):
                            continue
                    else:
                        if not invert and line.find(tfilter) == -1:
                            continue
                        if invert and not line.find(tfilter) == -1:
                            continue
                logline = {}
                if line.find(levels[level]) != -1:
                    logline['level'] = classes[level]
                    logline['line'] = line
                    log.append(logline)
                elif up:
                    i = level
                    found = False
                    while i > 0:
                        if line.find(levels[i]) != -1:
                            logline['level'] = classes[i]
                            logline['line'] = line
                            log.append(logline)
                            found = True
                        i -= 1
                    if not found and re.search('^[^0-9]+', line):
                        logline['level'] = 'log_other'
                        logline['line'] = line
                        log.append(logline)
        except IOError:
            self.log.debug('Could not read from logfile!')
        return log


# Copyright 2016 NeuroData (http://neurodata.io)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# group_func.py
# Created by Eric W Bridgeford on 2016-04-03.
# Email: ebridge2@jhu.edu

import warnings

warnings.simplefilter("ignore")
import pickle
from ndmg.stats.qa_mri import qa_mri as mqa
import numpy as np
import ndmg.utils as mgu
import os
import plotly.offline as pyo
from plotly.tools import FigureFactory as ff
from ndmg.utils import loadGraphs
from ndmg.stats.qa_graphs import compute_metrics
from ndmg.stats.qa_graphs_plotting import make_panel_plot
import networkx as nx
from ndmg.stats.plotly_helper import *

class group_func(object):
    def __init__(self, basedir, outdir, atlas=None, dataset=None):
        """
        A class for group level quality control.

        **Positional Arguments:**
            - basedir:
                - the ndmg-formatted functional outputs.
                  should have a qa/ folder contained within it.
            - outdir:
                - the directory to place all group level quality control.
            - dataset:
                - an optional parameter for the name of the dataset
                  to be present in the quality control output filenames.
        """
        print(atlas)
        self.ndmgdir = basedir
        self.qadir = "{}/qa".format(self.ndmgdir)
        self.outdir = outdir
        self.conn_dir = "{}/connectomes".format(self.ndmgdir)
        self.dataset = dataset
        self.atlas = atlas
        (self.qa_files, self.subs) = self.get_qa_files()
        self.connectomes = self.get_connectomes()
        self.qa_objects = self.load_qa()
        self.group_level_analysis()
        pass

    def get_qa_files(self):
        """
        A function to load the relevant quality assessment files,
        for all the subjects we have in our study, given a properly-formatted
        ndmg functional directory.
        """
        qa_files = []
        subs = []
        for sub in os.listdir(self.qadir):
            sub_qa = "{}/{}/{}_stats.pkl".format(self.qadir, sub, sub)
            # if the files exists, add it to our qa_files
            if os.path.isfile(sub_qa):
                qa_files.append(sub_qa)
                subs.append(sub)
        return (qa_files, subs)

    def get_connectomes(self):
        """
        A function to load the relevant connectomes for all of the subjects
        for each parcellation we have.
        """
        connectomes = {}
        for label in os.listdir(self.conn_dir):
            print(label)
            this_label = []
            label_dir = "{}/{}".format(self.conn_dir, label)
            for connectome in os.listdir(label_dir):
                conn_path = "{}/{}".format(label_dir, connectome)
                if os.path.isfile(conn_path):
                    this_label.append(conn_path)
            connectomes[label] = this_label
        return connectomes

    def load_qa(self):
        """
        A function to load the quality control objects.
        """
        qa_objects = []
        for qa_file in self.qa_files:
            # load the qa objects as qa_mri objects
            qa_objects.append(mqa.load(qa_file))
        return qa_objects

    def group_level_analysis(self):
        """
        A function to perform group level analysis after loading the
        functional qa objects properly.
        """
        self.group_reg()
        self.group_motion()

    def group_reg(self):
        """
        A function that performs group level registration quality control.
        """
        regdir = "{}/{}".format(self.outdir, "reg")
        cmd = "mkdir -p {}".format(regdir)
        mgu.execute_cmd(cmd)

        self_reg_sc = []
        temp_reg_sc = []
        cnr = []
        snr = []
        for sub in self.qa_objects:
            self_reg_sc.append(sub.self_reg_sc)
            temp_reg_sc.append(sub.temp_reg_sc)
            cnr.append(sub.cnr)
            snr.append(sub.snr)

        fig_cnr = plot_rugdensity(cnr)
        fig_snr = plot_rugdensity(snr)
        fig_sreg = plot_rugdensity(self_reg_sc)
        fig_treg = plot_rugdensity(temp_reg_sc)

        figs = [fig_cnr, fig_snr, fig_sreg, fig_treg]
        names = [
            "temporal Contrast to Noise Ratio",
            "temporal Signal to Noise Ratio",
            "Self Registration Score",
            "Template Registration Score",
        ]
        ylab = ["Density", "Density", "Density", "Density"]
        xlab = ["Ratio", "Ratio", "Score", "Score"]
        traces = [fig_to_trace(fig) for fig in figs]

        fname_multi = "registration_qa.html"
        # if a dataset name is provided, add it to the name
        if self.dataset is not None:
            fname_multi = "{}_{}".format(self.dataset, fname_multi)
        fname_multi = "{}/{}".format(regdir, fname_multi)
        multi = traces_to_panels(traces, names=names, ylabs=ylab, xlabs=xlab)
        pyo.plot(multi, validate=False, filename=fname_multi)
        pass

    def group_motion(self):
        """
        A function that performs group level motion corrective quality control.
        """
        mcdir = "{}/{}".format(self.outdir, "mc")
        cmd = "mkdir -p {}".format(mcdir)
        mgu.execute_cmd(cmd)

        trans_abs = np.zeros((len(self.qa_objects)))
        trans_rel = np.zeros((len(self.qa_objects)))
        trans_abs_gt = np.zeros((len(self.qa_objects)))
        trans_rel_gt = np.zeros((len(self.qa_objects)))

        FD_mean = [sub.fd_mean for sub in self.qa_objects]
        FD_max = [sub.fd_max for sub in self.qa_objects]
        FD_gt_100um = [sub.fd_gt_100um for sub in self.qa_objects]
        FD_gt_200um = [sub.fd_gt_200um for sub in self.qa_objects]

        fig_mean = plot_rugdensity(FD_mean)
        fig_max = plot_rugdensity(FD_max)
        fig_gt_100um = plot_rugdensity(FD_gt_100um)
        fig_gt_200um = plot_rugdensity(FD_gt_200um)

        figs = [fig_mean, fig_max, fig_gt_100um, fig_gt_200um]
        names = [
            "Average FD KDE",
            "Max FD KDE",
            "Number of FD > 0.1 mm KDE",
            "Number of FD > 0.2 mm KDE",
        ]
        ylab = ["Density", "Density", "Density", "Density"]
        xlab = [
            "Average FD (mm)",
            "Average Motion (mm)",
            "Number of Volumes",
            "Number of Volumes",
        ]
        traces = [fig_to_trace(fig) for fig in figs]

        fname_multi = "motion_correction.html"

        # if a dataset name is provided, add it to the name
        if self.dataset is not None:
            fname_multi = "{}_{}".format(self.dataset, fname_multi)
        fname_multi = "{}/{}".format(mcdir, fname_multi)

        multi = traces_to_panels(traces, names=names, ylabs=ylab, xlabs=xlab)
        pyo.plot(multi, validate=False, filename=fname_multi)
        pass


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author      zhouyu
# created on  2016/3/21

PROPAGATION_REQUIRED        = 1
PROPAGATION_SUPPORTS        = 2
PROPAGATION_MANDATORY       = 3
PROPAGATION_REQUIRES_NEW    = 4
PROPAGATION_NOT_SUPPORTED   = 5
PROPAGATION_NEVER           = 6
#PROPAGATION_NESTED          = 7


"""
.. module:: model_handler
   :synopsis: The model_handler module prepares the model inputs
   to be sent to the backend model server as well as creating a
   Model object consisting of the inputs, outputs, and other
   model-specific attributes such as 'run_type' and 'user' who
   ran the model.  This generic Model object is the communication
   backbone between the frontend and backend / model server.  If
   a model requires specific treatment for frontend-backend
   communication, this module can be bypassed by creating a
   "*_model" module in the model's module directory.
"""

import json
import logging
import os
import pandas as pd
import re

from ..REST import rest_funcs

# Set HTTP header
http_headers = rest_funcs.set_http_headers()
url_part1 = os.environ['UBERTOOL_REST_SERVER']

class Model(object):
    def __init__(self, run_type, jid, pd_obj_in, pd_obj_out):
        """
        Generic Python 'Model' object created from two Pandas
        DataFrame objects, one for inputs and one for outputs.
        """

        self.pd_obj_in = pd_obj_in
        self.pd_obj_out = pd_obj_out
        self.jid = jid
        self.run_type = run_type

        # Set object attributes to model inputs and outputs
        """
        Set each column of the DataFrames as object attribute with
        a value equal to the first row's value
        """
        for col in self.pd_obj_in:
            setattr(self, self.pd_obj_in[col].name, self.pd_obj_in[col].iloc[0])
        for col in self.pd_obj_out:
            setattr(self, self.pd_obj_out[col].name, self.pd_obj_out[col].iloc[0])

class ModelFortran(object):
    def __init__(self, run_type, jid, inputs, outputs):
        """
            Generic Python 'Model' object created from two Pandas
            DataFrame objects, one for inputs and one for outputs.
        """

        self.inputs = inputs
        self.outputs = outputs
        self.jid = jid
        self.run_type = run_type

        # Set object attributes to model inputs and outputs
        """
            Set inputs and outputs as model object attributes
        """
        for k, v in self.inputs:
            setattr(self, k, v)
        for k, v in self.outputs:
            setattr(self, k, v)

def call_model_server(model, args):
    """
        Sends model input parameters (args) to model
        backend server and returns the 'response'.
        'args' can be type == 'dict' or 'string' (valid
        JSON string).
    """

    # If 'args' is a Python dictionary, dump it to a JSON string
    if type(args) == dict:
        data = json.dumps(args)
    else:
        data = args

    # POST JSON to model server through rest proxy
    # REST/rest_funcs
    response = rest_funcs.rest_proxy_post(model, data, rest_funcs.gen_jid())

    return response

def replace_nans(encoded):
    regex = re.compile(r'\bNaN\b')
    return regex.sub('null', encoded)

def create_dataframe(response):
    logging.info("=========== model_handler.create_dataframe")
    # Load 'inputs' key from JSON response to Pandas DataFrame
    logging.info("=========== inputs")
    #deserialize the serialized string objects coming back from the back end
    dict_obj_in = response.json()['inputs']
    json_obj_in = replace_nans(json.dumps(dict_obj_in))
    pd_obj_in = pd.read_json(json_obj_in)
    # Load 'outputs' key from JSON response to Pandas DataFrame
    logging.info("=========== outputs")
    dict_obj_out = response.json()['outputs']
    json_obj_out = replace_nans(json.dumps(dict_obj_out))
    pd_obj_out = pd.read_json(json_obj_out)
    logging.info("=========== dataframes created")
    return pd_obj_in, pd_obj_out

def model_input_post_receiver(request, model):
    """
        Converts the POSTed data from the model's input page form
        to a Python dictionary and passes it to the Model object where
        it is to be converted to JSON and passed to the backend server.
    """

    logging.info("=========== model_handler.model_input_post_receiver")
    args = {"inputs": {}}
    for key in request.POST:
        args["inputs"][key] = {"0": request.POST.get(key)}
    args["run_type"] = "single"
    logging.info("model:")
    logging.info(model)
    logging.info("args:")
    logging.info(args)
    logging.info("=========== model_handler.model_input_post_receiver calling back end")
    response = call_model_server(model, args)
    logging.info("=========== returned from back end")
    if(response.ok):
        logging.info("returned ok, status_code = " + str(response.status_code))
        jid = response.json()['_id']
        logging.info("job id = " + str(jid))
        run_type = response.json()['run_type']
        logging.info("run_type = " + run_type)
        dataframes = create_dataframe(response)
        model_obj = Model(run_type, jid, dataframes[0], dataframes[1])
    else:
        logging.info("back end not ok")
        logging.info(response.text)
        logging.info("status_code = " + str(response.status_code))
        logging.info("raise = " + str(response.raise_for_status()))
    return model_obj

def modelInputPOSTReceiverFortran(request, model):
    """
        Converts the POSTed data from the model's input page form
        to a Python dictionary and passes it to the Model object where
        it is to be converted to JSON and passed to the backend server.

        ==> FORTAN version <==
    """

    args = {"inputs": {}}
    for key in request.POST:
        args["inputs"][key] = request.POST.get(key)
    args["run_type"] = "single"

    response = call_model_server(model, args)

    if model in {'sam'}:
        logging.info(response.json()['outputs'])

        return str(response.json()['outputs'])

    else:

        jid = response.json()['_id']
        run_type = response.json()['run_type']
        # dataframes = create_dataframe(response)

        model_obj = ModelFortran(run_type, jid, args['inputs'], response['inputs'])

        return model_obj

class ModelQAQC(object):
    def __init__(self, run_type, jid, pd_obj_in, pd_obj_out, pd_obj_exp):
        """
            Generic Python 'Model' object created from two Pandas
            DataFrame objects, one for inputs and one for outputs.
        """

        logging.info("=========== model_handler.ModelQAQC")
        self.pd_obj_in = pd_obj_in
        self.pd_obj_out = pd_obj_out
        self.pd_obj_exp = pd_obj_exp
        self.jid = jid
        self.run_type = run_type

        for col in self.pd_obj_in:
            setattr(self, self.pd_obj_in[col].name, self.pd_obj_in[col].iloc[0])
        for col in self.pd_obj_out:
            setattr(self, self.pd_obj_out[col].name, self.pd_obj_out[col].iloc[0])
        for col in self.pd_obj_exp:
            setattr(self, self.pd_obj_exp[col].name, self.pd_obj_exp[col].iloc[0])

def generate_model_object_list(response):
    """
        Loops over the input and output DataFrames creating
        a Python object 'Model' for each model run and appends
        the object to a Python list.  Returns the list of objects.
    """

    logging.info("=========== model_handler.generate_model_object_list")
    ModelList = []

    run_type = response.json()['run_type']
    jid = response.json()['_id']

    # Load 'inputs' key from JSON response to Pandas DataFrame
    pd_obj_in = pd.io.json.read_json(json.dumps(response.json()['inputs']))
    # Load 'outputs' key from JSON response to Pandas DataFrame
    pd_obj_out = pd.io.json.read_json(json.dumps(response.json()['outputs']))

    if run_type == "qaqc":
        pd_obj_exp = pd.io.json.read_json(json.dumps(response.json()['exp_out']))

    no_of_runs = len(pd_obj_out.index)
    logging.info(no_of_runs)

    logging.info(pd_obj_out)

    i = 0
    while (i < no_of_runs):
        logging.info(i)
        run_list = range(no_of_runs)
        del run_list[i]

        logging.info(run_list)

        pd_obj_in_slice = pd_obj_in.drop(run_list)
        pd_obj_out_slice = pd_obj_out.drop(run_list)
        if run_type == "batch":
            model_obj = Model(run_type, jid, pd_obj_in_slice, pd_obj_out_slice)
        elif run_type == "qaqc":
            pd_obj_exp_slice = pd_obj_exp.drop(run_list)
            model_obj = ModelQAQC(run_type, jid, pd_obj_in_slice, pd_obj_out_slice, pd_obj_exp_slice)

        ModelList.append(model_obj)
        i += 1

    return ModelList


from django import forms
from .models import Todo
from friendship.models import Friend
from datetimewidget.widgets import DateTimeWidget
from django.contrib.auth.models import User

class TodoForm(forms.ModelForm):

    class Meta:
        model = Todo
        fields = (
            'title',
            'text',
            'due_date',
            'shared_user',
            'completed',
        )
        widgets = {
            'title': forms.TextInput(attrs={'placeholder': 'Title'}),
            'text': forms.Textarea(attrs={'placeholder': 'Detailed description', 'rows': 4}),
            'due_date': DateTimeWidget(attrs={'id': "yourdatetimeid", 'placeholder': "Due Date"}, usel10n=True, bootstrap_version=3),
            'shared_user': forms.SelectMultiple()
        }


import logistic_regression as lr
import importMNIST
import numpy as np
from sklearn.linear_model import LogisticRegression
from scipy.optimize import minimize
import time
from prettytable import PrettyTable

def lr_model(num_runs):

    deg = [[1,0,0], [1,1,0]]

    mean_acc = []
    median_acc = []
    var_acc = []

    for degrees in deg:
        print (degrees)
        compare_accuracy = []
        for i in range(num_runs):
            train_x, train_y, test_x, test_y= importMNIST.get_MNIST()

            model = lr.logistic(degrees, train_x, train_y, 10, 9)
            model.train(tol=1e-4)

            _, accuracy = model.accuracy(test_x,test_y)

            compare_accuracy.append(accuracy)

        mean_acc.append(np.mean(compare_accuracy))
        median_acc.append(np.mean(compare_accuracy))
        var_acc.append(np.var(compare_accuracy))

    t = PrettyTable(['Model', 'Mean', 'Median', 'Variance'])
    for i in range(len(deg)):
        t.add_row([deg[i], mean_acc[i], median_acc[i], var_acc[i]])
    print (t)

def skl_model(num_runs):

    deg = [[1, 1, 0]]

    mean_acc = []
    median_acc = []
    var_acc = []
    compare_accuracy = []

    for i in range(num_runs):
        print (i)
        train_x, train_y, test_x, test_y= importMNIST.get_MNIST()

        model = LogisticRegression(solver='lbfgs', multi_class='multinomial').fit(train_x,train_y)
        accuracy = model.score(test_x,test_y)
        compare_accuracy.append(accuracy)

    mean_acc.append(np.mean(compare_accuracy))
    median_acc.append(np.mean(compare_accuracy))
    var_acc.append(np.var(compare_accuracy))

    t = PrettyTable(['Model', 'Mean', 'Median', 'Variance'])
    for i in range(len(deg)):
        t.add_row([deg[i], mean_acc[i], median_acc[i], var_acc[i]])
    print (t)

def compare_optimisers():

    compare_optimisers = []
    model_coeffs = []

    train_x, train_y, test_x, test_y= importMNIST.get_MNIST()

    #terms = [constant, linear, quadratic]
    degrees = [1, 1, 0]
    '''
    model = lr.logistic(degrees, train_x, train_y, 10, 9)
    model.train(tol=1e-4)
    _, accuracy = model.accuracy(test_x,test_y)
    compare_optimisers.append(accuracy)
    model_coeffs.append(model.B_mat)
    '''

    model_sk = lr.logistic(degrees, train_x, train_y, 10, 9)

def grad_updates(xvals, model):
    pass

if __name__ == "__main__":

    ##################
    # Compare functions
    ##################
    compare_optimisers()
    #skl_model(5)


#import pandas as pd
#import numpy as np
#import datetime as dt
from keras import backend as K
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM, SimpleRNN
from pandas import Series
from keras.regularizers import l2
import numpy as np

class FNN():
    def __init__(self, units = [5], dropout=None, layers=1, regularizer=None, epochs=1000, batch_size=256, weights=None):
        self.batch_size = batch_size
        self.time_steps = 1
        self.num_features = 1
        self.epochs = epochs
        self.layers = layers
        self.units = units
        self.activation = ["tanh", "linear"]
        self.regularizer = regularizer
        self.dropout = dropout
        self.model = self._create(batch_size=self.batch_size, weights=weights)

    def _create(self, batch_size=1, weights=None):
        model = Sequential()
        for n in range(self.layers):
            model.add(Dense( int(self.units[0]),
                input_shape=(50,),
                activation=self.activation[0],
                kernel_regularizer = l2(0.01) if self.regularizer == "l2" else None,
                ))
        model.add(Dropout(self.dropout))
        model.add(Dense(1, activation=self.activation[-1]))

        if(weights is not None):
            model.set_weights(weights)

        model.compile(loss='mean_squared_error', optimizer='adam')
        return model

    def window(self, x, ws=50):
        x = x.reshape(-1)
        xx=[]
        for n in range(len(x) - ws):
            xx+=[x[n:n+ws]]
        return np.array(xx)

    def train(self, x, y, x_val, y_val):
        x = self.window(x, ws=50)
        y = y.reshape(-1)[50:]
        bs1 = int(x.shape[0]/self.batch_size) * self.batch_size
        x = x[:bs1]
        y = y[:bs1]
        for n in range( self.epochs ):
            if (n%10) is 0:
                print("Training step ", n)
            self.model.fit(x, y, batch_size=self.batch_size, shuffle=False, epochs=1, verbose=0)
            #self.model.reset_states()
        return self

    def _prediction_model(self):
        old_weights = self.model.get_weights()
        return self._create(batch_size=1, weights=old_weights)

    def single_forecast(self, x):
        x = self.window(x, ws=50)
        model = self._prediction_model()
        #model.reset_states()
        return model.predict(x, batch_size=1).reshape(-1)

    def multistep_forecast(self, x, horizon=24):
        x = self.window(x, ws=50)

        predictions={}
        for i in range(horizon):
            predictions[i] = []

        model = self._prediction_model()
        for t_0 in x:
            current_window = t_0
            predictions[0].append(model.predict(np.array([current_window]), batch_size=1)[0][0])
            for t_n in range(1, horizon):
                current_window = np.append( current_window[1:], predictions[t_n-1][-1]  )
                y = model.predict( np.array([current_window]), batch_size=1)
                predictions[t_n].append(y[0][0])
        return predictions

    def forecast(self, x, horizon=6): # SKIP?
        model = self._prediction_model()
        results = []

        model.reset_states()
        #return Series(model.predict(x, batch_size=1).reshape(-1))
        """
        for t_0 in range(1, len(x)+1):
            predictions = []
            model.reset_states()
            last_prediction = model.predict(x[:t_0], batch_size=1)[-1].reshape(1,1,1)

            #predictions.append(last_prediction)
            #for t_n in range(horizon):
            #    last_prediction = model.predict(predictions[-1], batch_size=1 )
            #    predictions.append(last_prediction)
            #results.append(predictions)
        return results
        """


def main():
    ft = open('t.txt','w')
    fo = open('o.txt','w')
    fa = open('a.txt','w')
    ft1 = open('t1.txt','w')
    fo1 = open('o1.txt','w')
    fa1 = open('a1.txt','w')
    fterms = open('terms.txt','r')
    ftermsright = open('10-terms.txt','r')

    #split new
    for line in fterms:
        if 'a-' in line:
            fa1.write(line)
        elif 'o-' in line:
            fo1.write(line)
        elif 't-' in line:
            ft1.write(line)

    #split old
    for line in ftermsright:
        if 'a-' in line:
            fa.write(line)
        elif 'o-' in line:
            fo.write(line)
        elif 't-' in line:
            ft.write(line)
main()


import numpy as np
import matplotlib.pyplot as plt

# Hide axes
# ax.xaxis.set_visible(False)
# ax.yaxis.set_visible(False)

showing = False

def showTable(q):
    global showing
    fig, ax = plt.subplots()
    fig.patch.set_visible(False)
    ax.axis('off')
    labels = [i for i in range(len(q))]
    cols = [i for i in range(len(q[0]))]
    plt.table(cellText=q, rowLabels=labels, colLabels=cols, loc='center')
    plt.pause(0.05)
    if not showing:
        plt.show()
        showing = True

saved = False

def saveTable(q):
    global saved
    if saved:
        return
    f = open('table.csv', 'w')

    for st in q:
        for a in st:
            f.write("%f," % a)
        f.write("\n")

    f.close()
    saved = True


#import urllib.request
#import cv2
#import numpy as np
import os

global infoNr
infoNr = 1

#def create_pos_n_neg():
while True:
    global infoNr
    if infoNr == 17:
        break
    else:
        for img in os.listdir('/home/pi/RPi/trainjohan/info'+str(infoNr)):
            os.system('sudo mv /home/pi/RPi/trainjohan/info'+str(infoNr)+'/'+img+' /home/pi/RPi/trainjohan/info/'+str(infoNr)+img)
        infoNr += 1
#            if file_type == 'pos':
#                line = file_type+'/'+img+' 1 0 0 50 50\n'
#                with open('info.dat','a') as f:
#                   f.write(line)
#            elif file_type == 'neg':
#                line = file_type+'/'+img+'\n'
#                with open('bg.txt','a') as f:
#                    f.write(line)

#create_pos_n_neg()


from ArrayStack import ArrayStack

def is_matched(expression):
    left = "({["
    right = ")}]"
    stack = ArrayStack()
    for i in expression:
        if i in left:
            stack.push(i)
        else:
            if stack.is_empty():
                return False
            elif left.index(stack.pop())!=right.index(i):
                return False
    return True

print(is_matched("()(()){([()])}"))


import logging

from rest_framework.generics import GenericAPIView
from rest_framework.response import Response

from .models import ChineseMetadata, ChineseWord
from .serializers import ChineseWordsSerializer

LOG = logging.getLogger(__name__)

class RecordWords(GenericAPIView):
  serializer_class = ChineseWordsSerializer

  def post(self, request, format=None):
    metadata = ChineseMetadata(**{
      'native': request.POST.get('native') == 'true',
      'gender': request.POST.get('gender')
    })
    metadata.save()

    for word in request.FILES:
      # import ipdb
      # ipdb.set_trace()
      _word = ChineseWord(word=word, wave=request.FILES.get(word).read(), metadata=metadata)
      _word.save()

    return Response(status=204)
record_words = RecordWords.as_view()


import numpy as np
from math import pi, sin, cos, radians
from scipy.interpolate import interp1d
from scipy.signal.windows import hann
import matplotlib.pyplot as plt

from beagle_firmware import BeagleFirmware

# These paramters are expected to be mostly constant, fixed by competition
channel_depth = 5
source_depth = 4.5
fs = 96000
fc = 30000
T = 0.004  # pulse width, ms
c = 1480.  #speed of sound

# signal threshold value. Once this is exceeded we begin beamforming
p2_thresh = .025

# these will change all the time, and can be played with (within reason)
num_channels = 3
channel_coord = np.array([0, c / (2 * fc), c / fc])
receiver_depth = 4.5
receiver_range = 1  # range to channel at 0 coordinate point
receiver_bearing = -30 # bearing, degrees. Between -90 and 90

# add some noise to the signal. We can't anticipate what the tank will sound
# like in the background, so this is a coarse approximation of background
# interference
noise_level = 1e-5  # white noise variance. play around with this number

# each receiver will have a slighly different coordinates based on bearing and
# distance
dx = receiver_range + channel_coord * sin(radians(receiver_bearing))
dy = receiver_range + channel_coord * cos(radians(receiver_bearing))
rr = np.sqrt(dx ** 2 + dy ** 2)

# simulate first 3 in-plane arrivals
r_dir = np.sqrt(rr ** 2 + (receiver_depth - source_depth) ** 2)
r_surf = np.sqrt(rr ** 2 + (receiver_depth + source_depth) ** 2)
r_bottom = np.sqrt(rr ** 2 + (2 * channel_depth - source_depth) ** 2)

# time of arrival
time_dir = r_dir / c
time_surf = r_surf / c
time_bottom = r_bottom / c

# time axis of simulated recorded signal
taxis = np.arange(2 ** 13) / fs
taxis += time_dir[0] - 300 / fs  # small offset for plotting purposes

# transmitted signal
tsig = np.arange(np.ceil(T * fs)) / fs
sig_xmitt = np.sin(2 * pi * fc * tsig)
sig_xmitt *= hann(tsig.size)
# create an interpolator, this allows for sub sample timing of arrivals
sig_ier = interp1d(tsig, sig_xmitt, kind=3, bounds_error=False, fill_value=0.)

# create time series as a sum of all arrivals
x_sig = np.zeros((taxis.size, num_channels), dtype=np.float_)
x_sig += sig_ier(taxis[:, None] - time_dir) / r_dir
x_sig -= sig_ier(taxis[:, None] - time_surf) / r_surf
x_sig -= sig_ier(taxis[:, None] - time_bottom) / r_bottom

# create some noise on all channels
x_sig += np.random.randn(*x_sig.shape) * np.sqrt(noise_level)

nbd = BeagleFirmware(fc)

# compute output on each channel as though it came from the hydrophones
starti = 0
buffer_size = nbd.buffer_size
num_buffer = int(np.floor(x_sig.shape[0] / buffer_size))
buff_i = np.arange(num_buffer) * buffer_size

out_data = []
for i in buff_i:
    p_atfc = nbd.process(x_sig[i: i + buffer_size, :])
    out_data.append(np.concatenate(p_atfc))

out_data = np.concatenate(out_data)

# scale the data to V peak
out_data = np.sqrt(2) * np.sqrt(2 * np.abs(out_data) ** 2 / np.sum(nbd.window) ** 2)
win_time = np.arange(out_data.shape[0]) * nbd.num_step / fs + taxis[0]

fig, ax = plt.subplots()
ax.plot(taxis * 1e3, x_sig)
ax.plot([-1e4, 1e4], [1, 1], color='C3', label='clipping threshold')
ax.plot([-1e4, 1e4], [-1, -1], color='C3')
ax.grid()
ax.legend()
ax.set_xlim(taxis[0] * 1e3, taxis[-1] * 1e3)
ax.set_xlabel('time, (ms)')
ax.set_ylabel('pressure')
ax.set_title('simulated pressure arrival')

fig, ax = plt.subplots()
ax.plot(win_time * 1e3, out_data)
ax.plot([-1e4, 1e4], [p2_thresh, p2_thresh], 'k', label='detection threshold')
ax.plot([-1e4, 1e4], [1, 1], color='C3', label='clipping threshold')
ax.grid()
ax.legend()
ax.set_xlim(win_time[0] * 1e3, win_time[-1] * 1e3)
ax.set_xlabel('time, (ms)')
ax.set_ylabel('$|p|^2$')
ax.set_title('arrival energy at {:d} kHz, time series'.format(int(fc / 1e3)))

plt.show(block=False)


# -*- coding: utf-8 -*-
"""
Created on Thu Jun 11 10:08:29 2015

Module to test and demonstrate the use of execution_gui

@author: albert
"""
from __future__ import absolute_import
import logging
import sys
import yaml
from wx import App
from time import sleep
from threading import Thread
from jetplex import __version__ as jetplex_version
from jetplex import __version__ as jetplex_revision
from jetplex import Fixture
from jetplex import Procedure
from jetplex import Execution
from jetplex import log_yaml_file
from jetplex import log_data
from jetplexwxgui.utils import get_path
from jetplexwxgui.progress_gui import ProgressFrame

# import wx

# from wx import __version__ as wx_version
# logging.info('wx version: %s', wx_version)

# from yaml import __version__ as yaml_version
# logging.info('pyYaml version: %s', yaml_version)

# from autopep8 import __version__ as autopep8_version
# logging.info('autopep8 version: %s', autopep8_version)

try:
    from _version import __version__ as jetplexwxgui_version
except ImportError:
    jetplexwxgui_version = "UNKNOWN"
try:
    from _version import __revision__ as jetplexwxgui_revision
except ImportError:
    jetplexwxgui_revision = "UNKNOWN"

YAML_FILE = get_path('testsample.yaml')
FX_FILE = get_path('fx.yaml')
FUNCS_TXT = """\
main branch:
- branch 1:
  - {get one: 1.0}
  - {get three: self.get_three}
- branch 2:
  - {get three: self.get_three}
  - {root path: 'self.step.path_list()[0]'}
  - {get fixture var: self.abc}
  - branch 3

branch 3:
  - {set var: self.set_var}
  - {get var: self.get_var}
"""
FUNCS = yaml.load(FUNCS_TXT)

class MyProcedure(Procedure):
    """demonstrate sequence"""
    def __init__(self, *args, **kwargs):
        """initialize"""
        super(MyProcedure, self).__init__(*args, **kwargs)
        self.var = None

    def get_three(self):
        """return three"""
        sleep(1)
        return 3.0

class MyExecution(Execution):
    def __init__(self, *args, **kwargs):
        """initialize"""
        super(MyExecution, self).__init__(*args, **kwargs)

    def functions(self):
        return {
            'step 1': (self.my_step, 1.0),
            'step 2': (self.my_step, 2.0),
            'step 3': (self.my_step, 3.0),
            'branch 3': [
                {'set var': (self.set_var, 5.0)},
                {'get var': self.get_var}
                ],
            'main branch': [
                {
                    'branch 1': [
                        {'get one': 1.0},
                        {'get three': self.get_three}
                        ]
                    },
                {
                    'branch 2': [
                        {'get three': self.get_three},
                        {'root path': self.step.path_list()[0]},
                        {'get fixture var': self.abc},
                        ]
                    }
                ]
            }

    def my_step(self, arg):
        sleep(1)
        return arg

class MyFixture(Fixture):
    """demonstrate fixture"""
    def __init__(self, *args, **kwargs):
        """initialize"""
        super(MyFixture, self).__init__(*args, **kwargs)

    def set_var(self, value):
        """set the var, which can be read by get_var"""
        logging.info('set_var @: %s', self.name)
        setattr(self, 'var', value)
        sleep(1)

    def get_var(self):
        """read var"""
        logging.info('get_var @: %s', self.name)
        sleep(1)
        return getattr(self, 'var')

def _main():
    """is the main function to execute when the module is executed"""
    # Setup logging
    logging.basicConfig(level=logging.INFO)
    logging.getLogger().setLevel(logging.INFO)
    # log version info
    logging.info('python version: %s', sys.version)
    logging.info('jetplex version: %s', jetplex_version)
    logging.info('jetplex revision: %s', jetplex_revision)
    logging.info('jetplexwxgui version: %s', jetplexwxgui_version)
    logging.info('jetplexwxgui revision: %s', jetplexwxgui_revision)
    # Log the functions
    log_data(FUNCS)
    # Log the fixture file
    log_yaml_file(FX_FILE)
    # Get fixture instance
    my_fixture = MyFixture(name='my fixture', yaml_file=FX_FILE)
    # get sequence specification
    name = 'pn11full'  # 'main branch'
    my_procedure = MyProcedure(name=name, yaml_file=YAML_FILE)
    # run the test to get sequence instance
    my_execution = MyExecution(my_procedure)
    thread = Thread(
        target=my_execution.run,
        args=(
            my_fixture,
            'myserialnumber',
            'mypartnumber',
            'mypartrevision',
            'mytesttype',
            )
        )
    thread.start()
    app = App(False)
    frame = ProgressFrame(my_execution)
    frame.Show()
#    test_db = TestDatabase()
#    print 'test database size:', len(test_db)
    app.MainLoop()
#    thread.join()
    # yaml_data = my_execution.get_y()
    # log_yaml(yaml_data, 'testinstance')

if __name__ == "__main__":
    _main()