import sys
_module = sys.modules[__name__]
del sys
config = _module
datasets = _module
aircraft_dataset = _module
bird_dataset = _module
car_dataset = _module
dog_dataset = _module
eval = _module
models = _module
blocks = _module
inception = _module
resnet = _module
vgg = _module
wsdan = _module
train = _module
utils = _module

from _paritybench_helpers import _mock_config, patch_functional
from unittest.mock import mock_open, MagicMock
from torch.autograd import Function
from torch.nn import Module
import abc, collections, copy, enum, functools, inspect, itertools, logging, math, matplotlib, numbers, numpy, pandas, queue, random, re, scipy, sklearn, string, tensorflow, time, torch, torchaudio, torchtext, torchvision, types, typing, uuid, warnings
import numpy as np
from torch import Tensor
patch_functional()
open = mock_open()
yaml = logging = sys = argparse = MagicMock()
ArgumentParser = argparse.ArgumentParser
_global_config = args = argv = cfg = config = params = _mock_config()
argparse.ArgumentParser.return_value.parse_args.return_value = _global_config
yaml.load.return_value = _global_config
sys.argv = _global_config
__version__ = '1.0.0'
xrange = range
wraps = functools.wraps

from torch.utils.data import Dataset

from scipy.io import loadmat

import logging

import warnings

import torch

import torch.nn as nn

import torch.nn.functional as F

from torchvision import transforms

from torch.utils.data import DataLoader

import math

import numpy as np

import torch.utils.model_zoo as model_zoo

import time

import random

import torchvision.transforms as transforms

class CBAMLayer(nn.Module):

    def __init__(self, channel, reduction=16, spatial_kernel=7):
        super(CBAMLayer, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.max_pool = nn.AdaptiveMaxPool2d(1)
        self.mlp = nn.Sequential(nn.Conv2d(channel, channel // reduction, 1, bias=False), nn.ReLU(inplace=True), nn.Conv2d(channel // reduction, channel, 1, bias=False))
        self.conv = nn.Conv2d(2, 1, kernel_size=spatial_kernel, padding=spatial_kernel // 2, bias=False)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        max_out = self.mlp(self.max_pool(x))
        avg_out = self.mlp(self.avg_pool(x))
        channel_out = self.sigmoid(max_out + avg_out)
        x = channel_out * x
        max_out, _ = torch.max(x, dim=1, keepdim=True)
        avg_out = torch.mean(x, dim=1, keepdim=True)
        spatial_out = self.sigmoid(self.conv(torch.cat([max_out, avg_out], dim=1)))
        x = spatial_out * x
        return x

class SPPLayer(nn.Module):

    def __init__(self, pool_size, pool=nn.MaxPool2d):
        super(SPPLayer, self).__init__()
        self.pool_size = pool_size
        self.pool = pool
        self.out_length = np.sum(np.array(self.pool_size) ** 2)

    def forward(self, x):
        B, C, H, W = x.size()
        for i in range(len(self.pool_size)):
            h_wid = int(math.ceil(H / self.pool_size[i]))
            w_wid = int(math.ceil(W / self.pool_size[i]))
            h_pad = (h_wid * self.pool_size[i] - H + 1) / 2
            w_pad = (w_wid * self.pool_size[i] - W + 1) / 2
            out = self.pool((h_wid, w_wid), stride=(h_wid, w_wid), padding=(h_pad, w_pad))(x)
            if i == 0:
                spp = out.view(B, -1)
            else:
                spp = torch.cat([spp, out.view(B, -1)], dim=1)
        return spp

class BasicConv2d(nn.Module):

    def __init__(self, in_channels, out_channels, **kwargs):
        super(BasicConv2d, self).__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs)
        self.bn = nn.BatchNorm2d(out_channels, eps=0.001)

    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        return F.relu(x, inplace=True)

class InceptionA(nn.Module):

    def __init__(self, in_channels, pool_features):
        super(InceptionA, self).__init__()
        self.branch1x1 = BasicConv2d(in_channels, 64, kernel_size=1)
        self.branch5x5_1 = BasicConv2d(in_channels, 48, kernel_size=1)
        self.branch5x5_2 = BasicConv2d(48, 64, kernel_size=5, padding=2)
        self.branch3x3dbl_1 = BasicConv2d(in_channels, 64, kernel_size=1)
        self.branch3x3dbl_2 = BasicConv2d(64, 96, kernel_size=3, padding=1)
        self.branch3x3dbl_3 = BasicConv2d(96, 96, kernel_size=3, padding=1)
        self.branch_pool = BasicConv2d(in_channels, pool_features, kernel_size=1)

    def forward(self, x):
        branch1x1 = self.branch1x1(x)
        branch5x5 = self.branch5x5_1(x)
        branch5x5 = self.branch5x5_2(branch5x5)
        branch3x3dbl = self.branch3x3dbl_1(x)
        branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)
        branch3x3dbl = self.branch3x3dbl_3(branch3x3dbl)
        branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)
        branch_pool = self.branch_pool(branch_pool)
        outputs = [branch1x1, branch5x5, branch3x3dbl, branch_pool]
        return torch.cat(outputs, 1)

class InceptionAux(nn.Module):

    def __init__(self, in_channels, num_classes):
        super(InceptionAux, self).__init__()
        self.conv0 = BasicConv2d(in_channels, 128, kernel_size=1)
        self.conv1 = BasicConv2d(128, 768, kernel_size=5)
        self.conv1.stddev = 0.01
        self.fc = nn.Linear(768, num_classes)
        self.fc.stddev = 0.001

    def forward(self, x):
        x = F.avg_pool2d(x, kernel_size=5, stride=3)
        x = self.conv0(x)
        x = self.conv1(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        return x

class InceptionB(nn.Module):

    def __init__(self, in_channels):
        super(InceptionB, self).__init__()
        self.branch3x3 = BasicConv2d(in_channels, 384, kernel_size=3, stride=2)
        self.branch3x3dbl_1 = BasicConv2d(in_channels, 64, kernel_size=1)
        self.branch3x3dbl_2 = BasicConv2d(64, 96, kernel_size=3, padding=1)
        self.branch3x3dbl_3 = BasicConv2d(96, 96, kernel_size=3, stride=2)

    def forward(self, x):
        branch3x3 = self.branch3x3(x)
        branch3x3dbl = self.branch3x3dbl_1(x)
        branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)
        branch3x3dbl = self.branch3x3dbl_3(branch3x3dbl)
        branch_pool = F.max_pool2d(x, kernel_size=3, stride=2)
        outputs = [branch3x3, branch3x3dbl, branch_pool]
        return torch.cat(outputs, 1)

class InceptionC(nn.Module):

    def __init__(self, in_channels, channels_7x7):
        super(InceptionC, self).__init__()
        self.branch1x1 = BasicConv2d(in_channels, 192, kernel_size=1)
        c7 = channels_7x7
        self.branch7x7_1 = BasicConv2d(in_channels, c7, kernel_size=1)
        self.branch7x7_2 = BasicConv2d(c7, c7, kernel_size=(1, 7), padding=(0, 3))
        self.branch7x7_3 = BasicConv2d(c7, 192, kernel_size=(7, 1), padding=(3, 0))
        self.branch7x7dbl_1 = BasicConv2d(in_channels, c7, kernel_size=1)
        self.branch7x7dbl_2 = BasicConv2d(c7, c7, kernel_size=(7, 1), padding=(3, 0))
        self.branch7x7dbl_3 = BasicConv2d(c7, c7, kernel_size=(1, 7), padding=(0, 3))
        self.branch7x7dbl_4 = BasicConv2d(c7, c7, kernel_size=(7, 1), padding=(3, 0))
        self.branch7x7dbl_5 = BasicConv2d(c7, 192, kernel_size=(1, 7), padding=(0, 3))
        self.branch_pool = BasicConv2d(in_channels, 192, kernel_size=1)

    def forward(self, x):
        branch1x1 = self.branch1x1(x)
        branch7x7 = self.branch7x7_1(x)
        branch7x7 = self.branch7x7_2(branch7x7)
        branch7x7 = self.branch7x7_3(branch7x7)
        branch7x7dbl = self.branch7x7dbl_1(x)
        branch7x7dbl = self.branch7x7dbl_2(branch7x7dbl)
        branch7x7dbl = self.branch7x7dbl_3(branch7x7dbl)
        branch7x7dbl = self.branch7x7dbl_4(branch7x7dbl)
        branch7x7dbl = self.branch7x7dbl_5(branch7x7dbl)
        branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)
        branch_pool = self.branch_pool(branch_pool)
        outputs = [branch1x1, branch7x7, branch7x7dbl, branch_pool]
        return torch.cat(outputs, 1)

class InceptionD(nn.Module):

    def __init__(self, in_channels):
        super(InceptionD, self).__init__()
        self.branch3x3_1 = BasicConv2d(in_channels, 192, kernel_size=1)
        self.branch3x3_2 = BasicConv2d(192, 320, kernel_size=3, stride=2)
        self.branch7x7x3_1 = BasicConv2d(in_channels, 192, kernel_size=1)
        self.branch7x7x3_2 = BasicConv2d(192, 192, kernel_size=(1, 7), padding=(0, 3))
        self.branch7x7x3_3 = BasicConv2d(192, 192, kernel_size=(7, 1), padding=(3, 0))
        self.branch7x7x3_4 = BasicConv2d(192, 192, kernel_size=3, stride=2)

    def forward(self, x):
        branch3x3 = self.branch3x3_1(x)
        branch3x3 = self.branch3x3_2(branch3x3)
        branch7x7x3 = self.branch7x7x3_1(x)
        branch7x7x3 = self.branch7x7x3_2(branch7x7x3)
        branch7x7x3 = self.branch7x7x3_3(branch7x7x3)
        branch7x7x3 = self.branch7x7x3_4(branch7x7x3)
        branch_pool = F.max_pool2d(x, kernel_size=3, stride=2)
        outputs = [branch3x3, branch7x7x3, branch_pool]
        return torch.cat(outputs, 1)

class InceptionE(nn.Module):

    def __init__(self, in_channels):
        super(InceptionE, self).__init__()
        self.branch1x1 = BasicConv2d(in_channels, 320, kernel_size=1)
        self.branch3x3_1 = BasicConv2d(in_channels, 384, kernel_size=1)
        self.branch3x3_2a = BasicConv2d(384, 384, kernel_size=(1, 3), padding=(0, 1))
        self.branch3x3_2b = BasicConv2d(384, 384, kernel_size=(3, 1), padding=(1, 0))
        self.branch3x3dbl_1 = BasicConv2d(in_channels, 448, kernel_size=1)
        self.branch3x3dbl_2 = BasicConv2d(448, 384, kernel_size=3, padding=1)
        self.branch3x3dbl_3a = BasicConv2d(384, 384, kernel_size=(1, 3), padding=(0, 1))
        self.branch3x3dbl_3b = BasicConv2d(384, 384, kernel_size=(3, 1), padding=(1, 0))
        self.branch_pool = BasicConv2d(in_channels, 192, kernel_size=1)

    def forward(self, x):
        branch1x1 = self.branch1x1(x)
        branch3x3 = self.branch3x3_1(x)
        branch3x3 = [self.branch3x3_2a(branch3x3), self.branch3x3_2b(branch3x3)]
        branch3x3 = torch.cat(branch3x3, 1)
        branch3x3dbl = self.branch3x3dbl_1(x)
        branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)
        branch3x3dbl = [self.branch3x3dbl_3a(branch3x3dbl), self.branch3x3dbl_3b(branch3x3dbl)]
        branch3x3dbl = torch.cat(branch3x3dbl, 1)
        branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)
        branch_pool = self.branch_pool(branch_pool)
        outputs = [branch1x1, branch3x3, branch3x3dbl, branch_pool]
        return torch.cat(outputs, 1)

class Inception3(nn.Module):

    def __init__(self, num_classes=1000, aux_logits=True, transform_input=False):
        super(Inception3, self).__init__()
        self.aux_logits = aux_logits
        self.transform_input = transform_input
        self.Conv2d_1a_3x3 = BasicConv2d(3, 32, kernel_size=3, stride=2)
        self.Conv2d_2a_3x3 = BasicConv2d(32, 32, kernel_size=3)
        self.Conv2d_2b_3x3 = BasicConv2d(32, 64, kernel_size=3, padding=1)
        self.Conv2d_3b_1x1 = BasicConv2d(64, 80, kernel_size=1)
        self.Conv2d_4a_3x3 = BasicConv2d(80, 192, kernel_size=3)
        self.Mixed_5b = InceptionA(192, pool_features=32)
        self.Mixed_5c = InceptionA(256, pool_features=64)
        self.Mixed_5d = InceptionA(288, pool_features=64)
        self.Mixed_6a = InceptionB(288)
        self.Mixed_6b = InceptionC(768, channels_7x7=128)
        self.Mixed_6c = InceptionC(768, channels_7x7=160)
        self.Mixed_6d = InceptionC(768, channels_7x7=160)
        self.Mixed_6e = InceptionC(768, channels_7x7=192)
        if aux_logits:
            self.AuxLogits = InceptionAux(768, num_classes)
        self.Mixed_7a = InceptionD(768)
        self.Mixed_7b = InceptionE(1280)
        self.Mixed_7c = InceptionE(2048)
        self.fc = nn.Linear(2048, num_classes)
        for m in self.modules():
            if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
                import scipy.stats as stats
                stddev = m.stddev if hasattr(m, 'stddev') else 0.1
                X = stats.truncnorm(-2, 2, scale=stddev)
                values = torch.Tensor(X.rvs(m.weight.data.numel()))
                values = values.view(m.weight.data.size())
                m.weight.data.copy_(values)
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def forward(self, x):
        if self.transform_input:
            x = x.clone()
            x[:, (0)] = x[:, (0)] * (0.229 / 0.5) + (0.485 - 0.5) / 0.5
            x[:, (1)] = x[:, (1)] * (0.224 / 0.5) + (0.456 - 0.5) / 0.5
            x[:, (2)] = x[:, (2)] * (0.225 / 0.5) + (0.406 - 0.5) / 0.5
        x = self.Conv2d_1a_3x3(x)
        x = self.Conv2d_2a_3x3(x)
        x = self.Conv2d_2b_3x3(x)
        x = F.max_pool2d(x, kernel_size=3, stride=2)
        x = self.Conv2d_3b_1x1(x)
        x = self.Conv2d_4a_3x3(x)
        x = F.max_pool2d(x, kernel_size=3, stride=2)
        x = self.Mixed_5b(x)
        x = self.Mixed_5c(x)
        x = self.Mixed_5d(x)
        x = self.Mixed_6a(x)
        x = self.Mixed_6b(x)
        x = self.Mixed_6c(x)
        x = self.Mixed_6d(x)
        x = self.Mixed_6e(x)
        if self.training and self.aux_logits:
            aux = self.AuxLogits(x)
        x = self.Mixed_7a(x)
        x = self.Mixed_7b(x)
        x = self.Mixed_7c(x)
        x = F.avg_pool2d(x, kernel_size=8)
        x = F.dropout(x, training=self.training)
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        if self.training and self.aux_logits:
            return x, aux
        return x

    def get_features_mixed_6e(self):
        return nn.Sequential(self.Conv2d_1a_3x3, self.Conv2d_2a_3x3, self.Conv2d_2b_3x3, nn.MaxPool2d(kernel_size=3, stride=2), self.Conv2d_3b_1x1, self.Conv2d_4a_3x3, nn.MaxPool2d(kernel_size=3, stride=2), self.Mixed_5b, self.Mixed_5c, self.Mixed_5d, self.Mixed_6a, self.Mixed_6b, self.Mixed_6c, self.Mixed_6d, self.Mixed_6e)

    def get_features_mixed_7c(self):
        return nn.Sequential(self.Conv2d_1a_3x3, self.Conv2d_2a_3x3, self.Conv2d_2b_3x3, nn.MaxPool2d(kernel_size=3, stride=2), self.Conv2d_3b_1x1, self.Conv2d_4a_3x3, nn.MaxPool2d(kernel_size=3, stride=2), self.Mixed_5b, self.Mixed_5c, self.Mixed_5d, self.Mixed_6a, self.Mixed_6b, self.Mixed_6c, self.Mixed_6d, self.Mixed_6e, self.Mixed_7a, self.Mixed_7b, self.Mixed_7c)

    def load_state_dict(self, state_dict, strict=True):
        model_dict = self.state_dict()
        pretrained_dict = {k: v for k, v in state_dict.items() if k in model_dict and model_dict[k].size() == v.size()}
        if len(pretrained_dict) == len(state_dict):
            logging.info('%s: All params loaded' % type(self).__name__)
        else:
            logging.info('%s: Some params were not loaded:' % type(self).__name__)
            not_loaded_keys = [k for k in state_dict.keys() if k not in pretrained_dict.keys()]
            logging.info(('%s, ' * (len(not_loaded_keys) - 1) + '%s') % tuple(not_loaded_keys))
        model_dict.update(pretrained_dict)
        super(Inception3, self).load_state_dict(model_dict)

class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, cbam=None, downsample=None):
        super(BasicBlock, self).__init__()
        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=3, padding=1, stride=stride)
        self.bn1 = nn.BatchNorm2d(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, padding=1, stride=1)
        self.bn2 = nn.BatchNorm2d(planes)
        self.downsample = downsample
        self.stride = stride
        if cbam is not None:
            self.cbam = CBAMLayer(planes)
        else:
            self.cbam = None

    def forward(self, x):
        residual = x
        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)
        out = self.conv2(out)
        out = self.bn2(out)
        if self.cbam is not None:
            out = self.cbam(out)
        if self.downsample is not None:
            residual = self.downsample(x)
        out += residual
        out = self.relu(out)
        return out

class Bottleneck(nn.Module):
    expansion = 4

    def __init__(self, inplanes, planes, stride=1, cbam=None, downsample=None):
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(planes)
        self.conv3 = nn.Conv2d(planes, planes * Bottleneck.expansion, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(planes * Bottleneck.expansion)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride
        if cbam is not None:
            self.cbam = CBAMLayer(planes * Bottleneck.expansion)
        else:
            self.cbam = None

    def forward(self, x):
        residual = x
        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)
        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)
        out = self.conv3(out)
        out = self.bn3(out)
        if self.cbam is not None:
            out = self.cbam(out)
        if self.downsample is not None:
            residual = self.downsample(x)
        out += residual
        out = self.relu(out)
        return out

class ResNet(nn.Module):

    def __init__(self, block, layers, cbam=None, num_classes=1000):
        self.inplanes = 64
        super(ResNet, self).__init__()
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0], cbam)
        self.layer2 = self._make_layer(block, 128, layers[1], cbam, stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], cbam, stride=2)
        self.layer4 = self._make_layer(block, 512, layers[3], cbam, stride=2)
        self.avgpool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Linear(512 * block.expansion, num_classes)
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2.0 / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def _make_layer(self, block, planes, blocks, cbam=None, stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(planes * block.expansion))
        layers = []
        layers.append(block(self.inplanes, planes, stride=stride, cbam=cbam, downsample=downsample))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes, cbam=cbam))
        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        return x

    def get_features(self):
        return nn.Sequential(self.conv1, self.bn1, self.relu, self.maxpool, self.layer1, self.layer2, self.layer3, self.layer4)

    def load_state_dict(self, state_dict, strict=True):
        model_dict = self.state_dict()
        pretrained_dict = {k: v for k, v in state_dict.items() if k in model_dict and model_dict[k].size() == v.size()}
        if len(pretrained_dict) == len(state_dict):
            logging.info('%s: All params loaded' % type(self).__name__)
        else:
            logging.info('%s: Some params were not loaded:' % type(self).__name__)
            not_loaded_keys = [k for k in state_dict.keys() if k not in pretrained_dict.keys()]
            logging.info(('%s, ' * (len(not_loaded_keys) - 1) + '%s') % tuple(not_loaded_keys))
        model_dict.update(pretrained_dict)
        super(ResNet, self).load_state_dict(model_dict)

class VGG(nn.Module):

    def __init__(self, features, num_classes=1000, init_weights=True):
        super(VGG, self).__init__()
        self.features = features
        self.spp = SPPLayer(pool_size=[1, 2, 4], pool=nn.MaxPool2d)
        self.fc = nn.Sequential(nn.Linear(512 * self.spp.out_length, 1024), nn.ReLU(True), nn.Dropout(), nn.Linear(1024, num_classes))
        if init_weights:
            self._initialize_weights()

    def forward(self, x):
        x = self.features(x)
        x = self.spp(x)
        x = self.fc(x)
        return x

    def _initialize_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2.0 / n))
                if m.bias is not None:
                    m.bias.data.zero_()
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
            elif isinstance(m, nn.Linear):
                m.weight.data.normal_(0, 0.01)
                m.bias.data.zero_()

    def get_features(self):
        return self.features

    def load_state_dict(self, state_dict, strict=True):
        model_dict = self.state_dict()
        pretrained_dict = {k: v for k, v in state_dict.items() if k in model_dict and model_dict[k].size() == v.size()}
        if len(pretrained_dict) == len(state_dict):
            logging.info('%s: All params loaded' % type(self).__name__)
        else:
            logging.info('%s: Some params were not loaded:' % type(self).__name__)
            not_loaded_keys = [k for k in state_dict.keys() if k not in pretrained_dict.keys()]
            logging.info(('%s, ' * (len(not_loaded_keys) - 1) + '%s') % tuple(not_loaded_keys))
        model_dict.update(pretrained_dict)
        super(VGG, self).load_state_dict(model_dict)

EPSILON = 1e-12

class BAP(nn.Module):

    def __init__(self, pool='GAP'):
        super(BAP, self).__init__()
        assert pool in ['GAP', 'GMP']
        if pool == 'GAP':
            self.pool = None
        else:
            self.pool = nn.AdaptiveMaxPool2d(1)

    def forward(self, features, attentions):
        B, C, H, W = features.size()
        _, M, AH, AW = attentions.size()
        if AH != H or AW != W:
            attentions = F.upsample_bilinear(attentions, size=(H, W))
        if self.pool is None:
            feature_matrix = (torch.einsum('imjk,injk->imn', (attentions, features)) / float(H * W)).view(B, -1)
        else:
            feature_matrix = []
            for i in range(M):
                AiF = self.pool(features * attentions[:, i:i + 1, (...)]).view(B, -1)
                feature_matrix.append(AiF)
            feature_matrix = torch.cat(feature_matrix, dim=1)
        feature_matrix = torch.sign(feature_matrix) * torch.sqrt(torch.abs(feature_matrix) + EPSILON)
        feature_matrix = F.normalize(feature_matrix, dim=-1)
        return feature_matrix

model_urls = {'vgg19': 'https://download.pytorch.org/models/vgg19-dcbb9e9d.pth', 'vgg19_bn': 'https://download.pytorch.org/models/vgg19_bn-c79401a0.pth'}

def inception_v3(pretrained=False, **kwargs):
    """Inception v3 model architecture from
    `"Rethinking the Inception Architecture for Computer Vision" <http://arxiv.org/abs/1512.00567>`_.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    if pretrained:
        if 'transform_input' not in kwargs:
            kwargs['transform_input'] = True
        model = Inception3(**kwargs)
        model.load_state_dict(model_zoo.load_url(model_urls['inception_v3_google']))
        return model
    return Inception3(**kwargs)

class WSDAN(nn.Module):

    def __init__(self, num_classes, M=32, net='inception_mixed_6e', pretrained=False):
        super(WSDAN, self).__init__()
        self.num_classes = num_classes
        self.M = M
        self.net = net
        if 'inception' in net:
            if net == 'inception_mixed_6e':
                self.features = inception_v3(pretrained=pretrained).get_features_mixed_6e()
                self.num_features = 768
            elif net == 'inception_mixed_7c':
                self.features = inception_v3(pretrained=pretrained).get_features_mixed_7c()
                self.num_features = 2048
            else:
                raise ValueError('Unsupported net: %s' % net)
        elif 'vgg' in net:
            self.features = getattr(vgg, net)(pretrained=pretrained).get_features()
            self.num_features = 512
        elif 'resnet' in net:
            self.features = getattr(resnet, net)(pretrained=pretrained).get_features()
            self.num_features = 512 * self.features[-1][-1].expansion
        else:
            raise ValueError('Unsupported net: %s' % net)
        self.attentions = BasicConv2d(self.num_features, self.M, kernel_size=1)
        self.bap = BAP(pool='GAP')
        self.fc = nn.Linear(self.M * self.num_features, self.num_classes, bias=False)
        logging.info('WSDAN: using {} as feature extractor, num_classes: {}, num_attentions: {}'.format(net, self.num_classes, self.M))

    def forward(self, x):
        batch_size = x.size(0)
        feature_maps = self.features(x)
        if self.net != 'inception_mixed_7c':
            attention_maps = self.attentions(feature_maps)
        else:
            attention_maps = feature_maps[:, :self.M, (...)]
        feature_matrix = self.bap(feature_maps, attention_maps)
        p = self.fc(feature_matrix * 100.0)
        if self.training:
            attention_map = []
            for i in range(batch_size):
                attention_weights = torch.sqrt(attention_maps[i].sum(dim=(1, 2)).detach() + EPSILON)
                attention_weights = F.normalize(attention_weights, p=1, dim=0)
                k_index = np.random.choice(self.M, 2, p=attention_weights.cpu().numpy())
                attention_map.append(attention_maps[i, k_index, ...])
            attention_map = torch.stack(attention_map)
        else:
            attention_map = torch.mean(attention_maps, dim=1, keepdim=True)
        return p, feature_matrix, attention_map

    def load_state_dict(self, state_dict, strict=True):
        model_dict = self.state_dict()
        pretrained_dict = {k: v for k, v in state_dict.items() if k in model_dict and model_dict[k].size() == v.size()}
        if len(pretrained_dict) == len(state_dict):
            logging.info('%s: All params loaded' % type(self).__name__)
        else:
            logging.info('%s: Some params were not loaded:' % type(self).__name__)
            not_loaded_keys = [k for k in state_dict.keys() if k not in pretrained_dict.keys()]
            logging.info(('%s, ' * (len(not_loaded_keys) - 1) + '%s') % tuple(not_loaded_keys))
        model_dict.update(pretrained_dict)
        super(WSDAN, self).load_state_dict(model_dict)

class CenterLoss(nn.Module):

    def __init__(self):
        super(CenterLoss, self).__init__()
        self.l2_loss = nn.MSELoss(reduction='sum')

    def forward(self, outputs, targets):
        return self.l2_loss(outputs, targets) / outputs.size(0)

import torch
from torch.nn import MSELoss, ReLU
from _paritybench_helpers import _mock_config, _mock_layer, _paritybench_base, _fails_compile

TESTCASES = [
    # (nn.Module, init_args, forward_args, jit_compiles)
    (BAP,
     lambda: ([], {}),
     lambda: ([torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])], {}),
     False),
    (BasicBlock,
     lambda: ([], {'inplanes': 4, 'planes': 4}),
     lambda: ([torch.rand([4, 4, 4, 4])], {}),
     True),
    (BasicConv2d,
     lambda: ([], {'in_channels': 4, 'out_channels': 4, 'kernel_size': 4}),
     lambda: ([torch.rand([4, 4, 4, 4])], {}),
     True),
    (CBAMLayer,
     lambda: ([], {'channel': 18}),
     lambda: ([torch.rand([4, 18, 4, 4])], {}),
     True),
    (CenterLoss,
     lambda: ([], {}),
     lambda: ([torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])], {}),
     True),
    (Inception3,
     lambda: ([], {}),
     lambda: ([torch.rand([4, 3, 512, 512])], {}),
     False),
    (InceptionA,
     lambda: ([], {'in_channels': 4, 'pool_features': 4}),
     lambda: ([torch.rand([4, 4, 4, 4])], {}),
     True),
    (InceptionB,
     lambda: ([], {'in_channels': 4}),
     lambda: ([torch.rand([4, 4, 4, 4])], {}),
     True),
    (InceptionC,
     lambda: ([], {'in_channels': 4, 'channels_7x7': 4}),
     lambda: ([torch.rand([4, 4, 4, 4])], {}),
     True),
    (InceptionD,
     lambda: ([], {'in_channels': 4}),
     lambda: ([torch.rand([4, 4, 4, 4])], {}),
     True),
    (InceptionE,
     lambda: ([], {'in_channels': 4}),
     lambda: ([torch.rand([4, 4, 4, 4])], {}),
     True),
    (WSDAN,
     lambda: ([], {'num_classes': 4}),
     lambda: ([torch.rand([4, 3, 64, 64])], {}),
     False),
]

class Test_GuYuc_WS_DAN_PyTorch(_paritybench_base):
    def test_000(self):
        self._check(*TESTCASES[0])

    def test_001(self):
        self._check(*TESTCASES[1])

    def test_002(self):
        self._check(*TESTCASES[2])

    def test_003(self):
        self._check(*TESTCASES[3])

    def test_004(self):
        self._check(*TESTCASES[4])

    def test_005(self):
        self._check(*TESTCASES[5])

    def test_006(self):
        self._check(*TESTCASES[6])

    def test_007(self):
        self._check(*TESTCASES[7])

    def test_008(self):
        self._check(*TESTCASES[8])

    def test_009(self):
        self._check(*TESTCASES[9])

    def test_010(self):
        self._check(*TESTCASES[10])

    def test_011(self):
        self._check(*TESTCASES[11])


import math

def findFunctionalValue(x):
    return math.sin(x)

def findIntegrallBySimpsonMethod(a, b, n):
    h = abs(b - a) / n
    res = 0
    for i in range (0, math.trunc(n/2)):
        res += (findFunctionalValue(a) + findFunctionalValue(a + 2 * h) + 4 * findFunctionalValue(h + a)) * h / 3
        a += 2 * h
    return res

def findIntegrallByTrapezoidMethod(a, b, n):
    h = abs(b - a) / n
    res = 0
    for i in range (0, n):
        res += (findFunctionalValue(a) + findFunctionalValue(a + h)) / 2 * h
        a += h
    return res

if __name__ == '__main__':

    sh100 = findIntegrallByTrapezoidMethod(0, math.pi / 2, 100)
    sh200 = findIntegrallByTrapezoidMethod(0, math.pi / 2, 200)

    sh1000 = findIntegrallByTrapezoidMethod(0, math.pi / 2, 1000)
    sh2000 = findIntegrallByTrapezoidMethod(0, math.pi / 2, 2000)

    sh1100 = findIntegrallBySimpsonMethod(0, math.pi / 2, 50)
    sh2200 = findIntegrallBySimpsonMethod(0, math.pi / 2, 100)

    sh11000 = findIntegrallBySimpsonMethod(0, math.pi / 2, 1000)
    sh22000 = findIntegrallBySimpsonMethod(0, math.pi / 2, 2000)

    sh1200 = findIntegrallBySimpsonMethod(0, math.pi / 2, 200)
    sh2400 = findIntegrallBySimpsonMethod(0, math.pi / 2, 400)

    print("sh100 = ", sh100)
    print("sh200 = ", sh200)
    print("sh1000 = ", sh1000)
    print("sh2000 = ", sh2000)

    print("sh1100 = ", sh1100)
    print("sh2200 = ", sh2200)
    print("sh11000 = ", sh11000)
    print("sh22000 = ", sh22000)

    print("sh1200 = ", sh1200)
    print("sh2400 = ", sh2400)

    print("k_trapezoid1, h = 2pi/50, 2pi/100 =", math.log2(abs(sh100 - 1) / abs(sh200 - 1)))
    print("k_trapezoid2, h = 2pi/100, 2pi/200 =", math.log2(abs(sh1000 - 1) / abs(sh2000 - 1)))

    print("k_simpson1, h = 2pi/50, 2pi/100 =", math.log2(abs(sh1100 - 1) / abs(sh2200 - 1)))

    print("k_simpson2, h = 2pi/500, 2pi/10ы00 =", math.log2(abs(sh11000 - 1) / abs(sh22000 - 1)))

    print("k_simpson3, h = 2pi/200, 2pi/400 =", math.log2(abs(sh1200 - 1) / abs(sh2400 - 1)))


import pygame
import sys
from bullet import Bullet
from alien import Alien

def get_number_alien_x(ai_settings, alien_width):
    available_space_x = ai_settings.screen_width - 2 * alien_width
    number_alien_x = int(available_space_x / (2 * alien_width))
    return number_alien_x

def get_number_alien_y(ai_settings, alien_height, ship_height):
    available_space_y = ai_settings.screen_height - 2 * alien_height - ship_height
    rows_number = int(available_space_y / alien_height)
    return rows_number

def creat_alien(ai_settings, screen, aliens, alien_number, rows_number):
    alien = Alien(ai_settings, screen)
    alien_width = alien.rect.width
    alien_height = alien.rect.height
    alien.x = alien_width + 2 * alien_width * alien_number
    alien.y = alien_height + 2 * alien_height * rows_number
    alien.rect.x = alien.x
    alien.rect.y = alien.y
    aliens.add(alien)

def creat_fleets(ai_settings, ship, aliens, screen):
    alien = Alien(ai_settings, screen)
    number_alien_x = get_number_alien_x(ai_settings, alien.rect.width)
    rows_number = get_number_alien_y(ai_settings, alien_height=alien.rect.height, ship_height=ship.rect.height)

    for row_number in range(rows_number):
        for alien_number in range(number_alien_x):
            creat_alien(ai_settings, screen, aliens, alien_number, row_number)

def check_keydown_events(event, ai_settings, screen, ship, bullets):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = True
    elif event.key == pygame.K_LEFT:
        ship.moving_left = True
    elif event.key == pygame.K_SPACE:
        fire_bullet(bullets, ai_settings, screen, ship)
    elif event.key == pygame.K_q:
        sys.exit()

def fire_bullet(bullets, ai_settings, screen, ship):
    if len(bullets) < ai_settings.bullet_allowed:
        new_bullets = Bullet(ai_settings, screen, ship)
        bullets.add(new_bullets)

def check_keyup_events(event, ship):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False
    elif event.key == pygame.K_LEFT:
        ship.moving_left = False

def check_events(ai_settings, screen, ship, bullets):
    # 监视键盘和鼠标事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, screen, ship, bullets)
        elif event.type == pygame.KEYUP:
            check_keyup_events(event, ship)

def update_screen(ai_settings, screen, ship, aliens, bullets):
    # 每次循环都重新绘制屏幕
    screen.fill(ai_settings.bg_color)

    for bullet in bullets.sprites():
        bullet.draw_bullet()

    ship.blitme()
    aliens.draw(screen)

    # 让最近绘制的屏幕可见
    pygame.display.flip()

def update_bullets(bullets):
    bullets.update()

    for bullet in bullets.copy():
        if bullet.rect.y < 0:
            bullets.remove(bullet)


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
from collections import defaultdict

class Solution:
    def widthOfBinaryTree(self, root: Optional[TreeNode]) -> int:

        if not root:
            return 0

        depth_min = defaultdict(lambda: float('inf'))
        depth_max = defaultdict(lambda: float('-inf'))

        # needs to be self. otherwise will have reference problem
        self.max_width = float('-inf')

        def helper(root, depth, idx):
            if not root:
                return

            depth_min[depth] = min(depth_min[depth], idx)
            depth_max[depth] = max(depth_max[depth], idx)

            self.max_width = max(self.max_width, depth_max[depth] - depth_min[depth])

            helper(root.left, depth + 1, idx * 2 - 1)
            helper(root.right, depth + 1, idx * 2)

        helper(root, 0, 1)
        return int(self.max_width) + 1


import pygame
from GameMechanics.GameSound import load_sound
from GameMechanics.GameSound import play

class Missile:

    def __init__(self, __type__, __from__, __to__, __gap__ = (0,0)):

        from_x = __from__[0] + __gap__[0]
        from_y = __from__[1] + __gap__[1]
        to_x = __to__[0] * 32 + __gap__[0]
        to_y = __to__[1] * 32 + __gap__[1]
        self.speed = [ 0 , 0 ]

        if from_x > to_x:
            self.direction = "left"
            self.speed[0] = -5
        if from_x < to_x:
            self.direction = "right"
            self.speed[0] = 5
        if from_y > to_y:
            self.direction = "up"
            self.speed[1] = -5
        if from_y < to_y:
            self.direction = "down"
            self.speed[1] =  5

        self.image = pygame.image.load("images/" + __type__ + "/" + self.direction + ".png")
        self.missile = self.image.get_rect().move([from_x, from_y])
        self.target = pygame.Rect( to_x, to_y, 32, 32)
        self.battle_sound = load_sound(__type__)

    def show_animation(self):
        screen = pygame.display.get_surface()
        window = screen.copy()
        play(self.battle_sound, True)
        pygame.time.delay(500)

        while not self.missile.colliderect(self.target):
            self.missile = self.missile.move(self.speed)
            screen.blit(window,(0,0))
            screen.blit(self.image, self.missile)
            pygame.time.delay(50)
            pygame.display.flip()


# exercise 33 of practicepython.org
print("Welcome to the birthday dictionary. We know the birthdays of :  ");

print(" 1. Geo Mukkath\n 2. Roger Fedrer \n 3. Ralph Lauren\n 4. Mark Cuban");

a = ["Geo Mukkath", "08/10/1996"];
b = ["Roger Fedrer", "11/07/1982"];
c = ["Ralph Lauren", "27/12/1965"];
d = ["Mark Cuban", "17/02/1979"];

name = input("Who's birthday do you want to look up ?\n");
if name == a[0]:
 	print("%s's birthday is on %s" %(a[0],a[1]));
elif name  == b[0]:
 	print("%s's birthday is on %s" %(b[0],b[1]));
elif name == c[0]:
	print("%s's birthday is on %s" %(c[0],c[1]));
elif name == d[0]:
	print("%s's birthday is on %s" %(d[0],d[1]));


"""
1541 잃어버린 괄호
+,-,0~9 길이 최대 50인 식
()를 넣어 식의 값을 최소로 만들어라
"""
#-가 나오기 전에 최대한 덧셈을 뭉친다면 최소이지 않을까?
import sys
result = 0
first = 0 #맨 앞 수를 저장할 변수
num = sys.stdin.readline().split('-') #마이너스 기준으로 자름
for i in range(len(num)):
    arr = list(num[i].split('+')) #플러스로 나눈 수를 모두 합산
    cnt = 0
    for j in arr:
        cnt += int(j) #합산하는 코드
        if i == 0:
            first = cnt
    result -= cnt
print(result+(first*2))

그리디/1541 잃어버린 괄호.py


"""
End-to-end test: Fetches data, creates output, stores it in /tmp and checks if output
is valid.
"""

from pathlib import Path
import os

import geojson

from blockutils.common import setup_test_directories

if __name__ == "__main__":
    TESTNAME = "e2e_a-processing-block"
    TEST_DIR = Path("/tmp") / TESTNAME
    INPUT_DIR = TEST_DIR / "input"
    OUTPUT_DIR = TEST_DIR / "output"
    setup_test_directories(TEST_DIR)

    os.system(
        "gsutil cp -r gs://floss-blocks-e2e-testing/e2e_sharpening/sentinel2_rgb/input/ %s"
        % TEST_DIR
    )

    RUN_CMD = (
        """docker run -v %s:/tmp \
                 -e 'UP42_TASK_PARAMETERS={"exponent": 3}' \
                  -it a-processing-block"""
        % TEST_DIR
    )

    os.system(RUN_CMD)

    # Print out bbox of one tile
    GEOJSON_PATH = OUTPUT_DIR / "data.json"

    with open(str(GEOJSON_PATH)) as f:
        FEATURE_COLLECTION = geojson.load(f)

    out_path = FEATURE_COLLECTION.features[0]["properties"]["up42.data_path"]
    print(out_path)

    print("e2e test successful")


from django.urls import path
from carts.views import cart_page, cart_update, check_out, success

app_name="carts"
urlpatterns = [
    path('', cart_page, name="cart"),
    path('update', cart_update, name="update"),
    path('checkout', check_out, name="checkout"),
    path('success', success, name="success"),
 ]


import os
import sys
import json
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.models as models
import numpy as np
import albumentations as A
from PIL import Image
from torchsummary import summary
from albumentations.pytorch import ToTensorV2
from tqdm import tqdm
from utils.classification_utils import (
    load_checkpoint,
    save_checkpoint,
    get_loaders,
    check_metrics,
    save_predictions_to_folder
)

from torch.utils.tensorboard import SummaryWriter
from utils.earlyStopping import EarlyStopping

sys.path.append('../models')
from custom_mobilenet_v3_small import customMNSmall
sys.path.remove('../models')

with open(sys.argv[1]) as json_data:
    model_config = json.load(json_data)

with open(sys.argv[2]) as json_data:
    results_config = json.load(json_data)

MODEL = model_config["MODEL"]
LEARNING_RATE = model_config["LEARNING_RATE"]
DEVICE = model_config["DEVICE"]
BATCH_SIZE = model_config["BATCH_SIZE"]
NUM_EPOCHS = model_config["NUM_EPOCHS"]
PATIENCE  = model_config["PATIENCE"]
NUM_WORKERS = model_config["NUM_WORKERS"]
IMAGE_HEIGHT = model_config["IMAGE_HEIGHT"]
IMAGE_WIDTH = model_config["IMAGE_WIDTH"]
PIN_MEMORY = model_config["PIN_MEMORY"] in ("True")
LOAD_MODEL = model_config["LOAD_MODEL"] in ("True")
MODEL_CHECKPOINT = model_config["MODEL_CHECKPOINT"]
DICE_LOSS = model_config["DICE_LOSS"] in ("True")
DATASET_PATH = ""
if "DATASET_PATH" in model_config:
    DATASET_PATH = model_config["DATASET_PATH"]

SAVE_CHECKPOINT = results_config["SAVE_CHECKPOINT"]
SAVE_METRICS = results_config["SAVE_METRICS"]
SAVE_TB_LOGS = results_config["SAVE_TB_LOGS"]

earlyStopping = EarlyStopping(PATIENCE)

if not os.path.exists(SAVE_TB_LOGS):
    os.makedirs(SAVE_TB_LOGS)
if not os.path.exists(SAVE_CHECKPOINT):
    os.makedirs(SAVE_CHECKPOINT)
if not os.path.exists(SAVE_METRICS):
    os.makedirs(SAVE_METRICS)

writer = SummaryWriter(f"{SAVE_TB_LOGS}")

'''
GPU_NUM = sys.argv[1]
LEARNING_RATE = 1e-3
DEVICE = f"cuda:{GPU_NUM}" if torch.cuda.is_available() else "cpu"  #cuda:1
BATCH_SIZE = 32
START_EPOCH = 0
NUM_EPOCHS = 100
NUM_WORKERS = 4
IMAGE_HEIGHT = 256
IMAGE_WIDTH = 256
PIN_MEMORY = True
LOAD_MODEL = False
MODEL = sys.argv[2]
'''

def train(loader, model, optimizer, loss_fn, scaler, epoch):
    """[summary]

    Args:
        loader ([type]): [description]
        model ([type]): [description]
        optimizer ([type]): [description]
        loss_fn ([type]): [description]
        scaler ([type]): for mixed precision
        epoch ([type])): current epoch to track total steps for tensorboard
    """
    loop = tqdm(loader)
    length = len(loader)
    running_loss = 0

    for batch_idx, (data, targets) in enumerate(loop):
         data = data.to(device=DEVICE)
         targets = torch.unsqueeze(targets, 1).to(device=DEVICE)

         # float16 to speed up training
         with torch.cuda.amp.autocast():
             predictions = model(data)
             loss = loss_fn(predictions, targets)

         optimizer.zero_grad()
         scaler.scale(loss).backward()
         scaler.step(optimizer)
         scaler.update()

         loop.set_postfix(loss=loss.item())
         running_loss += loss.item()

    return running_loss / length

def eval(loader, model, loss_fn, epoch):
    """[summary]
        Calculate the validation loss over 1 epoch
    Args:
        loader ([type]): [description]
        model ([type]): [description]
        optimizer ([type]): [description]
        loss_fn ([type]): [description]
        scaler ([type]): for mixed precision
        epoch ([type])): current epoch to track total steps for tensorboard
    """
    model.eval()
    loop = tqdm(loader)
    length = len(loader)
    running_loss = 0

    for batch_idx, (data, targets) in enumerate(loop):
         data = data.to(device=DEVICE)
         targets = torch.unsqueeze(targets, 1).to(device=DEVICE)

         # float16 to speed up training
         with torch.cuda.amp.autocast():
             predictions = model(data)
             loss = loss_fn(predictions, targets)

         loop.set_postfix(loss=loss.item())
         running_loss += loss.item()

         # writer.add_scalar('Validation Loss', loss.item(), global_step=((epoch * length) + batch_idx))
    model.train()
    return running_loss / length

def main():
    train_transform = A.Compose(
        [
            # data agumentation? -> already have 200K samples
            A.Rotate(limit=35, p=0.5),
            A.HorizontalFlip(p=0.3),
            A.VerticalFlip(p=0.3),
            A.Normalize(
                mean=[0.0, 0.0, 0.0],
                std=[1.0, 1.0, 1.0],
                max_pixel_value=255.0
            ),
            ToTensorV2()
        ]
    )

    val_transforms = A.Compose(
        [
            A.Normalize(
                mean=[0.0, 0.0, 0.0],
                std=[1.0, 1.0, 1.0],
                max_pixel_value=255.0
            ),
            ToTensorV2()
        ]
    )

    print(f"Device: {DEVICE}. Device count: {torch.cuda.device_count()}")
    if MODEL == "resnet_18":
        model = models.resnet18(pretrained=True)
        num_ftrs = model.fc.in_features
        model.fc = nn.Linear(num_ftrs, 1)
    elif MODEL == "mobilenet_v3_small":
        model = models.mobilenet_v3_small(pretrained=True)
        num_ftrs = model.classifier[3].in_features
        model.classifier[3] = nn.Linear(num_ftrs, 1)
    elif MODEL == "custom_mobilenet_v3_small":
        # 576 -> 288 -> 64 -> 1
        pretrained = models.mobilenet_v3_small(pretrained=True)
        model = customMNSmall(pretrained)
    elif MODEL == "custom_mobilenet_v3_small_untrained":
        untrained = models.mobilenet_v3_small(pretrained=False)
        model = customMNSmall(untrained)
    elif MODEL == "mobilenet_v3_large":
        model = models.mobilenet_v3_large(pretrained=True)
        num_ftrs = model.classifier[3].in_features
        model.classifier[3] = nn.Linear(num_ftrs, 1)
    elif MODEL == "efficient_net_b3":
        model = models.efficientnet_b3(pretrained=True)
        num_ftrs = model.classifier[1].in_features
        model.classifier[1] = nn.Linear(num_ftrs, 1)
    elif MODEL == "efficient_net_b4":
        model = models.efficientnet_b4(pretrained=True)
        num_ftrs = model.classifier[1].in_features
        model.classifier[1] = nn.Linear(num_ftrs, 1)
    #print(f"MODEL: {model}")

    model = model.to(device=DEVICE)
    loss_fn = nn.BCEWithLogitsLoss()
    optimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE)
    if DATASET_PATH != "":
        train_loader, val_loader = get_loaders(
            BATCH_SIZE,
            train_transform,
            val_transforms,
            NUM_WORKERS,
            PIN_MEMORY,
            DATASET_PATH
        )
    else:
        train_loader, val_loader = get_loaders(
            BATCH_SIZE,
            train_transform,
            val_transforms,
            NUM_WORKERS,
            PIN_MEMORY
        )

    if LOAD_MODEL:
        print(f"Loading Model From Checkpoint")
        load_checkpoint(torch.load(MODEL_CHECKPOINT), model)

    scaler = torch.cuda.amp.GradScaler() # mixed percision for faster training

    train_losses = []
    train_accuracies = []
    val_losses = []
    val_accuracies = []

    '''
    best_epoch = None
    global_val_loss = float('inf')
    earlyStopping = EarlyStopping(patience=10)

    images, targets = iter(train_loader).next()
    images = images.to(device=DEVICE)
    writer.add_graph(model, images)
    writer.close()
    '''

    min_val_loss = float('inf')

    for epoch in range(NUM_EPOCHS):
        train_loss = train(train_loader, model, optimizer, loss_fn, scaler, epoch)
        train_losses.append(train_loss)
        writer.add_scalar('Training Loss', train_loss, global_step=epoch)

        train_acc, _, _, _ = check_metrics(train_loader, model, BATCH_SIZE, device=DEVICE)
        train_accuracies.append(train_acc.cpu().numpy().tolist())
        writer.add_scalar('Training Accuracy', train_acc, global_step=epoch)

        checkpoint = {
            "state_dict": model.state_dict(),
            "optimizer": optimizer.state_dict()
        }

        val_loss = eval(val_loader, model, loss_fn, epoch)
        val_losses.append(val_loss)
        writer.add_scalar('Validation Loss', val_loss, global_step=epoch)

        val_accuracy, _, _, _ = check_metrics(val_loader, model, BATCH_SIZE, device=DEVICE)
        val_accuracies.append(val_accuracy.cpu().numpy().tolist())
        writer.add_scalar('Validation Accuracy', val_accuracy, global_step=epoch)

        if val_loss < min_val_loss:
            save_checkpoint(checkpoint, epoch, folder=SAVE_CHECKPOINT)
            min_val_loss = val_loss

        if earlyStopping.training_completeV2(val_loss):
            break

    _, precision, recall, f1_score = check_metrics(val_loader, model, BATCH_SIZE, device=DEVICE)

    metrics = {
        "minimum_loss" : earlyStopping.minimum_loss,
        "best_epoch" : earlyStopping.best_epoch,
        "train_losses" : train_losses,
        "train_accuracies" : train_accuracies,
        "val_losses" : val_losses,
        "val_accuracies" : val_accuracies,
        "precision" : precision.cpu().numpy().tolist(),
        "recall" : recall.cpu().numpy().tolist(),
        "f1_score" : f1_score.cpu().numpy().tolist()
    }

    with open(f"{SAVE_METRICS}/metrics.json", "w") as metrics_file:
        json.dump(metrics, metrics_file)

if __name__ == "__main__":
    main()


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

# In[11]:

import numpy as np
import pandas as pd
import json
import datetime
import warnings
warnings.filterwarnings("ignore",category=FutureWarning)

# # Reading the csv file

# In[12]:

df = pd.read_csv('districts.csv',parse_dates=['Date'],usecols=['Date','State','District','Confirmed'])
# df

# # Dropping unknown rows and selecting date between 15-03-2021 and 14-08-2021

# In[13]:

df = df[((df['District']!='Unknown') & (df['Date']<='2021-08-14')&(df['Date']>='2020-03-15'))]
df.reset_index(drop=True, inplace=True)
# df

# # Making columns sorted and no. of cases as integer

# In[14]:

df = df.sort_values(['District','Date'])
df['Confirmed'] = df['Confirmed'].astype('int64')
df.reset_index(drop=True, inplace=True)
# df

# # 3.(a) Districtwise Analysis : Weekly

# In[15]:

df1 = df.copy()

df2 =pd.DataFrame(columns=['districtid','weekid','cases','Date']) #weekly

df1 = df1.groupby(['State','District'])
for district, district_df in df1:
#     print(district)
#     print(district_df)
    temp = district_df.shift(1)
    temp.fillna(0,inplace=True)
    district_df['Confirmed'] = district_df['Confirmed'] - temp['Confirmed']
    district_df.set_index('Date',inplace=True)
#     print(district_df)
    t = pd.DataFrame(columns=['districtid','stateid','weekid','cases','Date'])
    t['cases']= district_df.Confirmed.resample('W-Sat').sum()
    t['districtid'] = district[1]
    t['stateid'] = district[0]
    t['Date'] = t.index
    t.reset_index(drop=True,inplace=True)
    t['weekid'] = t.index+1
    t=t[['districtid','stateid','weekid','cases','Date']]
#     print(t)
    df2 = df2.append(t,ignore_index=True)

cases_week = df2.copy()
cases_week = cases_week[['districtid','weekid','cases']]
cases_week['cases'] = cases_week['cases'].astype('int64')
cases_week = cases_week[cases_week['districtid']!='Other State']
cases_week.to_csv('cases-week.csv',index=False)

# In[16]:

# df2

# # 3.(b) Districtwise Analysis: Monthly

# In[17]:

df3 = df.copy()

df4 =pd.DataFrame(columns=['districtid','monthid','cases','Date']) #weekly

df3 = df3.groupby(['State','District'])
for district, district_df in df3:
#     print(district)
#     print(district_df)
    temp = district_df.shift(1)
    temp.fillna(0,inplace=True)
    district_df['Confirmed'] = district_df['Confirmed'] - temp['Confirmed'] # removing cumulative values by shifting
    district_df.rename({'Confirmed':'cases'},axis=1,inplace=True)
    district_df.set_index('Date',inplace=True)
#     print(district_df)
    custom_dates = pd.DatetimeIndex([datetime.date(2020,5,14),
                                     datetime.date(2020,6,14),
                                     datetime.date(2020,7,14),
                                     datetime.date(2020,8,14),
                                     datetime.date(2020,9,14),
                                     datetime.date(2020,10,14),
                                     datetime.date(2020,11,14),
                                     datetime.date(2020,12,14),
                                     datetime.date(2021,1,14),
                                     datetime.date(2021,2,14),
                                     datetime.date(2021,3,14),
                                     datetime.date(2021,4,14),
                                     datetime.date(2021,5,14),
                                     datetime.date(2021,6,14),
                                     datetime.date(2021,7,14),
                                     datetime.date(2021,8,14)])
    t = district_df.groupby(custom_dates[custom_dates.searchsorted(district_df.index)]).sum()

    t['districtid'] = district[1]
    t['stateid'] = district[0]
    t['Date'] = t.index
    t.reset_index(drop=True,inplace=True)
    t['monthid'] = t.index+1
    t=t[['districtid','stateid','monthid','cases','Date']]

#     print(t)
    df4 = df4.append(t,ignore_index=True)

# In[18]:

cases_month = df4.copy()
cases_month = cases_month[['districtid','monthid','cases']]
cases_month['cases'] = cases_month['cases'].astype('int64')
cases_month = cases_month[cases_month['districtid']!='Other State']
cases_month.to_csv('cases-month.csv',index=False)

# # 3.(c) Districtwise Analysis: Overall

# In[19]:

df5 = df.copy()

df6 =pd.DataFrame(columns=['districtid','overallid','cases']) #weekly

df5 = df5.groupby(['State','District'])
for district, district_df in df5:

    temp = district_df.shift(1)
    temp.fillna(0,inplace=True)
    district_df['Confirmed'] = district_df['Confirmed'] - temp['Confirmed']
    district_df.set_index('Date',inplace=True)

    t= {}
    t['cases']= district_df['Confirmed'].sum()
    t['districtid'] = district[1]
    t['stateid'] = district[0]
    t['overallid'] = 1
#     print(t)

    df6 = df6.append(t,ignore_index=True)

cases_overall = df6.copy()
cases_overall = cases_overall[['districtid','overallid','cases']]
cases_overall['cases'] = cases_overall['cases'].astype('int64')
cases_overall = cases_overall[cases_overall['districtid']!='Other State']
cases_overall.to_csv('cases-overall.csv',index=False)

# In[20]:

print("Execution completed successfully")

# In[ ]:


import torch
import torch.nn as nn
from typing import Any
import numpy as np
from torchvision.ops import deform_conv2d
import torch.nn.functional as F

class IDANet(nn.Module):
    def _forward_unimplemented(self, *input: Any) -> None:
        pass

    def __init__(self):
        super(IDANet, self).__init__()
        print(f"initialize {self.__class__.__name__}")
        self.en = Encoder()
        self.de = Decoder()
        self.sab_list = nn.ModuleList([SAB(dila_rate=2 ** i, input_channel=32) for i in range(6)])
        self.att_list = nn.ModuleList(
            [nn.Sequential(
                nn.Conv2d(32 * 2, 32, 1, 1, 0),
                nn.Sigmoid()
            ) for i in range(6)])

    def forward(self, x):
        x = x.unsqueeze(dim=1)
        x = self.en(x)  # [b,32,t,f]
        c = x
        for i in range(6):
            c_pool = torch.cat([F.adaptive_avg_pool2d(c, (1, 1)), F.adaptive_max_pool2d(c, (1, 1))], dim=1)
            dis_att = self.att_list[i](c_pool)
            dis_c = c * dis_att
            c = dis_c
            x = self.sab_list[i](x, dis_c)
            c += x
        x = self.de(c)
        x = x.squeeze(dim=1)
        return x

class AttLayer(nn.Module):
    def _forward_unimplemented(self, *input: Any) -> None:
        pass

    def __init__(self, out_channels):
        super(AttLayer, self).__init__()
        self.out_channels = out_channels
        self.global_pool = nn.AdaptiveAvgPool2d(1)
        self.fc1 = nn.Conv2d(out_channels, out_channels, 1, bias=True)
        self.fc2 = nn.Conv2d(out_channels, out_channels * 2, 1, 1, bias=True)
        self.softmax = nn.Softmax(dim=1)

    def forward(self, x1, x2):
        # 仿照SKNet来做
        x = x1 + x2
        s = self.global_pool(x)
        z = self.fc1(s)
        a_b = self.fc2(z)
        a_b = a_b.reshape(-1, 2, self.out_channels, 1)
        a_b = self.softmax(a_b)
        a_b = list(a_b.chunk(2, dim=1))
        a_b = list(map(lambda x: x.reshape(-1, self.out_channels, 1, 1), a_b))
        output = [x1, x2]
        V = list(map(lambda x, y: x * y, output, a_b))
        out = V[0] + V[1]
        return out

class Encoder(nn.Module):
    def _forward_unimplemented(self, *input: Any) -> None:
        pass

    def __init__(self):
        super(Encoder, self).__init__()
        self.en1 = OperationLayer(in_channel=1, out_channel=4)
        self.en2 = OperationLayer(in_channel=4, out_channel=8)
        self.en3 = OperationLayer(in_channel=8, out_channel=16)
        self.en4 = OperationLayer(in_channel=16, out_channel=32)

    def forward(self, x):
        x = self.en1(x)
        x = self.en2(x)
        x = self.en3(x)
        x = self.en4(x)
        return x

class OperationLayer(nn.Module):
    def _forward_unimplemented(self, *input: Any) -> None:
        pass

    def __init__(self, in_channel, out_channel, dia_late=1):
        super(OperationLayer, self).__init__()
        self.in_channel = in_channel
        self.out_channel = out_channel
        self.op1 = nn.Sequential(
            nn.Conv2d(in_channels=self.in_channel,
                      out_channels=self.out_channel,
                      kernel_size=3,
                      stride=1,
                      padding=np.int(dia_late * 2 / 2),
                      dilation=dia_late),
            nn.PReLU(self.out_channel))
        self.op2 = nn.Sequential(
            nn.Conv2d(in_channels=self.in_channel,
                      out_channels=self.out_channel,
                      kernel_size=5,
                      stride=1,
                      padding=np.int(dia_late * 4 / 2),
                      dilation=dia_late),
            nn.PReLU(self.out_channel))
        self.op3 = nn.Sequential(
            nn.Conv2d(in_channels=self.in_channel,
                      out_channels=self.out_channel,
                      kernel_size=7,
                      stride=1,
                      padding=np.int(dia_late * 6 / 2),
                      dilation=dia_late),
            nn.PReLU(self.out_channel))
        self.op4 = nn.Sequential(
            nn.Conv2d(in_channels=self.in_channel,
                      out_channels=self.out_channel,
                      kernel_size=9,
                      stride=1,
                      padding=np.int(dia_late * 8 / 2),
                      dilation=dia_late),
            nn.PReLU(self.out_channel))
        self.w1 = nn.Conv2d(in_channels=self.in_channel,
                            out_channels=self.out_channel,
                            kernel_size=1,
                            stride=1)
        self.w2 = nn.Conv2d(in_channels=4 * self.out_channel,
                            out_channels=self.out_channel,
                            kernel_size=1,
                            stride=1)

    def forward(self, x):
        op1 = self.op1(x)
        op2 = self.op2(x)
        op3 = self.op3(x)
        op4 = self.op4(x)
        x_o = torch.cat([op1, op2, op3, op4], dim=1)
        x_o = self.w2(x_o)
        x_r = self.w1(x)
        x = x_o + x_r
        return x

class Decoder(nn.Module):
    def _forward_unimplemented(self, *input: Any) -> None:
        pass

    def __init__(self):
        super(Decoder, self).__init__()
        # multi-scale
        self.de1 = OperationLayer(in_channel=32, out_channel=16)
        self.de2 = OperationLayer(in_channel=16, out_channel=8)
        self.de3 = OperationLayer(in_channel=8, out_channel=4)
        self.de4 = OperationLayer(in_channel=4, out_channel=1)
        # offset
        self.of1 = nn.Conv2d(in_channels=16,
                             out_channels=2 * 16 * 9,
                             kernel_size=3,
                             stride=1,
                             padding=1)
        self.of2 = nn.Conv2d(in_channels=8,
                             out_channels=2 * 8 * 9,
                             kernel_size=3,
                             stride=1,
                             padding=1)
        self.of3 = nn.Conv2d(in_channels=4,
                             out_channels=2 * 4 * 9,
                             kernel_size=3,
                             stride=1,
                             padding=1)
        self.of4 = nn.Conv2d(in_channels=1,
                             out_channels=2 * 1 * 9,
                             kernel_size=3,
                             stride=1,
                             padding=1)

        # weight
        self.w1 = nn.Parameter(torch.Tensor(16, 16, 3, 3))
        self.w2 = nn.Parameter(torch.Tensor(8, 8, 3, 3))
        self.w3 = nn.Parameter(torch.Tensor(4, 4, 3, 3))
        self.w4 = nn.Parameter(torch.Tensor(1, 1, 3, 3))
        self.b1 = nn.Parameter(torch.Tensor(16))
        self.b2 = nn.Parameter(torch.Tensor(8))
        self.b3 = nn.Parameter(torch.Tensor(4))
        self.b4 = nn.Parameter(torch.Tensor(1))

        # att
        self.att_layer1 = AttLayer(out_channels=16)
        self.att_layer2 = AttLayer(out_channels=8)
        self.att_layer3 = AttLayer(out_channels=4)
        self.att_layer4 = AttLayer(out_channels=1)

    def forward(self, x):
        x1 = self.de1(x)
        of1 = self.of1(x1)
        d1 = deform_conv2d(x1, of1, self.w1, self.b1, padding=(1, 1))
        s1 = self.att_layer1(d1, x1)
        x2 = self.de2(s1)
        of2 = self.of2(x2)
        d2 = deform_conv2d(x2, of2, self.w2, self.b2, padding=(1, 1))
        s2 = self.att_layer2(d2, x2)
        x3 = self.de3(s2)
        of3 = self.of3(x3)
        d3 = deform_conv2d(x3, of3, self.w3, self.b3, padding=(1, 1))
        s3 = self.att_layer3(d3, x3)
        x4 = self.de4(s3)
        of4 = self.of4(x4)
        d4 = deform_conv2d(x4, of4, self.w4, self.b4, padding=(1, 1))
        s4 = self.att_layer4(d4, x4)
        return s4

class SAB(nn.Module):
    def _forward_unimplemented(self, *input: Any) -> None:
        pass

    def __init__(self, dila_rate, input_channel):
        super(SAB, self).__init__()
        self.in_conv = nn.Conv2d(in_channels=input_channel, out_channels=16, kernel_size=1, stride=1)
        self.dila_conv_left = nn.Sequential(
            nn.PReLU(16),
            nn.Conv2d(in_channels=16,
                      out_channels=16,
                      kernel_size=11,
                      stride=1,
                      padding=np.int((dila_rate * 10) / 2),
                      dilation=dila_rate))
        self.dila_conv_right = nn.Sequential(
            nn.PReLU(16),
            nn.Conv2d(in_channels=16,
                      out_channels=16,
                      kernel_size=11,
                      stride=1,
                      padding=np.int((dila_rate * 10) / 2),
                      dilation=dila_rate),
            nn.Sigmoid())
        self.out_conv = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=1, stride=1)
        self.out_prelu = nn.PReLU(32)

    def forward(self, x, c):
        x = x + c
        input = x
        x = self.in_conv(x)
        x1 = self.dila_conv_left(x)
        x2 = self.dila_conv_right(x)
        x = x1 * x2
        x = self.out_conv(x)
        x = x + input
        x = self.out_prelu(x)
        return x


from flask import Blueprint, render_template, session, redirect, url_for, request, jsonify
from app.utils.role_util import *
from app.db_controllers.admin_controller import *
from app.db_controllers.helper import *
from app.data.grade_breakdown import data as grade_breakdown
from app.data.formula import formula_list
from app.db_controllers.logging import *

admin = Blueprint('admin', __name__, url_prefix="/admin")

@admin.route('/')
def login():
    try:
        if session.get("admin_id"):
            return redirect(url_for("admin.dashboard"))
        elif session.get("contributor_id"):
            return redirect(url_for("contributor.dashboard"))
        elif session.get("graphics_id"):
            return redirect(url_for("graphics.dashboard"))
        elif session.get("super_admin_id"):
            return redirect(url_for("super_admin.dashboard"))
        return render_template("/admin/login/login.html")
    except Exception as e:
        log_error("view", "admin", str(e), "login")
        return jsonify({
            "message": str(e),
            "status": "error"
        }), 400

@admin.route("/dashboard")
@required_role_as_admin()
def dashboard():
    try:
        session["question_id_list"] = None
        user_data = get_user_document_data(session.get('admin_id'))
        leaderboard_data = get_admin_review_stats()
        chart_data = get_cumulative_chart_data()
        line_chart_data = get_chart_data(session.get('admin_id'))
        active_grade = user_data.get("active_grade")
        return render_template( "/admin/dashboard/dashboard.html",
                                grade_breakdown=grade_breakdown,
                                user_data=user_data,
                                active_grade=active_grade,
                                leaderboard_data=leaderboard_data,
                                chart_data=chart_data,
                                line_chart_data=line_chart_data
                            )
    except Exception as e:
        log_error("view", "admin", str(e), "dashboard")
        return jsonify({
            "message": str(e),
            "status": "error"
        }), 400

@admin.route("/editor")
@required_role_as_admin()
def editor():
    try:
        session["question_id_list"] = None
        user_data = get_user_document_data(session.get('admin_id'))
        active_grade = user_data.get("active_grade")
        return render_template( "/admin/question/editor/editor.html",
                                formula_list=formula_list,
                                grade_breakdown=grade_breakdown,
                                active_grade=active_grade
                            )
    except Exception as e:
        log_error("view", "admin", str(e), "editor")
        return jsonify({
            "message": str(e),
            "status": "error"
        }), 400

@admin.route("/levels")
@required_role_as_admin()
def levels():
    session["question_id_list"] = None
    user_data = get_user_document_data(session.get('admin_id'))
    active_grade = user_data.get("active_grade")
    try:
        grade = request.args.get("grade")
        grade_dict = next((grade_dict for grade_dict in grade_breakdown if grade_dict.get('grade_number') == int(grade)), None)
        if not grade_dict:
            return jsonify({
                "status": "error",
                "message": "Invalid Grade!"
            }), 400
        chapter = request.args.get("chapter")
        chapter_dict = next((chapter_dict for chapter_dict in grade_dict.get("chapters") if chapter_dict.get('chapter_number') == int(chapter)), None)
        if not chapter_dict:
            return jsonify({
                "status": "error",
                "message": "Invalid Chapter!"
            }), 400
        levels = chapter_dict.get("levels")
        questions_for_review = firebase_get_questions_for_review(f"NCERT_G{int(grade):02}_TOPIC{int(chapter):02}")
        for level in levels:
            level_id = f"NCERT_G{int(grade):02}_TOPIC{int(chapter):02}_LEVEL{int(level.get('level_number')):02}"
            level["questions_for_review"] = questions_for_review.get(level_id) or 0
        return render_template( "/admin/level/level.html",
                                levels=levels,
                                chapter_name=chapter_dict.get("chapter_name"),
                                grade=grade,
                                chapter=chapter,
                                grade_breakdown=grade_breakdown,
                                active_grade=active_grade
                            )
    except ValueError as e:
        log_error("view", "admin", str(e), "levels")
        return jsonify({
            "status": "error",
            "message": "Do not tamper with the URL. Go to dashboard and try again!"
        }), 400

@admin.route("/review-question")
@required_role_as_admin()
def review_question():
    user_data = get_user_document_data(session.get('admin_id'))
    active_grade = user_data.get("active_grade")
    try:
        grade = int(request.args.get("grade"))
        chapter = int(request.args.get("chapter"))
        level = int(request.args.get("level"))
        document_id = request.args.get("document_id")
        if not all((grade, chapter, level)):
            return jsonify({
                "status": "error",
                "message": "Do not tamper with the URL. Go to dashboard and try again!"
            })
        if not document_id:
            question_document_ids = firebase_get_questions_for_review_list(grade, chapter, level)
            if len(question_document_ids) == 0:
                return jsonify({
                    "status": "error",
                    "message": "No questions for review!"
                }), 400
            session["question_id_list"] = question_document_ids
        question_document_list = session.get("question_id_list")
        if not question_document_list:
            return redirect(url_for("admin.dashboard"))
        elif len(question_document_list) == 0:
            return jsonify({
                "status": "error",
                "message": "No questions for review!"
            }), 400
        if not document_id:
            document_id = question_document_list[0]
        try:
            next_document_id = question_document_list[question_document_list.index(document_id) + 1]
        except IndexError:
            next_document_id = ""
        question_data = firebase_get_question(document_id, grade, chapter, level)
        grade_dict, chapter_dict, level_dict = get_grade_breakdown_dict(grade, chapter, level)
        return render_template( "/admin/question/review_question/review_question.html",
                                grade=grade,
                                chapter=chapter,
                                level=level,
                                level_name=level_dict.get("level_name"),
                                grade_breakdown=grade_breakdown,
                                next_document_id=next_document_id,
                                document_id=document_id,
                                question_data=question_data,
                                formula_list=formula_list,
                                pdf_url=chapter_dict.get("chapter_pdf"),
                                active_grade=active_grade
                            )
    except ValueError as e:
        log_error("view", "admin", str(e), "review_question")
        return jsonify({
            "status": "error",
            "message": "Do not tamper with the URL. Go to dashboard and try again!"
        }), 400


"""
This program process events.csv and outputs number of events by customer_id within one hour buckets.
Results of this program should be available here: http://localhost:8080/processed_events.json
"""
import csv
import json
import logging
import re
import unittest
from collections import defaultdict

from dateutil import parser

# excluding all events with this event_type
PROXY_EVENT = "INGEST-WITH-PROXY"

# files for input and outputting
INPUT_FILE = "events.csv"
OUTPUT_FILE = "processed_events.json"

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class ProcessEvents:
    def __init__(self):
        self.input_file = INPUT_FILE
        self.output_file = OUTPUT_FILE
        # key is customer_id; value is a dict of timestamp as key and number of processed events within the past hour
        self.customer_events = defaultdict(dict)
        self.skipped_rows = 0
        self.output = {}

    @staticmethod
    def process_customer_id(id):
        pattern = r"^[a-f0-9]{32}$"
        match = re.match(pattern, id)
        return match.group(0) if match else None

    @staticmethod
    def process_timestamps(ts):
        timestamp = parser.parse(ts)
        # round up to nearest hour
        timestamp_rounded = timestamp.replace(minute=0, second=0, microsecond=0)
        # format the timestamp as a string in the desired format
        return timestamp_rounded.strftime("%Y-%m-%d %H:%M:%S")

    def process_file(self):
        with open(self.input_file, newline="") as csvfile:
            reader = csv.reader(csvfile, delimiter=",", quotechar='"')
            rows = (row for row in reader)

            for row in rows:
                row_length = len(row)
                if row_length == 4:
                    if len(row[1]) != 6 or row[1] == PROXY_EVENT or row[2] == "%test%":
                        self.skipped_rows += 1
                        continue
                    processed_customer_id = self.process_customer_id(row[0])
                    if not processed_customer_id:
                        self.skipped_rows += 1
                        continue
                    processed_timestamp = self.process_timestamps(row[3])
                    self.customer_events[processed_customer_id][processed_timestamp] = (
                        self.customer_events[processed_customer_id].get(
                            processed_timestamp, 0
                        )
                        + 1
                    )
            if self.skipped_rows:
                logger.warning(
                    f"{self.skipped_rows} row(s) are not processed due to format not matching to requirements."
                )
            print("\n")

        with open(self.output_file, "w") as f:
            json.dump(self.customer_events, f)

    def output_valid_buckets(self, customer_id, start_ts, end_ts, output):
        buckets = self.customer_events[customer_id]
        for time_stamp, values in buckets.items():
            if start_ts <= time_stamp <= end_ts and time_stamp not in output:
                output[time_stamp] = values
        return output

    def ts_format_check(self, ts):
        pattern_ts = r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$"
        match = re.match(pattern_ts, ts)
        if match:
            return True
        else:
            return False

    def output_values_for_user(self):
        print("\n")
        request_prompt = input(
            "Do you want to input customer_id and time_stamp? Input 'Y' for yes or 'N' to exit the "
            "program. Any other input will cause the program to exit. \n"
        )
        if request_prompt == "N":
            print("Exiting program! \n")
            exit()
        elif request_prompt != "Y":
            print("Invalid input, exiting program! \n")
            exit()
        else:
            customer_id_to_check = input("Enter customer_id: \n")
            processed_customer_id = self.process_customer_id(customer_id_to_check)
            if not processed_customer_id:
                print("Invalid input, exiting program! \n")
                exit()
            if customer_id_to_check not in self.customer_events:
                print("This customer_id is not in the log hence exiting program! \n")
                exit()
            print(
                "Enter timestamps of format type - YYYY-MM-DD HH:MM:SS, example - 2021-03-01 13:00:00.\n"
            )
            ts_start = input("Enter start timestamp: \n")
            ts_end = input("Enter end timestamp: \n")
            if not self.ts_format_check(ts_start) or not self.ts_format_check(ts_end):
                print("Invalid input, exiting program! \n")
                exit()
        ts_start, ts_end = self.process_timestamps(ts_start), self.process_timestamps(
            ts_end
        )
        self.output_valid_buckets(customer_id_to_check, ts_start, ts_end, self.output)

    def output_values(self):
        if not self.output:
            print("No values in the provided timestamps! \n")
        else:
            print("These are the values for the timestamps provided! \n")
            for key, values in self.output.items():
                print(key, values)

class TestProcessEvents(unittest.TestCase):
    def test_process_customer_id(self):
        # test whether appropriately customer_id is processed or not
        pe = ProcessEvents()
        self.assertEqual(
            pe.process_customer_id("56789012345678901212345678901234"),
            "56789012345678901212345678901234",
        )
        self.assertEqual(pe.process_customer_id("123456"), None)
        self.assertEqual(
            pe.process_customer_id("789012345612345678901234567890123"), None
        )

    def test_process_timestamps(self):
        # test whether timestamps are outputting based on provided requirements.
        pe = ProcessEvents()
        ts1 = str("2021-03-03 19:47:59.868")
        str2 = str("2021-03-03 19:00:00")
        self.assertEqual(
            pe.process_timestamps(ts1),
            str2,
        )
        str3 = str("2021-03-03 20:00:00")
        str4 = str("2021-03-03 17:00:58.1")
        str5 = str("2021-03-03 17:00:00")
        self.assertNotEqual(
            pe.process_timestamps(ts1),
            str3,
        )
        self.assertEqual(
            pe.process_timestamps(str4),
            str5,
        )

    def test_results_accuracy(self):
        pe = ProcessEvents()
        pe.process_file()
        self.assertEqual(
            pe.customer_events["1abb42414607955dbf6088b99f837d8f"][
                "2021-03-01 04:00:00"
            ],
            2,
        )
        self.assertEqual(
            pe.customer_events["1abb42414607955dbf6088b99f837d8f"][
                "2021-03-01 15:00:00"
            ],
            13,
        )
        self.assertNotEqual(
            pe.customer_events["1abb42414607955dbf6088b99f837d8f"][
                "2021-03-01 16:00:00"
            ],
            20,
        )

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

answer = ProcessEvents()
answer.process_file()
answer.output_values_for_user()
answer.output_values()


import cv2
import json
from matplotlib import pyplot as plt

f = open('config.json')

data = json.load(f)

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(data['webcamNumber'])

while cap.isOpened():
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)

    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)

    cv2.imshow('Webcam', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()


from typing import Optional

from lcls_tools.superconducting.scLinac import (
    Cavity,
    CryoDict,
    Piezo,
    SSA,
    StepperTuner,
)
from lcls_tools.superconducting.sc_linac_utils import RF_MODE_SELAP

from gui_utils import SetupSignals

class SetupCavity(Cavity):
    def __init__(
        self,
        cavityNum,
        rackObject,
        ssaClass=SSA,
        stepperClass=StepperTuner,
        piezoClass=Piezo,
    ):
        super().__init__(cavityNum, rackObject)
        self._signals: Optional[SetupSignals] = None

        self._ssa_cal_requested: bool = False
        self._auto_tune_requested: bool = False
        self._cav_char_requested: bool = False
        self._rf_ramp_requested: bool = False

        self.running: bool = False

    @property
    def signals(self):
        return self._signals

    @signals.setter
    def signals(self, value: SetupSignals):
        self._signals = value

    @property
    def status_message(self):
        # TODO get from status PV when available
        raise NotImplementedError("Cavity status PV not yet created")

    @status_message.setter
    def status_message(self, message):
        # TODO write to status PV when available
        print(message)
        if self.signals:
            self.signals.status.emit(message)

    # TODO use severity PV and status message when created
    def set_finished_message(self, message):
        print(message)
        if self.signals:
            self.signals.finished.emit(message)

    # TODO use status PV when implemented
    @property
    def script_is_running(self) -> bool:
        return self.running

    # TODO use PV when available
    @property
    def ssa_cal_requested(self) -> bool:
        return self._ssa_cal_requested

    @ssa_cal_requested.setter
    def ssa_cal_requested(self, value: bool):
        self._ssa_cal_requested = value

    # TODO use PVs when available
    @property
    def auto_tune_requested(self) -> bool:
        return self._auto_tune_requested

    @auto_tune_requested.setter
    def auto_tune_requested(self, value: bool):
        self._auto_tune_requested = value

    @property
    def cav_char_requested(self) -> bool:
        return self._cav_char_requested

    @cav_char_requested.setter
    def cav_char_requested(self, value: bool):
        self._cav_char_requested = value

    @property
    def rf_ramp_requested(self) -> bool:
        return self._rf_ramp_requested

    @rf_ramp_requested.setter
    def rf_ramp_requested(self, value: bool):
        self._rf_ramp_requested = value

    def capture_acon(self):
        self.acon = self.ades

    def shut_down(self):
        self.running = True
        self.status_message = f"Shutting {self} down"
        self.turnOff()
        self.ssa.turn_off()
        self.set_finished_message(f"{self} RF and SSA off")
        self.running = False

    def setup(self):
        self.running = True

        self.status_message = f"Turning {self} off"
        self.turnOff()

        self.status_message = f"Turning on {self} SSA if not on already"
        self.ssa.turn_on()

        self.status_message = f"Resetting {self} interlocks"
        self.reset_interlocks()

        if self.ssa_cal_requested:
            self.status_message = f"Running {self} SSA Calibration"
            self.turnOff()
            self.ssa.calibrate(self.ssa.drive_max)
            self.set_finished_message(f"{self} SSA Calibrated")

        self.check_abort()

        if self.auto_tune_requested:
            self.status_message = f"Tuning {self} to Resonance"
            self.move_to_resonance(use_sela=False)
            self.set_finished_message(f"{self} Tuned to Resonance")

        self.check_abort()

        if self.cav_char_requested:
            self.status_message = f"Running {self} Cavity Characterization"
            self.characterize()
            self.calc_probe_q_pv_obj.put(1)
            self.set_finished_message(f"{self} Characterized")

        self.check_abort()

        if self.rf_ramp_requested:
            self.status_message = f"Ramping {self} to {self.acon}"
            self.piezo.enable_feedback()

            if not self.is_on or (self.is_on and self.rf_mode != RF_MODE_SELAP):
                self.ades = min(5, self.acon)

            self.turn_on()

            self.check_abort()

            self.set_sela_mode()
            self.walk_amp(self.acon, 0.1)

            self.status_message = f"Centering {self} piezo"
            self.move_to_resonance(use_sela=True)

            self.set_selap_mode()

            self.set_finished_message(f"{self} Ramped Up to {self.acon} MV")

        self.running = False

SETUP_CRYOMODULES: CryoDict = CryoDict(cavityClass=SetupCavity)


# encoding=utf8

import PIL.Image
import itertools
import fontforge

CHARS = (
    u'ABCDEFGHIJKLMNOP',
    u'QRSTUVWXYZ():;[]',
    u'abcdefghijklmnop',
    u'qrstuvwxyz',
    u'ÄÖÜäöü',
    u'ḍḷṃṛṣṭṿ',
    u"'ⓅⓂ-  ?! &é    ♂",
    u'$×./,♀0123456789',
    u'',
    u'⒫⒦“” …'
)
SQUARE_SIZE = 100
VERSION = '1.0.1'
COPYRIGHT = 'Original by Nintendo / Game Freak / Pokemon Co. ' \
            'Converted by TPPOCR project.'

def get_pixel_data(filename='crystal_tiles.png', rows=10):
    image = PIL.Image.open(filename)
    image = image.convert('1')

    data = []

    for tile_row in range(rows):
        row_data = []

        for tile_col in range(16):
            tile_pixel_data = []
            for tile_y, tile_x in itertools.product(range(8), range(8)):
                x = tile_col * 8 + tile_x
                y = tile_row * 8 + tile_y

                tile_pixel_data.append(image.getpixel((x, y)))

            row_data.append(tile_pixel_data)

        data.append(row_data)

    return data

def main():
    font = fontforge.font()
    font.descent = 1 * SQUARE_SIZE
    font.ascent = 8 * SQUARE_SIZE
    font.upos = -1 * SQUARE_SIZE
    font.uwidth = SQUARE_SIZE
    font.fontname = 'pkmn_gb_en_tppocr-regular'
    font.familyname = 'pkmn_gb_en_tppocr'
    font.fullname = 'PkMn GB English TPPOCR Regular'
    font.encoding = 'unicode'

    char_pixel_data = get_pixel_data()
    red_char_pixel_data = get_pixel_data('red_font.png', rows=7)

    def draw_glyph(char, char_pixel_data):
        glyph = font.createChar(ord(char))
        glyph.clear()
        pixel_data = char_pixel_data[char_row][char_col]
        pen = glyph.glyphPen()

        for tile_y, tile_x in itertools.product(range(8), range(8)):
            if pixel_data[tile_y * 8 + tile_x] != 0:
                continue

            x = tile_x * SQUARE_SIZE
            y = 7 * SQUARE_SIZE - tile_y * SQUARE_SIZE

            # Add +/-1 to slightly increase the block size so each block
            # will slightly overlap and ensure no points overlap.
            pen.moveTo((x - 1, y - 1))
            pen.lineTo((x + SQUARE_SIZE + 1, y - 1))
            pen.lineTo((x + SQUARE_SIZE + 1, y + SQUARE_SIZE + 1))
            pen.lineTo((x - 1, y + SQUARE_SIZE + 1))
            pen.closePath()

        glyph.width = SQUARE_SIZE * 8

    for char_row, row in enumerate(CHARS):
        for char_col, char in enumerate(row):
            if char == ' ':
                continue

            draw_glyph(char, char_pixel_data)

            if char == 'c':
                draw_glyph(u'ċ', red_char_pixel_data)

    glyph = font.createChar(ord(' '))
    glyph.width = SQUARE_SIZE * 8

    font.selection.all()
    font.removeOverlap()
    font.simplify()
    font.correctDirection()

    font.version = VERSION
    font.copyright = COPYRIGHT

    font.generate(font.fontname + '.ttf', flags=('no-hints',))

if __name__ == '__main__':
    main()


import json
import pandas as pd
import numpy as np
import gzip

################################################################################
def isfloat(x): # {{{
    try:
        float(x)
        return True
    except ValueError:
        return False
    except TypeError:
        return False

# }}}

################################################################################
def json_to_df(data = None, path = None): #{{{

    assert bool(data is not None) ^ bool(path is not None)

    if path is not None:
        if path.endswith(".gz"):
            with gzip.open(path, 'r') as f:
                file_content = f.read()
            json_str = file_content.decode("utf-8")
            data = json.loads(json_str)
        else:
            f = open(path, "r")
            data = json.load(f)
            f.close()

    x = flatten_dict(data)
    x = [(*a, str(b)) for a,b in x.items()]
    x = pd.DataFrame(x)
    x.columns = ["lvl" + str(i) for i in range(len(x.columns))]
    return x
# }}}

################################################################################
def flatten_dict(nested_dict): #{{{
    res = {}
    if isinstance(nested_dict, dict):
        for k in nested_dict:
            flattened_dict = flatten_dict(nested_dict[k])
            for key, val in flattened_dict.items():
                key = list(key)
                key.insert(0, k)
                res[tuple(key)] = val
    else:
        res[()] = nested_dict
    return res
# }}}

################################################################################
class mapping_variables: # {{{

    def __init__(self):
        self.building_type_to_class =\
                pd.DataFrame(data = {
                      "assembly"           : "Commercial"
                    , "education"          : "Commercial"
                    , "food sales"         : "Commercial"
                    , "food service"       : "Commercial"
                    , "health care"        : "Commercial"
                    , "lodging"            : "Commercial"
                    , "large office"       : "Commercial"
                    , "small office"       : "Commercial"
                    , "mercantile/service" : "Commercial"
                    , "warehouse"          : "Commercial"
                    , "other"              : "Commercial"
                    , "single family home" : "Residential"
                    , "multi family home"  : "Residential"
                    , "mobile home"        : "Residential"
                    }.items(),
                    columns = ["building_type", "building_class"]
                    )

        self.end_uses =\
                pd.DataFrame(data = {
                      "Cooking"                   : "Appliances"
                    , "Cooling (Env.)"            : np.nan
                    , "Cooling (Equip.)"          : "Cooling"
                    , "Computers and Electronics" : "Other"
                    , "Heating (Env.)"            : np.nan
                    , "Heating (Equip.)"          : "Heating"
                    , "Lighting"                  : "Lighting"
                    , "Other"                     : "Other"
                    , "Refrigeration"             : "Appliances"
                    , "Ventilation"               : np.nan
                    , "Water Heating"             : "Appliances"
                    }.items(),
                    columns = ["end_use", "emf_end_use"]
                    )

        self.building_class_construction =\
                pd.DataFrame.from_dict(data = {
                    "Commercial (Existing)"  : {
                        "building_class" : "Commercial",
                        "building_construction" : "Existing"
                        },
                    "Commercial (New)": {
                        "building_class" : "Commercial",
                        "building_construction" : "New"
                        },
                    "Residential (Existing)" : {
                        "building_class" : "Residential",
                        "building_construction" : "Existing"
                        },
                    "Residential (New)": {
                        "building_class" : "Residential",
                        "building_construction" : "New"
                        }
                    }, orient = "index")\
                            .reset_index()\
                            .rename(columns = {"index" : "building_class0"})

        self.direct_indirect_fuel =\
                pd.DataFrame(data = {
                    "Natural Gas"      : "Direct",
                    "Distillate/Other" : "Direct",
                    "Biomass"          : "Direct",
                    "Propane"          : "Direct",
                    "Electric"         : "Indirect",
                    "Non-Electric"     : "Indirect"
                    }.items(),
                    columns = ["fuel_type", "direct_indirect_fuel"]
                    )

        self.emf_base_string =\
                pd.DataFrame(data = {
                    "Avoided CO\u2082 Emissions (MMTons)" : "*Emissions|CO2|Energy|Demand|Buildings",
                    "Energy Savings (MMBtu)"              : "*Final Energy|Buildings"
                    }.items(),
                    columns = ["impact", "emf_base_string"]
                    )

        self.fuel_types =\
                pd.DataFrame(data = {
                    "Natural Gas"      : "Gas",
                    "Propane"          : "Gas",
                    "Distillate/Other" : "Oil",
                    "Biomass"          : "Biomass Solids",
                    "Electric"         : "Electricity",
                    "Electricity"      : "Electricity"
                    }.items(),
                    columns = ["fuel_type", "emf_fuel_type"]
                    )

    def info(self):
        print("A collection of DataFrames to map one set of variable values to another.\n" +
              " * building_type_to_class" +
              " * building_class_construction" +
              " * direct_indirect_fuel" +
              " * end_uses" +
              " * emf_base_string " +
              " * fuel_types "
                )
# }}}

################################################################################
class conversion_data: # {{{
    def __init__(self, version = ""): # {{{

        assert version in ["", "ce", "gsref", "decarb", "decarb_lite"]

        self.path = "supporting_data/convert_data/site_source_co2_conversions" + version + ".json"
        df = json_to_df(path = self.path)
        self.aeo_year = int(df.lvl1[df.lvl0 == "updated_to_aeo_year"].values[0])
        self.aeo_case = str(df.lvl1[df.lvl0 == "updated_to_aeo_case"].values[0])
        self.calc_method = str(df.lvl1[df.lvl0 == "site-source calculation method"].values[0])
        #
        self.units_sources = df[(df.lvl1 == "units") | (df.lvl1 == "source") | (df.lvl2 == "units") | (df.lvl2 == "source")][["lvl0", "lvl1", "lvl2", "lvl3"]]
        self.units_sources.loc[self.units_sources.lvl0 == "CO2 price", "lvl3"] = self.units_sources.loc[self.units_sources.lvl0 == "CO2 price", "lvl2"]
        self.units_sources.loc[self.units_sources.lvl0 == "CO2 price", "lvl2"] = self.units_sources.loc[self.units_sources.lvl0 == "CO2 price", "lvl1"]
        self.units_sources.loc[self.units_sources.lvl0 == "CO2 price", "lvl1"] = np.nan
        #
        self.data = df.loc[df.lvl2 == "data"][["lvl0", "lvl1", "lvl3", "lvl4", "lvl5"]]
        idx = ~((self.data.lvl3 == "residential") | (self.data.lvl3 == "commercial"))
        self.data.loc[idx, "lvl5"] = self.data.loc[idx, "lvl4"]
        self.data.loc[idx, "lvl4"] = self.data.loc[idx, "lvl3"]
        self.data.loc[idx, "lvl3"] = np.nan
        self.data.columns = ["fuel", "impact", "rescom", "year", "value"]
        self.data.value = self.data.value.apply(float)
        self.data.year  = self.data.year.apply(int)
    # }}}

    def info(self): # {{{
        print(f"path:                           {self.path}")
        print(f"basename:                       {self.basename}")
        print(f"aeo_year:                       {self.aeo_year}")
        print(f"aeo_case:                       {self.aeo_case}")
        print(f"site-source calculation method: {self.calc_method}")
        print(f"units_sources:                  a DataFrame")
        print(f"data:                           a DataFrame")
    # }}}
# }}}

################################################################################
#                                 End of File                                  #
################################################################################


from src.app_functions.question.pick_question import pick_question
from src.app_functions.statistics import save_statistics

def notification_handler(app):
    """Called when clicked on a notification of the app through the decorator of rumps.
    Updates statistics based on return value of pick_question()

    Args:
        app (rumps.App): The App object of the main app
    """
    correct = pick_question(app)
    app.statistics["total_questions"][1] += 1
    if correct:
        app.statistics["correct_answers"][1] += 1
    else:
        app.statistics["incorrect_answers"][1] += 1
    save_statistics.save_statistics(app)


from styles.settings import VIEW_STYLES_MAP

IGNORE_APPS = [
    'grappelli',
    'django',
]

def get_view_labels(view):
    '''Return app_label, view_name for the view'''
    try:
        view_name = view.__name__
    except AttributeError:
        return None, None
    app_label = view.__module__

    if any(map(app_label.startswith, IGNORE_APPS)):
        return None, None

    config = VIEW_STYLES_MAP.get(view_name, None)
    if config is not None:
        view_name = config['view_class']
        app_label = config['app_class']

    elif app_label.endswith('.views'):
        app_label = app_label[:-6]

    app_label = app_label.replace('.', '_')

    return app_label, view_name

def create_classes_from_request(request):
    """
    Creates app_class and view_class names for the views we care about, i.e.
    the ones that display content on the front-end.

    Store those class names in the request.session for use when retrieving stylesheets
    particular to the app or view.
    """
    view = getattr(request, 'view', None)

    return get_view_labels(view)