#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# File: cifar-convnet.py
# Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import tensorflow as tf
import argparse
import numpy as np
import os

from tensorpack import *
import tensorpack.tfutils.symbolic_functions as symbf
from tensorpack.tfutils.summary import *
from tensorpack.utils.gpu import get_nr_gpu

"""
A small convnet model for Cifar10 or Cifar100 dataset.

Cifar10:
    91% accuracy after 50k step.
    19.3 step/s on Tesla M40

Not a good model for Cifar100, just for demonstration.
"""

class Model(ModelDesc):
    def __init__(self, cifar_classnum):
        super(Model, self).__init__()
        self.cifar_classnum = cifar_classnum

    def _get_input_vars(self):
        return [InputVar(tf.float32, [None, 30, 30, 3], 'input'),
                InputVar(tf.int32, [None], 'label')
               ]

    def _build_graph(self, input_vars):
        image, label = input_vars
        is_training = get_current_tower_context().is_training
        keep_prob = tf.constant(0.5 if is_training else 1.0)

        if is_training:
            tf.image_summary("train_image", image, 10)

        image = image / 4.0     # just to make range smaller
        with argscope(Conv2D, nl=BNReLU, use_bias=False, kernel_shape=3):
            logits = LinearWrap(image) \
                    .Conv2D('conv1.1', out_channel=64) \
                    .Conv2D('conv1.2', out_channel=64) \
                    .MaxPooling('pool1', 3, stride=2, padding='SAME') \
                    .Conv2D('conv2.1', out_channel=128) \
                    .Conv2D('conv2.2', out_channel=128) \
                    .MaxPooling('pool2', 3, stride=2, padding='SAME') \
                    .Conv2D('conv3.1', out_channel=128, padding='VALID') \
                    .Conv2D('conv3.2', out_channel=128, padding='VALID') \
                    .FullyConnected('fc0', 1024 + 512, nl=tf.nn.relu) \
                    .tf.nn.dropout(keep_prob) \
                    .FullyConnected('fc1', 512, nl=tf.nn.relu) \
                    .FullyConnected('linear', out_dim=self.cifar_classnum, nl=tf.identity)()

        cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, label)
        cost = tf.reduce_mean(cost, name='cross_entropy_loss')

        wrong = symbf.prediction_incorrect(logits, label)
        # monitor training error
        add_moving_summary(tf.reduce_mean(wrong, name='train_error'))

        # weight decay on all W of fc layers
        wd_cost = tf.mul(0.0004,
                         regularize_cost('fc.*/W', tf.nn.l2_loss),
                         name='regularize_loss')
        add_moving_summary(cost, wd_cost)

        add_param_summary([('.*/W', ['histogram'])])   # monitor W
        self.cost = tf.add_n([cost, wd_cost], name='cost')

def get_data(train_or_test, cifar_classnum):
    isTrain = train_or_test == 'train'
    if cifar_classnum == 10:
        ds = dataset.Cifar10(train_or_test)
    else:
        ds = dataset.Cifar100(train_or_test)
    if isTrain:
        augmentors = [
            imgaug.RandomCrop((30, 30)),
            imgaug.Flip(horiz=True),
            imgaug.Brightness(63),
            imgaug.Contrast((0.2,1.8)),
            imgaug.GaussianDeform(
                [(0.2, 0.2), (0.2, 0.8), (0.8,0.8), (0.8,0.2)],
                (30,30), 0.2, 3),
            imgaug.MeanVarianceNormalize(all_channel=True)
        ]
    else:
        augmentors = [
            imgaug.CenterCrop((30, 30)),
            imgaug.MeanVarianceNormalize(all_channel=True)
        ]
    ds = AugmentImageComponent(ds, augmentors)
    ds = BatchData(ds, 128, remainder=not isTrain)
    if isTrain:
        ds = PrefetchData(ds, 3, 2)
    return ds

def get_config(cifar_classnum):
    logger.auto_set_dir()

    # prepare dataset
    dataset_train = get_data('train', cifar_classnum)
    step_per_epoch = dataset_train.size()
    dataset_test = get_data('test', cifar_classnum)

    sess_config = get_default_sess_config(0.5)

    lr = symbf.get_scalar_var('learning_rate', 1e-2, summary=True)
    def lr_func(lr):
        if lr < 3e-5:
            raise StopTraining()
        return lr * 0.31

    return TrainConfig(
        dataset=dataset_train,
        optimizer=tf.train.AdamOptimizer(lr, epsilon=1e-3),
        callbacks=Callbacks([
            StatPrinter(), ModelSaver(),
            InferenceRunner(dataset_test, ClassificationError()),
            StatMonitorParamSetter('learning_rate', 'val_error', lr_func,
                threshold=0.001, last_k=10),
        ]),
        session_config=sess_config,
        model=Model(cifar_classnum),
        step_per_epoch=step_per_epoch,
        max_epoch=150,
    )

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', help='comma separated list of GPU(s) to use.')
    parser.add_argument('--load', help='load model')
    parser.add_argument('--classnum', help='10 for cifar10 or 100 for cifar100',
                        type=int, default=10)
    args = parser.parse_args()

    if args.gpu:
        os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
    else:
        os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    with tf.Graph().as_default():
        config = get_config(args.classnum)
        if args.load:
            config.session_init = SaverRestore(args.load)

        if args.gpu:
            config.nr_tower = len(args.gpu.split(','))
        nr_gpu = get_nr_gpu()
        if nr_gpu == 1:
            QueueInputTrainer(config).train()
        else:
            SyncMultiGPUTrainer(config).train()


import cv2
import math
import monta
import numpy as np
import matcompat
from scipy import signal
import matplotlib.pyplot as plt

lammbda=6
pi = math.pi
theta = np.arange(0, (np.pi-np.pi/8)+(np.pi/8), np.pi/8)
psi = 0
gamma = np.linspace(.4,1,4)
gamma = np.arange(.4, 1.2, .2)
b = 4
sigma = (1/pi)*math.sqrt((math.log(2)/2))*((2**b+1)/(2**b-1))*lammbda
l = int(12/2)
gt = 0
imagen0 = np.float32(cv2.imread('images/NegroyYo4.jpg'))
imagen0 = cv2.cvtColor(imagen0,cv2.COLOR_BGR2RGB)
imagen0 = cv2.resize(imagen0, (320, 240))
imagen1 = (imagen0-128)/127
imagen = np.zeros((240,320,4))
imagen[:,:,0]=(imagen1[:,:,0]-imagen1[:,:,1])/2
imagen[:,:,1]=(imagen1[:,:,0]+imagen1[:,:,1]-2*imagen1[:,:,2])/4
imagen[:,:,2]=(imagen1[:,:,0]+imagen1[:,:,1]+imagen1[:,:,2])/3
s = matcompat.size(imagen0)
for i in np.arange(1., (s[0])+1):
    for j in np.arange(1., (s[1])+1):
        imagen[int(i)-1,int(j)-1,3] = ((imagen1[int(i)-1,int(j)-1,:]).max()-(imagen1[int(i)-1,int(j)-1,:]).min())/2
contador = 0
g = np.zeros((13,13))
imagenSalida=np.zeros((240,320,4,32))
for i in range(len(theta)):
	for f in range(len(gamma)):
		for j in range(-l,l+1):
			for k in range(-l,l+1):
				x = j*math.cos(theta[i])+k*math.sin(theta[i])
				y = k*math.cos(theta[i])-j*math.sin(theta[i])
				g[j+l,k+l]=math.exp(-(x**2 + (gamma[f]**2)*(y**2))/(2*sigma**2))*math.cos((2*pi*x/lammbda)+psi)
		imagenSalida[:,:,0,contador] = signal.convolve2d(imagen[:,:,0], g,  boundary='symm', mode='same')
		imagenSalida[:,:,1,contador] = signal.convolve2d(imagen[:,:,1], g,  boundary='symm', mode='same')
		imagenSalida[:,:,2,contador] = signal.convolve2d(imagen[:,:,2], g,  boundary='symm', mode='same')
		imagenSalida[:,:,3,contador] = signal.convolve2d(imagen[:,:,3], g,  boundary='symm', mode='same')
		contador = contador + 1
s = matcompat.size(imagenSalida)
FM = np.zeros(s)
area = []
for i in range(s[3]):
	alpha = .6
	m1 = alpha*imagenSalida[:,:,0,k].max().max()
	m2 = alpha*imagenSalida[:,:,1,k].max().max()
	m3 = alpha*imagenSalida[:,:,2,k].max().max()
	m4 = alpha*imagenSalida[:,:,3,k].max().max()
	for i in range(s[0]):
		for j in range(s[1]):
			if imagenSalida[i,j,0,k]>m1:
				FM[i,j,0,k] = 1
			if imagenSalida[i,j,1,k]>m2:
				FM[i,j,1,k]=1
			if imagenSalida[i,j,2,k]>m3:
				FM[i,j,2,k]=1
			if imagenSalida[i,j,3,k]>m4:
				FM[i,j,3,k]=1
	[area,num] = monta.monta(FM[:,:,0,k])

cv2.imshow("input",  imagen)
cv2.waitKey(0)
cv2.destroyAllWindows()

#theta = [round(float(i)/10000000,4) for i in range(0,int((pi-pi/8)*10000000),int((pi/8)*10000000))]
#gamma = [float(i)/10 for i in range(4,11,2)]


"""Timezones lookup."""

import concurrent.futures
import os
import shutil
import subprocess
import sys
import time
import traceback
from datetime import datetime
from multiprocessing import cpu_count
from pathlib import Path

import pytz
import requests
import tzlocal
from fuzzywuzzy import process
import pycountry

import albert as v0

__title__ = "Timezones lookup"
__version__ = "0.4.0"
__triggers__ = "tz "
__authors__ = "Nikos Koukis"
__homepage__ = (
    "https://github.com/bergercookie/awesome-albert-plugins/blob/master/plugins/timezones"
)
__py_deps__ = ["pycountry", "fuzzywuzzy",
               "tzlocal", "requests", "traceback", "pytz"]

icon_path = str(Path(__file__).parent / "timezones")

cache_path = Path(v0.cacheLocation()) / "timezones"
config_path = Path(v0.configLocation()) / "timezones"
data_path = Path(v0.dataLocation()) / "timezones"
country_logos_path = data_path / "logos"
dev_mode = False

# country code -> cities
code_to_cities = dict({k: v for k, v in pytz.country_timezones.items()})
codes = list(code_to_cities.keys())
city_to_code = {vi: k for k, v in pytz.country_timezones.items() for vi in v}
cities = list(city_to_code.keys())
country_to_code = {
    c.name: c.alpha_2 for c in pycountry.countries if c.alpha_2 in codes}
country_to_cities = {
    country: [code_to_cities[code]] for country, code in country_to_code.items()
}
countries = list(country_to_code.keys())

local_tz_str = tzlocal.get_localzone().zone

def download_logo_for_code(code: str) -> bytes:
    """
    Download the logo of the given code.

    .. raises:: KeyError if given code is invalid.
    """
    # ret = requests.get(f"https://www.countryflags.io/{code}/flat/64.png")
    ret = requests.get(f"file:///64/{code}.png")
    if not ret.ok:
        print(f"[E] Couldn't download logo for code {code}")
    return ret.content

def get_logo_path_for_code(code: str) -> Path:
    """Return the path to the cached country logo"""
    return country_logos_path / f"{code}.png"

def save_logo_for_code(code: str, data: bytes):
    with open(get_logo_path_for_code(code), "wb") as f:
        f.write(data)

def download_and_save_logo_for_code(code):
    save_logo_for_code(code, download_logo_for_code(code))

def download_all_logos():
    with concurrent.futures.ThreadPoolExecutor(max_workers=cpu_count()) as executor:
        future_to_code = {
            executor.submit(download_and_save_logo_for_code, code): code for code in codes
        }
        for future in concurrent.futures.as_completed(future_to_code):
            code = future_to_code[future]
            try:
                future.result()
            except Exception as exc:
                print(
                    f"[W] Fetching logo for {code} generated an exception: {exc}")

# plugin main functions -----------------------------------------------------------------------

def initialize():
    """Called when the extension is loaded (ticked in the settings) - blocking."""

    # create plugin locations
    for p in (cache_path, config_path, data_path):
        p.mkdir(parents=False, exist_ok=True)

    # fetch all logos at startup
    country_logos_path.mkdir(exist_ok=True)
    if not list(country_logos_path.iterdir()):
        print("Downloading country logos")
        t = time.time()
        download_all_logos()
        print(f"Downloaded country logos - Took {time.time() - t} seconds")

def finalize():
    pass

def get_uniq_elements(seq):
    """Return only the unique elements off the list - Preserve the order.

    .. ref:: https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-whilst-preserving-order
    """
    seen = set()
    seen_add = seen.add
    return [x for x in seq if not (x in seen or seen_add(x))]

def handleQuery(query) -> list:
    """Hook that is called by albert with *every new keypress*."""  # noqa
    results = []

    if query.isTriggered:
        try:
            query.disableSort()

            results_setup = setup(query)
            if results_setup:
                return results_setup

            query_str = query.string.strip()

            matched = [
                elem[0] for elem in process.extract(query_str, [*cities, *countries], limit=8)
            ]

            matched2 = []
            # replace country names with its cities
            for m in matched:
                if m in countries:
                    matched2.extend(*country_to_cities[m])
                else:
                    matched2.append(m)
            matched2 = get_uniq_elements(matched2)

            # add own timezone:
            if local_tz_str in matched2:
                matched2.remove(local_tz_str)

            matched2.insert(0, local_tz_str)
            results.extend([get_as_item(m) for m in matched2])

        except Exception:  # user to report error
            if dev_mode:  # let exceptions fly!
                print(traceback.format_exc())
                raise

            results.insert(
                0,
                v0.Item(
                    id=__title__,
                    icon=icon_path,
                    text="Something went wrong! Press [ENTER] to copy error and report it",
                    actions=[
                        v0.ClipAction(
                            f"Copy error - report it to {__homepage__[8:]}",
                            f"{traceback.format_exc()}",
                        )
                    ],
                ),
            )

    return results

# supplementary functions ---------------------------------------------------------------------

def get_as_item(city: str):
    """Return an item - ready to be appended to the items list and be rendered by Albert."""
    code = city_to_code[city]

    icon = str(get_logo_path_for_code(code))
    utc_dt = pytz.utc.localize(datetime.utcnow())
    dst_tz = pytz.timezone(city)
    dst_dt = utc_dt.astimezone(dst_tz)

    text = f"{str(dst_dt)}"
    subtext = f"[{code}] | {city}"

    return v0.Item(
        id=__title__,
        icon=icon,
        text=text,
        subtext=subtext,
        completion=city,
        actions=[
            v0.UrlAction(
                "Open in zeitverschiebung.net",
                f'https://www.zeitverschiebung.net/en/timezone/{city.replace("/", "--").lower()}',
            ),
        ],
    )

def sanitize_string(s: str) -> str:
    return s.replace("<", "<")

def get_as_subtext_field(field, field_title=None) -> str:
    """Get a certain variable as part of the subtext, along with a title for that variable."""
    s = ""
    if field:
        s = f"{field} | "
    else:
        return ""

    if field_title:
        s = f"{field_title}: " + s

    return s

def save_data(data: str, data_name: str):
    """Save a piece of data in the configuration directory."""
    with open(config_path / data_name, "w") as f:
        f.write(data)

def load_data(data_name) -> str:
    """Load a piece of data from the configuration directory."""
    with open(config_path / data_name, "r") as f:
        data = f.readline().strip().split()[0]

    return data

def setup(query):
    """Setup is successful if an empty list is returned.

    Use this function if you need the user to provide you data
    """

    results = []
    return results


from django.shortcuts import render, redirect, get_object_or_404
from django.shortcuts import render, get_object_or_404
from .models import *
from .forms import *
from .models import Product
from .forms import ProductUpdateForm
from .models import Category
from django.http import JsonResponse
# libraries for Import Export
from import_export.formats import base_formats
from django.http import HttpResponse
from import_export.resources import modelresource_factory
from .resources import ProductResources
from tablib import Dataset
from reportlab.pdfgen import canvas
from import_export import resources
from import_export.resources import ModelResource

#===============================================================code for category========================================

#====== list and add category======================
def category_list(request):
    queryset = Category.objects.all()
    if request.method == "POST":       # Add Categories
        formbb = CategoryForm(request.POST or None)
        if formbb.is_valid():
            formbb.save()
        return redirect('category_list')
    else:
        formbb = CategoryForm()
    context = {
        "queryset":queryset,
        "formbb": formbb,
    }
    return render(request, "category_list.html", context)

#============= delete category===================
def delete_categorys(request, pk):
    queryset = get_object_or_404(Category, pk=pk) # used to get the product
    if request.method == 'POST':
        queryset.delete()
        return redirect('category_list')
    return redirect('category_list')

#===========================================================the for Products page==========================================

#=========== list and Add product ===========================
def product_list(request):
    queryset = Product.objects.all()
    if request.method == 'POST':    # Add Products
        formcc = ProductForm(request.POST)
        if formcc.is_valid():
            formcc.save()
            return redirect('product_list')
    else:
        formcc = ProductForm()

    queryset = Product.objects.all().order_by('product_name')
    context ={
        "queryset":queryset,
        "formcc":formcc,
    }
    return render(request, "product_list.html", context)

#====================================== Add product to main Store=======================================
def receive_products(request, code):
    queryset = Product.objects.get(code=code)
    formjj= ProductAmendForm(request.POST or None, instance=queryset)
    if formjj.is_valid():
        instance= formjj.save(commit=False)
        instance.shop_send_quantity = 0   # set the value of the "issue to shop" =0
        instance.factory_quantity += instance.receive_main_quantity   # add the received quantity from factory to the quantity in the store
        instance.first_add_main_quantity = instance.receive_main_quantity+instance.first_add_main_quantity # recording the products that are stored in main store from the first record of received itme until now
        instance.save()
        return redirect ("product_list")
    context = {
        "instance":queryset,
        "formjj":formjj,
    }
    return render(request, 'receive_products.html',context)

#===================== issue products from main store to shop store================================

def issue_products(request, code):
    queryset = Product.objects.get(code=code)
    formuu= ProductIssueForm(request.POST or None, instance=queryset)
    if formuu.is_valid():
        instance= formuu.save(commit=False)
        instance.receive_main_quantity=0
        instance.factory_quantity -= instance.shop_send_quantity
        instance.shop_receive_quantity = instance.shop_receive_quantity+ instance.shop_send_quantity
        instance.shop_remain_quantity +=instance.shop_send_quantity
        instance.save()
        return redirect ("product_list")
    context = {
        "instance":queryset,
        "formuu":formuu,
    }
    return render(request, 'issue_products.html',context)

#====================== delete products from the list====================

def delete_product(request, code):
    queryset = get_object_or_404(Product, code=code)
    if request.method == 'POST':
        queryset.delete()
        return redirect('product_list')
    return redirect('product_list')

#======================= Update the Products =================================
def update_products(request, code):
          queryset= Product.objects.get(code=code)
          formvv= ProductUpdateForm(instance = queryset)
          if request.method == 'POST':
                    formvv = ProductUpdateForm(request.POST,instance=queryset)
                    if formvv.is_valid():
                              formvv.save()
                              return redirect('product_list')
          context= {
              'formvv' : formvv
          }
          return render(request, 'update_products.html', context)

#=================================================================for the shop store =============================================
def shop_sell(request):
    return render(request, 'shop_sell.html')

def product_shop_list(request):
    queryset = Product.objects.all()
    queryset = Product.objects.all().order_by('product_name')
    context ={
        "queryset":queryset,
    }
    return render(request, "product_shop_list.html", context)

 #==================== code for the import and Export=========================================

class ProductResource(ModelResource):
    class Meta:
        model = Product

def export_pdf(request):
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="products.pdf"'
    p = canvas.Canvas(response)

    product = Product.objects.all()
    for item in product:
        p.drawString(100, 700, f'Name: {item.product_name}')
        p.drawString(100, 680, f'Description: {item.description}')
        p.showPage()

    p.save()
    return response

def export_excel(request):
    product = ProductResource().export()
    response = HttpResponse(product.xls, content_type='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename="product.xls"'
    return response

def import_excel(request):
    if request.method == 'POST':
        dataset = Dataset()
        new_data = request.FILES['myfile']

        if not new_data.name.endswith('xls'):
            messages.info(request, 'Wrong format')
            return render(request, 'import_data.html')

        imported_data = dataset.load(new_data.read(), 'xls')
        result = ProductResource().import_data(dataset, dry_run=True)  # Check if the data is valid
        if not result.has_errors():
            ProductResource().import_data(dataset, dry_run=False)  # Import the actual data
            messages.success(request, 'Data imported successfully')

    return render(request, 'import_data.html')

def export_import(request):
    return render(request, 'product_list.html')


from pymongo.collection import Collection
from bson.objectid import ObjectId

def insert_object(obj: dict, collection: Collection):
    """Вставка объекта в коллекцию"""
    obj['fields'] = list(obj['fields'].items())
    return collection.insert_one(obj).inserted_id

def delete_object(object_id: str, collection: Collection):
    """Удаление объекта из коллекции"""
    collection.delete_one({"_id": ObjectId(object_id)})

def get_object(object_id: str, collection: Collection):
    """Получение объекта из коллекции по id"""
    obj = collection.find_one({"_id": ObjectId(object_id)})
    if obj is not None:
        obj['fields'] = dict(obj['fields'])
    return obj

def get_objects(
        page_size: int,
        page_number: int,
        collection: Collection
) -> list[dict]:
    """
    Получение объектов из коллекции
    :param page_size: Размер страницы
    :param page_number: Номер страницы
    :param collection: Коллекция MongoDB
    :return: Список объектов
    """
    result = []
    for obj in collection.find({}).limit(page_size).skip((page_number - 1) * page_size):
        obj['fields'] = dict(obj['fields'])
        result.append(obj)
    return result


import numpy as np
import cv2
import time

def my_padding(src, filter):
    (h, w) = src.shape
    if isinstance(filter, tuple):
        (h_pad, w_pad) = filter
    else:
        (h_pad, w_pad) = filter.shape
    h_pad = h_pad // 2
    w_pad = w_pad // 2
    padding_img = np.zeros((h+h_pad*2, w+w_pad*2))
    padding_img[h_pad:h+h_pad, w_pad:w+w_pad] = src

    # repetition padding
    # up
    padding_img[:h_pad, w_pad:w_pad + w] = src[0, :]
    # down
    padding_img[h_pad + h:, w_pad:w_pad + w] = src[h - 1, :]
    # left
    padding_img[:, :w_pad] = padding_img[:, w_pad:w_pad + 1]
    # right
    padding_img[:, w_pad + w:] = padding_img[:, w_pad + w - 1:w_pad + w]

    return padding_img

def my_filtering(src, filter):
    (h, w) = src.shape
    (f_h, f_w) = filter.shape

    #filter 확인
    #print('<filter>')
    #print(filter)

    # 직접 구현한 my_padding 함수를 이용
    pad_img = my_padding(src, filter)

    dst = np.zeros((h, w))
    for row in range(h):
        for col in range(w):
            dst[row, col] = np.sum(pad_img[row:row + f_h, col:col + f_w] * filter)

    return dst

def get_my_sobel():
    sobel_x = np.dot(np.array([[1], [2], [1]]), np.array([[-1, 0, 1]]))
    sobel_y = np.dot(np.array([[-1], [0], [1]]), np.array([[1, 2, 1]]))
    return sobel_x, sobel_y

def calc_derivatives(src):
    # calculate Ix, Iy
    sobel_x, sobel_y = get_my_sobel()
    Ix = my_filtering(src, sobel_x)
    Iy = my_filtering(src, sobel_y)
    return Ix, Iy

def find_local_maxima(src, ksize):
    (h, w) = src.shape
    pad_img = np.zeros((h+ksize, w+ksize))
    pad_img[ksize//2:h+ksize//2, ksize//2:w+ksize//2] = src
    dst = np.zeros((h, w))

    for row in range(h):
        for col in range(w):
            max_val = np.max(pad_img[row : row+ksize, col:col+ksize])
            if max_val == 0:
                continue
            if src[row, col] == max_val:
                dst[row, col] = src[row, col]

    return dst

def get_integral_image(src):
    assert len(src.shape) == 2
    h, w = src.shape
    dst = np.zeros(src.shape)
    ##############################
    # ToDo
    # dst는 integral image
    # dst 알아서 채우기
    ##############################
    integral_image = dst
    for row in range(0, h):
        summation = 0
        for col in range(0, w):
            summation += src[row][col]
            integral_image[row][col] = summation

            if row > 0:
                integral_image[row][col] += integral_image[row - 1][col]

    # dst = integral_image
    # return dst

    dst2 = np.zeros(src.shape)

    for y in range(h):
        for x in range(w):

            min_row, max_row = max(0, y - 1), min(h - 1, y + 1)
            min_col, max_col = max(0, x - 1), min(w - 1, x + 1)

            dst2[y][x] = integral_image[max_row][max_col]

            if min_row > 0:
                dst2[y][x] -= integral_image[min_row - 1][max_col]

            if min_col > 0:
                dst2[y][x] -= integral_image[max_row][min_col - 1]

            if min_col > 0 and min_row > 0:
                dst2[y][x] += integral_image[min_row - 1][min_col - 1]

    return dst2

def calc_M_harris(IxIx, IxIy, IyIy, fsize = 5):
    assert IxIx.shape == IxIy.shape and IxIx.shape == IyIy.shape
    h, w = IxIx.shape
    M = np.zeros((h, w, 2, 2))
    IxIx_pad = my_padding(IxIx, (fsize, fsize))
    IxIy_pad = my_padding(IxIy, (fsize, fsize))
    IyIy_pad = my_padding(IyIy, (fsize, fsize))

    # for row in range(h):
    #     for col in range(w):
    #         M[row, col, 0, 0] = np.sum(IxIx_pad[row:row+fsize, col:col+fsize])
    #         M[row, col, 0, 1] = np.sum(IxIy_pad[row:row+fsize, col:col+fsize])
    #         M[row, col, 1, 0] = M[row, col, 0, 1]
    #         M[row, col, 1, 1] = np.sum(IyIy_pad[row:row+fsize, col:col+fsize])

    for row in range(h):
        for col in range(w):
            ixix = 0
            ixiy = 0
            iyiy = 0
            for f_row in range(fsize):
                for f_col in range(fsize):
                    ixix = ixix + IxIx_pad[row + f_row][col + f_col]
                    ixiy = ixiy + IxIy_pad[row + f_row][col + f_col]
                    iyiy = iyiy + IyIy_pad[row + f_row][col + f_col]
            M[row, col, 0, 0] = ixix
            M[row, col, 0, 1] = ixiy
            M[row, col, 1, 0] = ixiy
            M[row, col, 1, 1] = iyiy

    return M

def harris_detector(src, k = 0.04, threshold_rate = 0.01, fsize=5):
    harris_img = src.copy()
    h, w, c = src.shape
    gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) / 255.
    # calculate Ix, Iy
    Ix, Iy = calc_derivatives(gray)

    # Square of derivatives
    IxIx = Ix**2
    IyIy = Iy**2
    IxIy = Ix * Iy

    start = time.perf_counter()  # 시간 측정 시작
    M_harris = calc_M_harris(IxIx, IxIy, IyIy, fsize)
    end = time.perf_counter()  # 시간 측정 끝
    print('M_harris time : ', end-start)

    R = np.zeros((h, w))
    for row in range(h):
        for col in range(w):
            ##########################################################################
            # ToDo
            # det_M 계산
            # trace_M 계산
            # R 계산 Harris & Stephens (1988), Nobel (1998) 어떤걸로 구현해도 상관없음
            ##########################################################################
            det_M = M_harris[row, col, 0, 0] * M_harris[row, col, 1, 1] - (M_harris[row, col, 0, 1] * M_harris[row, col, 1, 0])
            trace_M = M_harris[row, col, 0, 0] + M_harris[row, col, 1, 1]
            R[row, col] = det_M - k*trace_M*trace_M

    # thresholding
    R[R < threshold_rate * np.max(R)] = 0

    R = find_local_maxima(R, 21)
    R = cv2.dilate(R, None)

    harris_img[R != 0]=[0, 0, 255]

    return harris_img

def harris_detector_integral(src, k = 0.04, threshold_rate = 0.01, fsize=5):
    harris_img = src.copy()
    h, w, c = src.shape
    gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) / 255.
    # calculate Ix, Iy
    Ix, Iy = calc_derivatives(gray)

    # Square of derivatives
    IxIx = Ix**2
    IyIy = Iy**2
    IxIy = Ix * Iy

    start = time.perf_counter()  # 시간 측정 시작
    IxIx_integral = get_integral_image(IxIx)
    IxIy_integral = get_integral_image(IxIy)
    IyIy_integral = get_integral_image(IyIy)
    end = time.perf_counter()  # 시간 측정 끝

    print('make integral image time : ', end-start)

    start = time.perf_counter()  # 시간 측정 시작
    ##############################
    # ToDo
    # M_integral 완성시키기
    ##############################
    M_integral = calc_M_harris(IxIx_integral, IxIy_integral, IyIy_integral, fsize)
    end = time.perf_counter()  # 시간 측정 끝
    print('M_harris integral time : ', end-start)

    R = np.zeros((h, w))
    for row in range(h):
        for col in range(w):
            ##########################################################################
            # ToDo
            # det_M 계산
            # trace_M 계산
            # R 계산 Harris & Stephens (1988), Nobel (1998) 어떤걸로 구현해도 상관없음
            ##########################################################################
            det_M = M_integral[row, col, 0, 0] * M_integral[row, col, 1, 1] - (M_integral[row, col, 0, 1] * M_integral[row, col, 1, 0])
            trace_M = M_integral[row, col, 0, 0] + M_integral[row, col, 1, 1]
            R[row, col] = det_M - k * trace_M * trace_M

    # thresholding
    R[R < threshold_rate * np.max(R)] = 0

    R = find_local_maxima(R, 21)
    R = cv2.dilate(R, None)

    harris_img[R != 0]=[0, 0, 255]

    return harris_img

def main():
    src = cv2.imread('zebra.png') # shape : (552, 435, 3)
    print('start!')
    cv2.imshow('original ', src)
    harris_img = harris_detector(src)
    cv2.imshow('harris_img ' + '201402414', harris_img)

    harris_integral_img = harris_detector_integral(src)
    cv2.imshow('harris_integral_img ' + '201402414' , harris_integral_img)
    cv2.waitKey()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()


from unittest.runner import TextTestRunner
import urllib.request
import unittest
from typing import TypeVar, Callable, List

T = TypeVar('T')
S = TypeVar('S')

#################################################################################
# EXERCISE 1
#################################################################################
def mysort(lst: List[T], compare: Callable[[T, T], int]) -> List[T]:
    """
    This method should sort input list lst of elements of some type T.

    Elements of the list are compared using function compare that takes two
    elements of type T as input and returns -1 if the left is smaller than the
    right element, 1 if the left is larger than the right, and 0 if the two
    elements are equal.
    """
    temp = lst
    switched = True
    while switched:
        switched = False
        for i in range(len(temp) - 1):
            if compare(temp[i], temp[i + 1]) == 1:
                temp[i], temp[i + 1] = temp[i + 1], temp[i]
                switched = True

    return temp

def mybinsearch(lst: List[T], elem: S, compare: Callable[[T, S], int]) -> int:
    """
    This method search for elem in lst using binary search.

    The elements of lst are compared using function compare. Returns the
    position of the first (leftmost) match for elem in lst. If elem does not
    exist in lst, then return -1.
    """
    def binsearch(ar, targ, start, end):
        if start > end:
            return -1

        mid = (start + end) // 2
        if compare(ar[mid], targ) == 0:
            return mid

        if compare(ar[mid], targ) == 1:
            return binsearch(ar, targ, start, mid-1)
        else:
            return binsearch(ar, targ, mid+1, end)

    return binsearch(lst, elem, 0, len(lst)-1)

class Student():
    """Custom class to test generic sorting and searching."""
    def __init__(self, name: str, gpa: float):
        self.name = name
        self.gpa = gpa

    def __eq__(self, other):
        return self.name == other.name

# 30 Points (total)
def test1():
    """Tests for generic sorting and binary search."""
    print(80 * "#" + "\nTests for generic sorting and binary search.")
    test1_1()
    test1_2()
    test1_3()
    test1_4()
    test1_5()

# 6 Points
def test1_1():
    """Sort ints."""
    print("\t-sort ints")
    tc = unittest.TestCase()
    ints = [ 4, 3, 7, 10, 9, 2 ]
    intcmp = lambda x,y:  0 if x == y else (-1 if x < y else 1)
    sortedints = mysort(ints, intcmp)
    tc.assertEqual(sortedints, [2, 3, 4, 7, 9, 10])

# 6 Points
def test1_2():
    """Sort strings based on their last character."""
    print("\t-sort strings on their last character")
    tc = unittest.TestCase()
    strs = [ 'abcd', 'aacz',  'zasa' ]
    suffixcmp = lambda x,y: 0 if x[-1] == y[-1] else (-1 if x[-1] < y[-1] else 1)
    sortedstrs = mysort(strs,suffixcmp)
    tc.assertEqual(sortedstrs, [ 'zasa', 'abcd', 'aacz' ])

# 6 Points
def test1_3():
    """Sort students based on their GPA."""
    print("\t-sort students on their GPA.")
    tc = unittest.TestCase()
    students = [ Student('Josh', 3.0), Student('Angela', 2.5), Student('Vinesh', 3.8),  Student('Jia',  3.5) ]
    sortedstudents = mysort(students, lambda x,y: 0 if x.gpa == y.gpa else (-1 if x.gpa < y.gpa else 1))
    expected = [ Student('Angela', 2.5), Student('Josh', 3.0), Student('Jia',  3.5), Student('Vinesh', 3.8) ]
    tc.assertEqual(sortedstudents, expected)

# 6 Points
def test1_4():
    """Binary search for ints."""
    print("\t-binsearch ints")
    tc = unittest.TestCase()
    ints = [ 4, 3, 7, 10, 9, 2 ]
    intcmp = lambda x,y:  0 if x == y else (-1 if x < y else 1)
    sortedints = mysort(ints, intcmp)
    tc.assertEqual(mybinsearch(sortedints, 3, intcmp), 1)
    tc.assertEqual(mybinsearch(sortedints, 10, intcmp), 5)
    tc.assertEqual(mybinsearch(sortedints, 11, intcmp), -1)

# 6 Points
def test1_5():
    """Binary search for students by gpa."""
    print("\t-binsearch students")
    tc = unittest.TestCase()
    students = [ Student('Josh', 3.0), Student('Angela', 2.5), Student('Vinesh', 3.8),  Student('Jia',  3.5) ]
    stcmp = lambda x,y: 0 if x.gpa == y.gpa else (-1 if x.gpa < y.gpa else 1)
    stbincmp = lambda x,y: 0 if x.gpa == y else (-1 if x.gpa < y else 1)
    sortedstudents = mysort(students, stcmp)
    tc.assertEqual(mybinsearch(sortedstudents, 3.5, stbincmp), 2)
    tc.assertEqual(mybinsearch(sortedstudents, 3.7, stbincmp), -1)

#################################################################################
# EXERCISE 2
#################################################################################
class PrefixSearcher():

    def __init__(self, document, k):
        """
        Initializes a prefix searcher using a document and a maximum
        search string length k.
        """
        self.strings = []
        for x in range(0, len(document) - 1):
            if x + k < len(document):
                self.strings.append(document[x: x + k])
            else:
                self.strings.append(document[x: len(document)])

        comp = lambda x,y:  0 if len(x) == len(y) else (-1 if len(x) > len(y) else 1)
        self.strings = mysort(self.strings, comp)

    def search(self, q):
        """
        Return true if the document contains search string q (of
        length up to n). If q is longer than n, then raise an
        Exception.
        """
        for x in self.strings:
            if q in x:
                return True

        return False

        pass

# 30 Points
def test2():
    print("#" * 80 + "\nSearch for substrings up to length n")
    test2_1()
    test2_2()

# 15Points
def test2_1():
    print("\t-search in hello world")
    tc = unittest.TestCase()
    p = PrefixSearcher("Hello World!", 1)
    tc.assertTrue(p.search("l"))
    tc.assertTrue(p.search("e"))
    tc.assertFalse(p.search("h"))
    tc.assertFalse(p.search("Z"))
    tc.assertFalse(p.search("Y"))
    p = PrefixSearcher("Hello World!", 2)
    tc.assertTrue(p.search("l"))
    tc.assertTrue(p.search("ll"))
    tc.assertFalse(p.search("lW"))

# 20 Points
def test2_2():
    print("\t-search in Moby Dick")
    tc = unittest.TestCase()
    md_url = 'https://www.gutenberg.org/files/2701/2701-0.txt'
    md_text = urllib.request.urlopen(md_url).read().decode()
    p = PrefixSearcher(md_text[0:1000],4)
    tc.assertTrue(p.search("Moby"))
    tc.assertTrue(p.search("Dick"))

#################################################################################
# EXERCISE 3
#################################################################################
class SuffixArray():

    def __init__(self, document: str):
        """
        Creates a suffix array for document (a string).
        """

        comp = lambda x,y:  0 if x == y else (-1 if x < y else 1)

        self.sa = mysort([document[i:] for i in range(len(document))], comp)
        pass

    def positions(self, searchstr: str):
        """
        Returns all the positions of searchstr in the documented indexed by the suffix array.
        """
        out = []
        for x in range(0, len(self.sa)):
            sub = self.sa[x]
            if searchstr == sub[0:len(searchstr)]:
                out.append(x)
                return out

        pass

    def contains(self, searchstr: str):
        """
        Returns true of searchstr is coontained in document.
        """
        for x in self.sa:
            if searchstr in x:
                return True
        pass

# 40 Points
def test3():
    """Test suffix arrays."""
    print(80 * "#" + "\nTest suffix arrays.")
    test3_1()
    test3_2()

# 20 Points
def test3_1():
    print("\t-suffixarray on Hello World!")
    tc = unittest.TestCase()
    s = SuffixArray("Hello World!")
    tc.assertTrue(s.contains("l"))
    tc.assertTrue(s.contains("e"))
    tc.assertFalse(s.contains("h"))
    tc.assertFalse(s.contains("Z"))
    tc.assertFalse(s.contains("Y"))
    tc.assertTrue(s.contains("ello Wo"))

# 20 Points
def test3_2():
    print("\t-suffixarray on Moby Dick!")
    tc = unittest.TestCase()
    md_url = 'https://www.gutenberg.org/files/2701/2701-0.txt'
    md_text = urllib.request.urlopen(md_url).read().decode()
    s = SuffixArray(md_text[0:1000])
    tc.assertTrue(s.contains("Moby-Dick"))
    tc.assertTrue(s.contains("Herman Melville"))
    posset = set(s.positions("Moby-Dick"))
    tc.assertEqual(posset, {355, 356})

#################################################################################
# TEST CASES
#################################################################################
def main():
    test1()
    test2()
    test3()

if __name__ == '__main__':
    main()


from mlearn import base
from functools import reduce
from datetime import datetime
from mlearn.data.dataset import GeneralDataset
from mlearn.data.batching import Batch, BatchExtractor
from sklearn.feature_extraction import DictVectorizer
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer

def process_and_batch(dataset: GeneralDataset, data: base.DataType, batch_size: int, onehot: bool = True,
                      shuffle: bool = False, **kwargs):
    """
    Process a dataset and data.

    :dataset (GeneralDataset): The dataset object to use for processing.
    :data (base.DataType): The data to be batched and processed.
    :batch_size (int): Size of batches to create.
    :returns: Batched data.
    """
    # Process labels and encode data.
    dataset.process_labels(data)

    # Batch data
    batch = Batch(batch_size, data)
    batch.create_batches()
    batches = BatchExtractor('label', batch, dataset, onehot)

    if shuffle:
        batches.shuffle()

    return batches

def get_deep_dict_value(source: dict, keys: str, default = None):
    """
    Get values from deeply nested dicts.

    :source (dict): Dictionary to get data from.
    :keys (str): Keys split by '|'. E.g. outerkey|middlekey|innerkey.
    :default: Default return value.
    """
    value = reduce(lambda d, key: d.get(key, default) if isinstance(d, dict) else default, keys.split("|"), source)
    return value

def select_vectorizer(vectorizer: str = 'dict') -> base.VectType:
    """
    Identify vectorizer used and return it to be used.

    :vectorizer, default = 'dict': Vectorizer to be used.
    :return v: Vectorizer function.
    """
    vect = vectorizer.lower()
    if 'dict' in vect:
        v = DictVectorizer()
        setattr(v, 'name', 'DictVectorizer')
    elif 'tfidf' in vect:
        v = TfidfVectorizer()
        setattr(v, 'name', 'TFIDF-Vectorizer')
    elif 'count' in vect:
        v = CountVectorizer()
        setattr(v, 'name', 'CountVectorizer')
    setattr(v, 'fitted', False)

    return v

def _get_datestr():
    return datetime.now().strftime('%Y.%m.%d.%H.%M.%S')

def hyperparam_space(search_space: base.List[dict], hyper_parameters: base.List[base.Tuple]
                     ) -> base.List[dict]:
    """
    Create all hyper-parameter combinations to run.

    :search_space (base.List[dict]): List of dictionaries with one value
    :hyper_parameters (base.List[dict]): List of tuples containing a dict with all values for each iteration.
    :returns search_space (base.Generator[dict]): A list of dictionaries containing the search space.
    """
    for param_name, param_space in hyper_parameters:
        additions = []
        for comb_dict in search_space:
            for param in param_space:
                additions.append({**comb_dict, **{param_name: param}})
        search_space = additions
    return search_space


#!/user/bin/env python
# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import h5py
#一个HDF5文件就是一个容器，用于储存两类对象：datasets，类似于数组的数据集合；groups，类似于文件夹的容器，可以储存datasets和其它groups。
from lr_utils import load_dataset
train_set_x_orig , train_set_y , test_set_x_orig , test_set_y , classes = load_dataset()
# index = 30
# print(train_set_x_orig[index])
# plt.imshow(train_set_x_orig[index])

#打印出当前的训练标签值
#使用np.squeeze的目的是压缩维度，【未压缩】train_set_y[:,index]的值为[1] , 【压缩后】np.squeeze(train_set_y[:,index])的值为1
#print("【使用np.squeeze：" + str(np.squeeze(train_set_y[:,index])) + "，不使用np.squeeze： " + str(train_set_y[:,index]) + "】")
#只有压缩后的值才能进行解码操作
# print("train_set_y=" +str(train_set_y[:,index]))
# print(classes[np.squeeze(train_set_y[:,index])])
# plt.show()
#image.shape[0],image.shape[1],image.shape[2]表示图像长，宽，通道数  image.shape表示图片的维度
m_train=train_set_y.shape[1]
m_test=test_set_y.shape[1]
num_px=train_set_x_orig.shape[1]

print("训练集的数量：m_train="+str(m_train))
print("测试集的数量：m_test="+str(m_test))
print("每张图片的高和宽：num_px="+str(num_px))
print("每张图片的大小：（"+str(num_px)+","+str(num_px)+",3)")
print("训练集图片的维度："+str(train_set_x_orig.shape))
print("训练集标签的维度："+str(train_set_y.shape))
print("测试集图片的维度："+str(test_set_x_orig.shape))
print("测试集标签的维度："+str(test_set_y.shape))
#X_flatten = X.reshape(X.shape [0]，-1).T ＃X.T是X的转置
#将训练集的维度降低并转置。这里的-1被理解为unspecified value，意思是未指定为给定的。如果我只需要特定的行数，列数多少我无所谓，我只需要指定行数，那么列数直接用-1代替就行了，计算机帮我们算赢有多少列，反之亦然。
#如果是reshape(5,-1) 就是将数组变为5行的矩阵，列的话根据具体的来分

train_set_x_flatten  = train_set_x_orig.reshape(train_set_x_orig.shape[0],-1).T
#将测试集的维度降低并转置。
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T
#
# print ("训练集降维最后的维度： " + str(train_set_x_flatten.shape))
# print ("训练集_标签的维数 : " + str(train_set_y.shape))
# print ("测试集降维之后的维度: " + str(test_set_x_flatten.shape))
# print ("测试集_标签的维数 : " + str(test_set_y.shape))
train_set_x = train_set_x_flatten / 255
test_set_x = test_set_x_flatten / 255

def sigmoid(z):
    """

    :param z: 任意大小的标量或者numpy数组
    :return:
    """
    s=1/(1+np.exp(-z))
    return s

def initialize_with_zero(dim):
    """
    此函数为w创建一个维度为（dim，1）的0向量，并将b初始化为0,w b都被初始化为0
    :param dim:想要的w的大小
    :return:w-维度为（dim,1)的初始化向量  b-初始化的标量
    """
    w=np.zeros(shape=(dim,1))
    b=0
    assert (w.shape==(dim,1))#assert 表示如果出错则终止程序，断言函数是对表达式布尔值的判断，要求表达式计算值必须为真。如果表达式为假，触发异常；如果表达式为真，不执行任何操作。
    assert (isinstance(b,float)or isinstance(b,int))#isinstance() 函数来判断一个对象是否是一个已知的类型，类似 type()。
    return (w,b)

def propagate(w,b,X,Y):
    """

    :param w:权重，大小不等的数组（num_px * num_px * 3，1）
    :param b:偏差，一个标量
    :param X:矩阵类型为（num_px * num_px * 3，训练数量）
    :param Y: 真正的“标签”矢量（如果非猫则为0，如果是猫则为1），矩阵维度为(1,训练数据数量)
    :return:  cost- 逻辑回归的负对数似然成本
        dw  - 相对于w的损失梯度，因此与w相同的形状
        db  - 相对于b的损失梯度，因此与b的形状相同
    """
    m=X.shape[1]  #X=np.array([[1,2,4,5], [3,4,6,1]]),X.shape[0]=2,X.shape[1]=4
    #正向传播
    A=sigmoid(np.dot(w.T,X)+b)
    cost=(-1/m)*(np.sum(Y*np.log(A)+(1-Y)*np.log(1-A)))

    #反向传播
    dw=(1/m)*(np.dot(X,(A-Y).T))
    db=(1/m)*(np.sum(A-Y))
    # 使用断言确保我的数据是正确的
    assert (dw.shape==w.shape)
    assert (db.dtype==float)
    cost=np.squeeze(cost)#只有一行或一列的维度（a singleton dimension）被去除掉了

    assert (cost.shape==())
    grads={
        "dw":dw,
        "db":db
    }
    return (grads,cost)
# #测试一下propagate
# print("====================测试propagate====================")
# #初始化一些参数
# w, b, X, Y = np.array([[1], [2]]), 2, np.array([[1,2], [3,4]]), np.array([[1, 0]])
# grads, cost = propagate(w, b, X, Y)
# print ("dw = " + str(grads["dw"]))
# print ("db = " + str(grads["db"]))
# print ("cost = " + str(cost))
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost=False):
    """
    此函数通过运行梯度下降算法来优化w和b

    参数：
        w  - 权重，大小不等的数组（num_px * num_px * 3，1）
        b  - 偏差，一个标量
        X  - 维度为（num_px * num_px * 3，训练数据的数量）的数组。
        Y  - 真正的“标签”矢量（如果非猫则为0，如果是猫则为1），矩阵维度为(1,训练数据的数量)
        num_iterations  - 优化循环的迭代次数
        learning_rate  - 梯度下降更新规则的学习率
        print_cost  - 每100步打印一次损失值

    返回：
        params  - 包含权重w和偏差b的字典
        grads  - 包含权重和偏差相对于成本函数的梯度的字典
        成本 - 优化期间计算的所有成本列表，将用于绘制学习曲线。

    提示：
    我们需要写下两个步骤并遍历它们：
        1）计算当前参数的成本和梯度，使用propagate（）。
        2）使用w和b的梯度下降法则更新参数。
    """
    costs = []

    for i in range(num_iterations):
        grads, cost = propagate(w, b, X, Y)

        dw = grads["dw"]
        db = grads["db"]

        w = w - learning_rate * dw
        b = b - learning_rate * db

        # 记录成本
        if i % 100 == 0:
            costs.append(cost)
        # 打印成本数据
        if (print_cost) and (i % 100 == 0):
            print("迭代的次数: %i ， 误差值： %f" % (i, cost))

    params = {
            "w": w,
            "b": b}
    grads = {
            "dw": dw,
            "db": db}
    return (params, grads, costs)

# #测试optimize
# print("====================测试optimize====================")
# w, b, X, Y = np.array([[1], [2]]), 2, np.array([[1,2], [3,4]]), np.array([[1, 0]])
# params , grads , costs = optimize(w , b , X , Y , num_iterations=100 , learning_rate = 0.009 , print_cost = False)
# print ("w = " + str(params["w"]))
# print ("b = " + str(params["b"]))
# print ("dw = " + str(grads["dw"]))
# print ("db = " + str(grads["db"]))

def predict(w, b, X):
    """
        使用学习逻辑回归参数logistic （w，b）预测标签是0还是1，
        参数：
            w  - 权重，大小不等的数组（num_px * num_px * 3，1）
            b  - 偏差，一个标量
            X  - 维度为（num_px * num_px * 3，训练数据的数量）的数据
        返回：
            Y_prediction  - 包含X中所有图片的所有预测【0 | 1】的一个numpy数组（向量）
        """
    m = X.shape[1]  # 图片的数量????为什么是图片的数量
    """
    shape函数是numpy.core.fromnumeric中的函数，它的功能是读取矩阵的长度，
    比如shape[0]就是读取矩阵第一维度的长度。
    shape的输入参数可以是一个整数（表示维度），也可以是一个矩阵。
    """
    Y_prediction = np.zeros((1, m))
    w = w.reshape(X.shape[0], 1)  # reshape函数：改变数组的维数

    # 计预测猫在图片中出现的概率
    A = sigmoid(np.dot(w.T, X) + b)
    for i in range(A.shape[1]):
        # 将概率a[0,i]转换为实际预测p[0,i]
        Y_prediction[0, i] = 1 if A[0, i] > 0.5 else 0
        # 使用断言
    assert (Y_prediction.shape == (1, m))
    return Y_prediction

#
# #测试predict
# print("------测试predict------")
# w,b,X,Y = np.array([[1],[2]]),2,np.array([[1,2],[3,4]]),np.array([[1,0]])
# print("predictions = " + str(predict(w,b,X)))
def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False):
    """
    通过调用之前实现的函数来构建逻辑回归模型
    参数：
        X_train  - numpy的数组,维度为（num_px * num_px * 3，m_train）的训练集
        Y_train  - numpy的数组,维度为（1，m_train）（矢量）的训练标签集
        X_test   - numpy的数组,维度为（num_px * num_px * 3，m_test）的测试集
        Y_test   - numpy的数组,维度为（1，m_test）的（向量）的测试标签集
        num_iterations  - 表示用于优化参数的迭代次数的超参数
        learning_rate  - 表示optimize（）更新规则中使用的学习速率的超参数
        print_cost  - 设置为true以每100次迭代打印成本
    返回：
        d  - 包含有关模型信息的字典。
    """
    w, b = initialize_with_zero(X_train.shape[0])
    parameters, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)

    # 从字典“参数”中检索参数w和b
    w, b = parameters["w"], parameters["b"]

    # 预测测试/训练集的例子
    Y_prediction_test = predict(w, b, X_test)
    Y_prediction_train = predict(w, b, X_train)

    # 打印训练后的准确性
    print("训练集准确性：", format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100), "%")  # mean()函数功能：求取均值,np.abs()返回决定值
    print("测试集准确性：", format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100), "%")  # abs() 函数返回数字的绝对值

    d = {
        "costs": costs,
        "Y_prediction_test": Y_prediction_test,
        "Y_prediction_train": Y_prediction_train,
        "w": w,
        "b": b,
        "learning_rate": learning_rate,
        "num_iterations": num_iterations}
    return d

print("------测试model------")
# 这里加载的是真实的数据，请参见上面的代码部分
d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations=2000, learning_rate=0.005, print_cost=True)

# 绘制图
costs = np.squeeze(d['costs'])
"""
squeeze（）函数的用法:
在机器学习和深度学习中，通常算法的结果是可以表示向量的数组（即包含两对或以上的方括号形式[[]]），
如果直接利用这个数组进行画图可能显示界面为空（见后面的示例）。我们可以利用squeeze（）函数将表示向量
的数组转换为秩为1的数组，这样利用matplotlib库函数画图时，就可以正常的显示结果了。
"""
plt.plot(costs)
plt.ylabel('cost')
plt.xlabel('iterations(per hundreds)')
plt.title("Learning rate = " + str(d["learning_rate"]))
plt.show()

learning_rates = [0.01, 0.001, 0.0001]
models = {}
for i in learning_rates:
    print("learning rate is:" + str(i))
    models[str(i)] = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations=1500, learning_rate=i,
                           print_cost=False)
    print('\n' + "--------------" + '\n')

for i in learning_rates:
    plt.plot(np.squeeze(models[str(i)]["costs"]), label=str(models[str(i)]["learning_rate"]))

plt.ylabel('cost')
plt.xlabel('iterations')

legend = plt.legend(loc='upper center', shadow=True)  # loc:图例所有figure位置;shadow:控制是否在图例后面画一个阴影
# 设置图例legend背景颜色
frame = legend.get_frame()
frame.set_facecolor('0.90')
plt.show()


'''
@Author: Never
@Date: 2020-06-13 11:02:05
@Description:
@LastEditTime: 2020-07-14 15:20:19
@LastEditors: Never
'''
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/4/27 19:47
# @Author  : Shark
# @Site    :
# @File    : lepin1.py
# @Software: PyCharm

import csv
import requests
import json
import random
import time

start =time.time()
print('程序开始时间：%s'%(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(start))))

WinStatuslist=[]
i=0
with open('C:\\Users\\lhx\\Desktop\\user1.csv','rt') as myfile:
    lines=csv.reader(myfile)
    for memberid,addid in lines:
        with open(r'\\192.168.0.200\shop.h5\MemberId.json', 'w') as m:
            m.write(memberid)
        t=0
        while t<20:
            date={
            "productid":216869,
            "lpTimes":1,
            "addressid":addid,
            "isPay":'true',
            "useBalance":10.9,
            }

            url = "http://192.168.0.200:818/order/ActivityOrderConfirm"
            response = requests.post(url,data=date)
            text=response.text
            jsonobj=json.loads(text)

            if jsonobj['success']==200:
                totext=jsonobj['data']['OrderIdList']
                url="http://192.168.0.200:818/HappyOrder/ForthWithOrder"
                data={"happyOrderId":totext,
                "chooseNumber":random.randint(0,9)}

                response=requests.post(url,data=data)
                text=response.text
                jsonobj=json.loads(text)

                totext=jsonobj['data']['WinStatus']
                WinStatuslist.append(totext)
            else:
                print(jsonobj)
                break
            t+=1
            # time.sleep(1)
        i+=1
        print(i)

m=0
print(WinStatuslist)
for j in WinStatuslist:
    if j==1:
        m+=1
i=20*i
print("订单数：%s"%i)
print("中奖次数：%s"%m)
s=m/i*100
print('中奖概率：{:.2f}%'.format(s))
end =time.time()
print('程序结束时间：%s'%(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(end))))
print("循环运行时间:%.2f秒"%(end-start))


import matplotlib.pyplot as plt
import librosa
import librosa.display
import os
import torch
from torch.distributions.beta import Beta
import numpy as np
from pytorch_lightning.callbacks import Callback
import torch.nn as nn
from einops import rearrange
from tqdm import tqdm

from helpers import nessi

image_folder = "images"
os.makedirs(image_folder, exist_ok=True)

class MyStaticPostQuantizationCallback(Callback):
    def __init__(self, get_calibration_loader, calibration_batches=100):
        self.calibration_loader = get_calibration_loader()
        self.calibration_batches = calibration_batches

    def quantize_model(self, pl_module):
        print("*********** Before Quantization: ***********")
        if hasattr(pl_module, 'mel'):
            pl_module.mel.cpu()

        # get the shape of spectrograms
        sample = next(iter(self.calibration_loader))[0][0].unsqueeze(0)
        sample = sample[:, :, :sample.size(2) // 10]
        shape = pl_module.mel_forward(sample).size()

        # get original macs and params
        macc_orig, n_params_orig = nessi.get_model_size(pl_module.net, input_size=(1, shape[1], shape[2], shape[3]))
        print("macc_orig: ", macc_orig)
        print("n_params_orig: ", n_params_orig)

        # print size of model before quantization
        print_size_of_model(pl_module.net)
        pl_module.net.fuse_model()

        # get macs and params after fusing model
        macc, n_params = nessi.get_model_size(
            pl_module.net, input_size=(1, shape[1], shape[2], shape[3]))
        print("macc after fuse : ", macc)
        print("n_params after fuse: ", n_params)

        pl_module.net.qconfig = torch.quantization.get_default_qconfig('fbgemm')
        torch.quantization.prepare(pl_module.net, inplace=True)

        pl_module.net.cpu()
        if hasattr(pl_module, 'mel'):
            pl_module.mel.cpu()
        for i, batch in enumerate(tqdm(self.calibration_loader, total=self.calibration_batches)):
            x, files, y, device_indices, cities, indices = batch
            # split to 1-second pieces
            x = rearrange(x, 'b c (slices t) -> (b slices) c t', slices=10)
            x = x.cpu()
            if hasattr(pl_module, 'mel'):
                x = pl_module.mel_forward(x)

            with torch.no_grad():
                pl_module.net(x)
            # stop after a certain number of calibration samples
            if i == self.calibration_batches:
                break

        torch.quantization.convert(pl_module.net, inplace=True)
        print("*********** After Quantization: ***********")
        return dict(macc_orig=macc_orig, n_params_orig=n_params_orig,
          macc_fuse=macc, n_params_fuse=n_params, model_size_bytes=print_size_of_model(pl_module.net))

    def on_test_start(self, trainer, pl_module):
        self.quantize_model(pl_module)

def mixstyle(x, p=0.5, alpha=0.1, eps=1e-6):
    if np.random.rand() > p:
        return x
    batch_size = x.size(0)

    # changed from dim=[2,3] to dim=[1,3] from channel-wise statistics to frequency-wise statistics
    f_mu = x.mean(dim=[1, 3], keepdim=True)
    f_var = x.var(dim=[1, 3], keepdim=True)

    f_sig = (f_var + eps).sqrt()  # compute instance standard deviation
    f_mu, f_sig = f_mu.detach(), f_sig.detach()  # block gradients
    x_normed = (x - f_mu) / f_sig  # normalize input
    lmda = Beta(alpha, alpha).sample((batch_size, 1, 1, 1)).to(x.device)  # sample instance-wise convex weights
    perm = torch.randperm(batch_size).to(x.device)  # generate shuffling indices
    f_mu_perm, f_sig_perm = f_mu[perm], f_sig[perm]  # shuffling
    mu_mix = f_mu * lmda + f_mu_perm * (1 - lmda)  # generate mixed mean
    sig_mix = f_sig * lmda + f_sig_perm * (1 - lmda)  # generate mixed standard deviation
    return x_normed * sig_mix + mu_mix  # denormalize input using the mixed statistics

def print_size_of_model(model):
    torch.save(model.state_dict(), "temp.p")
    model_size_bytes = os.path.getsize("temp.p")
    print('Size (MB):', model_size_bytes/1e6)
    os.remove('temp.p')
    return model_size_bytes

def mixup(size, alpha):
    rn_indices = torch.randperm(size)
    lambd = np.random.beta(alpha, alpha, size).astype(np.float32)
    lambd = np.concatenate([lambd[:, None], 1 - lambd[:, None]], 1).max(1)
    lam = torch.FloatTensor(lambd)
    # data = data * lam + data2 * (1 - lam)
    # targets = targets * lam + targets2 * (1 - lam)
    return rn_indices, lam

def spawn_get(seedseq, n_entropy, dtype):
    child = seedseq.spawn(1)[0]
    state = child.generate_state(n_entropy, dtype=np.uint32)

    if dtype == np.ndarray:
        return state
    elif dtype == int:
        state_as_int = 0
        for shift, s in enumerate(state):
            state_as_int = state_as_int + int((2 ** (32 * shift) * s))
        return state_as_int
    else:
        raise ValueError(f'not a valid dtype "{dtype}"')


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

"""
Author: Andy Tian
Contact: tianjunning@126.com
Software: PyCharm
Filename: get_heatMap_html.py
Time: 2019/2/21 10:51
"""

import requests
import re

def get_html():
    '''
    获取百度热力图demo的源代码
    :return: h5代码
    '''
    url = "http://lbsyun.baidu.com/jsdemo/demo/c1_15.htm"
    header = {
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6788.400 QQBrowser/10.3.2864.400",
        "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "Accept-Encoding":"gzip, deflate",
        "Accept-Language":"zh-CN,zh;q=0.9"
    }
    htmlstr = requests.get(url,headers=header)._content.decode()
    htmlstr_formated = htmlstr.replace("\n",'').replace("\t",'')

    return htmlstr_formated

def modify_html(htmlstr):
    '''
    根据项目需要,对demo中的参数进行修改
    1)ak  修改为自己在百度中申请的密钥
    2)container{height:500px;width:100%;} 地图打开时的大小.
    3)points 热力图显示的坐标
    4)new BMap.point 地图打开时显示的重点位置
    5)heatmapOverlay = new BMapLib.HeatmapOverlay({"radius":20}) 热力显示半径
    6)# heatmapOverlay.setDataSet({data:points,max:100}); 数据大小,超过max后显示颜色一致,根据实际数据,修改max大小
    :param htmlstr:需要修改的h5代码
    :return: 修改好的代码
    '''
    data = open("G:\Python\Project\Spider\scrapyProject\lianjia\lon_lat.json")
    datastr = data.read()
    htmlstr = htmlstr.replace("height:500px","height:80%").replace('{"radius":20}','{"radius":10}').replace("max:100","max:120000")
    be_replaced_data = ",\n".join(re.findall(r'{"lng":.*"count":\d*}',htmlstr))
    htmlstr_modified = htmlstr.replace(be_replaced_data,datastr)
    return htmlstr_modified

def rewrite_html(str):
    '''
    h5代码写入文件
    :param str: h5代码
    :return: h5文档
    '''
    with open("heat.html","w",encoding="utf-8") as f:
        f.write(str)

if __name__ == "__main__":
    htmlstr = get_html()
    htmlstr_modified = modify_html(htmlstr)
    write_html(htmlstr_modified)


import json

with open('mahasiswa.json', 'r') as file:
    a = json.load(file)

    b = dict()
    c = int(input("Masukkan Jumkah Mahasiswa baru : "))

    for i in range(c):
        nm = input("Masukkan nama anda: ")
        hb = []

        untuk_hobi = int(input("Masukkan jumlah hobi: "))
        for j in range(untuk_hobi):
            hb1 = input("Masukkan hobi ke-{} : ".format(j+1))
            hb.append(hb1)
        per = input("Masukkan prestasi anda: ")

        print("====Data Berhasil ditambahkan===")
        print()

        b [nm] = [{"Biodata": {"Hobi": hb, "Prestasti" : per}}]

    a.update(b)

with open('mahasiswa.json', 'w') as file:
    json.dump(a,file)


"""added columns to Places

Revision ID: cba44d27f422
Revises: 061ea741f852
Create Date: 2023-06-28 15:56:11.475592

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'cba44d27f422'
down_revision = '061ea741f852'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('places', schema=None) as batch_op:
        batch_op.add_column(sa.Column('website', sa.String(), nullable=True))
        batch_op.add_column(sa.Column('photo', sa.String(), nullable=True))
        batch_op.add_column(sa.Column('price_level', sa.Integer(), nullable=True))
        batch_op.add_column(sa.Column('user_ratings_total', sa.Integer(), nullable=True))
        batch_op.add_column(sa.Column('rating', sa.Float(), nullable=True))

    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('places', schema=None) as batch_op:
        batch_op.drop_column('rating')
        batch_op.drop_column('user_ratings_total')
        batch_op.drop_column('price_level')
        batch_op.drop_column('photo')
        batch_op.drop_column('website')

    # ### end Alembic commands ###


import torch
import time
import torch.nn.functional as F

def train(model, device, train_loader, optimizer, epoch):   # 训练模型
    model.train()
    best_acc = 0.0
    for batch_idx, (x1, x2, x3, y) in enumerate(train_loader):
        start_time = time.time()
        x1, x2, x3, y = x1.to(device), x2.to(device), x3.to(device), y.to(device)
        out = model([x1, x2, x3])  # 得到预测结果
        y_pred = out[0]
        model.zero_grad()             # 梯度清零
        loss = F.cross_entropy(y_pred, y.squeeze())  # 得到loss
        loss.backward()
        optimizer.step()
        if(batch_idx + 1) % 100 == 0:    # 打印loss
            print('Train Epoch: {} [{}/{} ({:.2f}%)]\t\tLoss: {:.6f}'.format(epoch, (batch_idx+1) * len(x1),
                                                                             len(train_loader.dataset),
                                                                             100. * (batch_idx+1) / len(train_loader),
                                                                             loss.item()))  # 记得为loss.item()

def test(model, device, test_loader):    # 测试模型, 得到测试集评估结果
    model.eval()
    test_loss = 0.0
    acc = 0
    for batch_idx, (x1, x2, x3, y) in enumerate(test_loader):
        x1, x2, x3, y = x1.to(device), x2.to(device), x3.to(device), y.to(device)
        with torch.no_grad():
            out = model([x1, x2, x3])
        y_ = out[0]
        test_loss += F.cross_entropy(y_, y.squeeze())
        pred = y_.max(-1, keepdim=True)[1]   # .max(): 2输出，分别为最大值和最大值的index
        acc += pred.eq(y.view_as(pred)).sum().item()    # 记得加item()
    test_loss /= len(test_loader)
    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)'.format(
          test_loss, acc, len(test_loader.dataset),
          100. * acc / len(test_loader.dataset)))
    return acc / len(test_loader.dataset)

def test_lem(model, device, test_loader):    # 测试模型, 得到测试集评估结果
    model.eval()
    input_embeddings, label_embeddings, labels = [], [], []
    test_loss = 0.0
    acc = 0
    for batch_idx, (x1, x2, x3, y) in enumerate(test_loader):
        x1, x2, x3, y = x1.to(device), x2.to(device), x3.to(device), y.to(device)
        with torch.no_grad():
            out = model([x1, x2, x3])
        y_, V, C = out[0], out[1], out[2]
        input_embeddings.append(V.cpu())
        label_embeddings = C.cpu()
        test_loss += F.cross_entropy(y_, y.squeeze())
        pred = y_.max(-1, keepdim=True)[1]   # .max(): 2输出，分别为最大值和最大值的index
        labels.append(pred)
        acc += pred.eq(y.view_as(pred)).sum().item()    # 记得加item()
    test_loss /= len(test_loader)
    return acc / len(test_loader.dataset), input_embeddings, label_embeddings, labels


#!/usr/bin/env python
'''
Created on Jun 28, 2016

@author: isvoboda
'''

from __future__ import print_function

import sys
import multiprocessing
import logging
import yaml
import argparse
from collections import OrderedDict
import cnn_image_processing as ci
import signal

signal.signal(signal.SIGINT, lambda x, y: sys.exit(1))
LOGGER = logging.getLogger("cnn_image_processing")

def parse_phase(conf):
    """
    Parse net phase Train/Test
    """
    dmodules = {}

    creator = ci.Creator

    pque_size = 5
    if 'provider_queue_size' in conf:
        pque_size = conf['provider_queue_size']

    sque_size = 512
    if 'sample_queue_size' in conf:
        sque_size = conf['sample_queue_size']

    dmodules['pque'] = multiprocessing.Queue(pque_size)
    dmodules['sque'] = multiprocessing.Queue(sque_size)

    if 'Provider' in conf:
        dmodules['provider'] = creator.create_provider(conf['Provider'])
        dmodules['provider'].out_queue = dmodules['pque']
    else:
        dmodules['provider'] = None
#     train_provider.file_list = train_list

    if 'Sampler' in conf:
        dmodules['sampler'] = creator.create_sampler(conf['Sampler'])
        dmodules['sampler'].in_queue = dmodules['pque']
        dmodules['sampler'].out_queue = dmodules['sque']
    else:
        dmodules['sampler'] = None

    return dmodules

def parse_config(conf=None):
    """
    Parse the train_cnn application configuration
    """
    creator = ci.Creator
    app = {}

    app['Train'] = parse_phase(conf['Train'])
    app['Train']['provider'].out_queue = app['Train']['pque']
    app['Train']['sampler'].in_queue = app['Train']['pque']
    app['Train']['sampler'].out_queue = app['Train']['sque']
    in_ques = []

    if 'Test' in conf:
        test_nets = OrderedDict()
        test_net_list = [test_net.keys()[0] for test_net in conf['Test']]
        test_net_list.sort()
        for i_key, net_key in enumerate(test_net_list):
            test_nets[net_key] = parse_phase(conf['Test'][i_key][net_key])

            if test_nets[net_key]['provider'] == None:
                tprovider = creator.create_provider(conf['Train']['Provider'])
                tprovider.out_queue = test_nets[net_key]['pque']
                test_nets[net_key]['provider'] = tprovider
            if test_nets[net_key]['sampler'] == None:
                tsampler = creator.create_sampler(['Train']['Sampler'])
                tsampler.in_queue = test_nets[net_key]['pque']
                tsampler.out_queue = test_nets[net_key]['sque']
                test_nets[net_key]['sampler'] = tsampler

            in_ques.append(test_nets[net_key]['sque'])
        app['Test'] = test_nets

    app['Trainer'] = creator.create_trainer(conf['Trainer'])
    app['Trainer'].train_in_queue = app['Train']['sque']
    app['Trainer'].test_in_queue = in_ques

    return app

def main():
    '''
    Entry point
    Args:
        argv: list of command line arguments.
    '''
    parser = argparse.ArgumentParser(description="Train the cnn")
    parser.add_argument("-c", "--conf-file", action='store', type=str,
                        choices=None, required=True, help="Configuration file",
                        metavar=None, dest='conf_file')

    parser.add_argument("-s", "--solver-file", action='store', type=str,
                        choices=None, required=True, help="Solver file",
                        metavar=None, dest='solver_file')

    parser.add_argument("-v", "--verbose", action="store_true", required=False,
                        help="Set the verbose mode.", dest='verbose')

    parser.add_argument("-tr", "--train-list", action='store', type=str,
                        help="Training file list", required=True,
                        dest='train_list')

    parser.add_argument("-te", "--test-lists", action='store',
                        nargs='*', type=str, default=None,
                        required=False, dest='test_lists',
                        help="Training file lists")

    args = parser.parse_args()

    # Print the arguments
    for key, val in vars(args).iteritems():
        print("{}: {}".format(key, val))

    # Initialize logging
    if args.verbose:
        LOGGER.setLevel(logging.DEBUG)
    else:
        LOGGER.setLevel(logging.INFO)

    logging.basicConfig()

    config_file = args.conf_file
    solver_file = args.solver_file
    train_list = args.train_list
    test_lists = args.test_lists

    # Open, parse and print the configuration file
    with open(config_file) as cf_file:
        conf = yaml.safe_load(cf_file)
        print (yaml.dump(conf))

    app = parse_config(conf)

    app['Train']['provider'].file_list = train_list
    app['Train']['provider'].start()
    app['Train']['sampler'].start()
    if test_lists is not None:
        assert len(test_lists) == len(app['Test'])
        for i_test, test_k in enumerate(app['Test']):
            app['Test'][test_k]['provider'].file_list = test_lists[i_test]
            app['Test'][test_k]['provider'].start()
            app['Test'][test_k]['sampler'].start()

    app['Trainer'].solver_file = solver_file
    app['Trainer'].start()
    app['Trainer'].join()

if __name__ == "__main__":
    main()


import torch.nn as nn
import tqdm
import torch
class ANN(nn.Module):
    def __init__(self, input=4):
        super().__init__()

        # self.relu1 = nn.ReLU(inplace=True)
        self.liner1 = nn.Linear(input,128)
        self.relu = nn.ReLU()
        self.liner2 = nn.Linear(128,8)
        self.liner3 = nn.Linear(8,4)

        # self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        out = self.relu(self.liner1(x))
        out = self.relu(self.liner2(out))
        out = self.relu(self.liner3(out))

        return out


#!/usr/bin/env python3

"""
Example for Implied Volatility using the NAG Library for Python
Finds implied volatilities of the Black Scholes equation using specfun.opt_imp_vol
Data needs to be downloaded from:
http://www.cboe.com/delayedquote/QuoteTableDownload.aspx
Make sure to download data during CBOE Trading Hours.
Updated for NAG Library for Python Mark 27.1
"""

# pylint: disable=invalid-name,too-many-branches,too-many-locals,too-many-statements

try:
    import sys
    import pandas
    import numpy as np
    import matplotlib.pylab as plt
    import warnings
    from naginterfaces.library import specfun, fit
    from naginterfaces.base import utils
    from matplotlib import cm

except ImportError as e:
    print(
        "Could not import the following module. "
        "Do you have a working installation of the NAG Library for Python?"
    )
    print(e)
    sys.exit(1)

__author__ = "Edvin Hopkins, John Morrissey and Brian Spector"
__copyright__ = "Copyright 2021, The Numerical Algorithms Group Inc"
__email__ = "support@nag.co.uk"

# Set to hold expiration dates
dates = []

cumulative_month = {'Jan': 31, 'Feb': 57, 'Mar': 90,
                    'Apr': 120, 'May': 151, 'Jun': 181,
                    'Jul': 212, 'Aug': 243, 'Sep': 273,
                    'Oct': 304, 'Nov': 334, 'Dec': 365}

def main(): # pylint: disable=missing-function-docstring

    try:
        if len(sys.argv)>1:
            QuoteData = sys.argv[1]
        else:
            QuoteData = 'QuoteData.dat'

        qd = open(QuoteData, 'r')
        qd_head = []
        qd_head.append(qd.readline())
        qd_head.append(qd.readline())
        qd.close()
    except: # pylint: disable=bare-except
        sys.stderr.write("Usage: implied_volatility.py QuoteData.dat\n")
        sys.stderr.write("Couldn't read QuoteData\n")
        sys.exit(1)

    print("Implied Volatility for %s %s" % (qd_head[0].strip(), qd_head[1]))

    # Parse the header information in QuotaData
    first = qd_head[0].split(',')
    second = qd_head[1].split()
    qd_date = qd_head[1].split(',')[0]

    company = first[0]
    underlyingprice = float(first[1])
    month, day = second[:2]
    today = cumulative_month[month] + int(day) - 30
    current_year = int(second[2])

    def getExpiration(x):
        monthday = x.split()
        adate = monthday[0] + ' ' + monthday[1]
        if adate not in dates:
            dates.append(adate)
        return (int(monthday[0]) - (current_year % 2000)) * 365 + cumulative_month[monthday[1]]

    def getStrike(x):
        monthday = x.split()
        return float(monthday[2])

    data = pandas.io.parsers.read_csv(QuoteData, sep=',', header=2, na_values=' ')

    # Need to fill the NA values in dataframe
    data = data.fillna(0.0)

    # Let's look at data where there was a recent sale
    data = data[(data['Last Sale'] > 0) | (data['Last Sale.1'] > 0)]

    # Get the Options Expiration Date
    exp = data.Calls.apply(getExpiration)
    exp.name = 'Expiration'

    # Get the Strike Prices
    strike = data.Calls.apply(getStrike)
    strike.name = 'Strike'

    data = data.join(exp).join(strike)

    print("Number of data points found: {}\n".format(len(data.index)))

    print('Calculating Implied Vol of Calls...')
    r = np.zeros(len(data.index))
    t = (data.Expiration - today)/365.0
    s0 = np.full(len(data.index),underlyingprice)
    pCall= (data.Bid + data.Ask) / 2

    # A lot of the data is incomplete or extreme so we tell the NAG routine
    # not to worry about warning us about data points it can't work with
    warnings.simplefilter('ignore',utils.NagAlgorithmicWarning)
    sigmaCall = specfun.opt_imp_vol('C',pCall,data.Strike, s0,t,r,mode = 1).sigma
    impvolcall = pandas.Series(sigmaCall,index=data.index, name='impvolCall')

    data = data.join(impvolcall)

    print('Calculating Implied Vol of Puts...')
    pPut= (data['Bid.1'] + data['Ask.1']) / 2
    sigmaPut = specfun.opt_imp_vol('P',pPut,data.Strike, s0,t,r,mode = 1).sigma
    impvolput = pandas.Series(sigmaPut,index=data.index, name='impvolPut')

    data = data.join(impvolput)
    fig = plt.figure(1)
    fig.subplots_adjust(hspace=.4, wspace=.3)

    # Plot the Volatility Curves
    # Encode graph layout: 3 rows, 3 columns, 1 is first graph.
    num = 331
    max_xticks = 4

    for date in dates:
        # add each subplot to the figure
        plot_year, plot_month = date.split()
        plot_date = (int(plot_year) - (current_year % 2000)) * 365 + cumulative_month[plot_month]
        plot_call = data[(data.impvolCall > .01) &
                       (data.Expiration == plot_date) &
                       (data['Last Sale'] > 0)]
        plot_put = data[(data.impvolPut > .01) &
                        (data.Expiration == plot_date) &
                        (data['Last Sale.1'] > 0)]

        myfig = fig.add_subplot(num)
        xloc = plt.MaxNLocator(max_xticks)
        myfig.xaxis.set_major_locator(xloc)
        myfig.set_title('Expiry: %s 20%s' % (plot_month, plot_year))
        myfig.plot(plot_call.Strike, plot_call.impvolCall, 'pr', label='call',markersize=0.5)
        myfig.plot(plot_put.Strike, plot_put.impvolPut, 'p', label='put',markersize=0.5)
        myfig.legend(loc=1, numpoints=1, prop={'size': 10})
        myfig.set_ylim([0,1])
        myfig.set_xlabel('Strike Price')
        myfig.set_ylabel('Implied Volatility')
        num += 1

    plt.suptitle('Implied Volatility for %s Current Price: %s Date: %s' %
                 (company, underlyingprice, qd_date))

    print("\nPlotting Volatility Curves/Surface")

    # The code below will plot the Volatility Surface
    # It uses fit.dim2_cheb_lines to fit with a polynomial and
    # fit.dim2_cheb_eval to evaluate at intermediate points

    m = np.empty(len(dates), dtype=np.int32)
    y = np.empty(len(dates), dtype=np.double)
    xmin = np.empty(len(dates), dtype=np.double)
    xmax = np.empty(len(dates), dtype=np.double)

    data = data.sort_values(by=['Strike']) # Need to sort for NAG Algorithm

    k = 3   # this is the degree of polynomial for x-axis (Strike Price)
    l = 3   # this is the degree of polynomial for y-axis (Expiration Date)

    i = 0

    for date in dates:
        plot_year, plot_month = date.split()
        plot_date = (int(plot_year) - (current_year % 2000)) * 365 + cumulative_month[plot_month]

        call_data = data[(data.Expiration == plot_date) &
				(data.impvolPut > .01) &
				(data.impvolPut < 1) &
                                (data['Last Sale.1'] > 0)]

        exp_sizes = call_data.Expiration.size
        if exp_sizes > 0:
            m[i] = exp_sizes

            if i == 0:
                x = np.array(call_data.Strike)
                call = np.array(call_data.impvolPut)
                xmin[0] = x.min()
                xmax[0] = x.max()
            else:
                x2 = np.array(call_data.Strike)
                x = np.append(x,x2)
                call2 = np.array(call_data.impvolPut)
                call = np.append(call,call2)
                xmin[i] = x2.min()
                xmax[i] = x2.max()
            y[i] = plot_date-today
            i+=1
    nux = np.zeros(1,dtype=np.double)
    nuy = np.zeros(1,dtype=np.double)

    if len(dates) != i:
        print(
            "Error with data: the CBOE may not be open for trading "
            "or one expiration date has null data"
        )
        return 0
    weight = np.ones(call.size, dtype=np.double)

    #Call the NAG Chebyshev fitting function
    output_coef = fit.dim2_cheb_lines(m,k,l,x,y,call,weight,(k + 1) * (l + 1),xmin,xmax,nux,nuy)

    # Now that we have fit the function,
    # we use fit.dim2_cheb_eval to evaluate at different strikes/expirations

    nStrikes = 100 # number of Strikes to evaluate
    spacing = 20 # number of Expirations to evaluate
    for i in range(spacing):
        mfirst = 1
        xmin = data.Strike.min()
        xmax = data.Strike.max()

        x = np.linspace(xmin, xmax, nStrikes)

        ymin = data.Expiration.min() - today
        ymax = data.Expiration.max() - today

        y = (ymin) + i * np.floor((ymax - ymin) / spacing)

        fx=np.empty(nStrikes)

        fx=fit.dim2_cheb_eval(mfirst,k,l,x,xmin,xmax,y,ymin,ymax,output_coef)

        if 'xaxis' in locals():
            xaxis = np.append(xaxis, x)
            temp = np.empty(len(x))
            temp.fill(y)
            yaxis = np.append(yaxis, temp)
            for j in range(len(x)):
                zaxis.append(fx[j])
        else:
            xaxis = x
            yaxis = np.empty(len(x), dtype=np.double)
            yaxis.fill(y)
            zaxis = []
            for j in range(len(x)):
                zaxis.append(fx[j])

    fig = plt.figure(2)
    ax = fig.add_subplot(111, projection='3d')

    # A try-except block for Matplotlib
    try:
        ax.plot_trisurf(xaxis, yaxis, zaxis, cmap=cm.jet)
    except AttributeError:
        print ("Your version of Matplotlib does not support plot_trisurf")
        print ("...plotting wireframe instead")
        ax.plot(xaxis, yaxis, zaxis)

    ax.set_xlabel('Strike Price')
    ax.set_ylabel('Days to Expiration')
    ax.set_zlabel('Implied Volatility for Put Options')
    plt.suptitle('Implied Volatility Surface for %s Current Price: %s Date: %s' %
                 (company, underlyingprice, qd_date))

    plt.show()

if __name__ == "__main__":
    main()


import types
from imp import reload

def print_status(module):
    print(f'reloading {module.__name__}')

def try_reload(module):
    try:
        reload(module)
    except Exception as e:
        print(f'FAILED {e.__repr__()} : {module}')

def transitive_reload(module, visited):
    if not module in visited:
        print_status(module)
        try_reload(module)
        visited[module] = True
        for attrobj in module.__dict__.values():
            if type(attrobj) == types.ModuleType:
                transitive_reload(attrobj, visited)

def reload_all(*args):
    visited = {}
    for arg in args:
        if type(arg) == types.ModuleType:
            transitive_reload(arg, visited)

if __name__ == '__main__':
    def tester(reloader, modname):
        import importlib, sys
        if len(sys.argv) > 1:
            modname = sys.argv[1]
        module = importlib.import_module(modname)
        reloader(module)

    tester(reload_all, 'reloadall')


import torch
import torch.nn as nn
from utils.resnet_infomin import model_dict
import torch.nn.functional as F
from collections import OrderedDict

class RGBSingleHead(nn.Module):
    """RGB model with a single linear/mlp projection head"""
    def __init__(self, name='resnet50', head='linear', feat_dim=128):
        super(RGBSingleHead, self).__init__()

        name, width = self._parse_width(name)
        dim_in = int(2048 * width)
        self.width = width

        self.encoder = model_dict[name](width=width)

        if head == 'linear':
            self.head = nn.Sequential(
                nn.Linear(dim_in, feat_dim),
                Normalize(2)
            )
        elif head == 'mlp':
            self.head = nn.Sequential(
                nn.Linear(dim_in, dim_in),
                nn.ReLU(inplace=True),
                nn.Linear(dim_in, feat_dim),
                Normalize(2)
            )
        else:
            raise NotImplementedError(
                'head not supported: {}'.format(head))

    @staticmethod
    def _parse_width(name):
        if name.endswith('x4'):
            return name[:-2], 4
        elif name.endswith('x2'):
            return name[:-2], 2
        else:
            return name, 1

    def forward(self, x, mode=0):
        # mode --
        # 0: normal encoder,
        # 1: momentum encoder,
        # 2: testing mode
        feat = self.encoder(x)
        if mode == 0 or mode == 1:
            feat = self.head(feat)
        return feat

class RGBMultiHeads(RGBSingleHead):
    """RGB model with Multiple linear/mlp projection heads"""
    def __init__(self, name='resnet50', head='linear', feat_dim=128):
        super(RGBMultiHeads, self).__init__(name, head, feat_dim)

        self.head_jig = JigsawHead(dim_in=int(2048*self.width),
                                   dim_out=feat_dim,
                                   head=head)

    def forward(self, x, x_jig=None, mode=0):
        # mode --
        # 0: normal encoder,
        # 1: momentum encoder,
        # 2: testing mode
        if mode == 0:
            feat = self.head(self.encoder(x))
            feat_jig = self.head_jig(self.encoder(x_jig))
            return feat, feat_jig
        elif mode == 1:
            feat = self.head(self.encoder(x))
            return feat
        else:
            feat = self.encoder(x)
            return feat

class CMCSingleHead(nn.Module):
    """CMC model with a single linear/mlp projection head"""
    def __init__(self, name='resnet50', head='linear', feat_dim=128):
        super(CMCSingleHead, self).__init__()

        name, width = self._parse_width(name)
        dim_in = int(2048 * width)
        self.width = width

        self.encoder1 = model_dict[name](width=width, in_channel=1)
        self.encoder2 = model_dict[name](width=width, in_channel=2)

        if head == 'linear':
            self.head1 = nn.Sequential(
                nn.Linear(dim_in, feat_dim),
                Normalize(2)
            )
            self.head2 = nn.Sequential(
                nn.Linear(dim_in, feat_dim),
                Normalize(2)
            )
        elif head == 'mlp':
            self.head1 = nn.Sequential(
                nn.Linear(dim_in, dim_in),
                nn.ReLU(inplace=True),
                nn.Linear(dim_in, feat_dim),
                Normalize(2)
            )
            self.head2 = nn.Sequential(
                nn.Linear(dim_in, dim_in),
                nn.ReLU(inplace=True),
                nn.Linear(dim_in, feat_dim),
                Normalize(2)
            )
        else:
            raise NotImplementedError(
                'head not supported: {}'.format(head))

    @staticmethod
    def _parse_width(name):
        if name.endswith('x4'):
            return name[:-2], 2
        elif name.endswith('x2'):
            return name[:-2], 1
        else:
            return name, 0.5

    def forward(self, x, mode=0):
        # mode --
        # 0: normal encoder,
        # 1: momentum encoder,
        # 2: testing mode
        x1, x2 = torch.split(x, [1, 2], dim=1)
        feat1 = self.encoder1(x1)
        feat2 = self.encoder2(x2)
        if mode == 0 or mode == 1:
            feat1 = self.head1(feat1)
            feat2 = self.head2(feat2)
        return torch.cat((feat1, feat2), dim=1)

class CMCMultiHeads(CMCSingleHead):
    """CMC model with Multiple linear/mlp projection heads"""
    def __init__(self, name='resnet50', head='linear', feat_dim=128):
        super(CMCMultiHeads, self).__init__(name, head, feat_dim)

        self.head1_jig = JigsawHead(dim_in=int(2048*self.width),
                                    dim_out=feat_dim,
                                    head=head)
        self.head2_jig = JigsawHead(dim_in=int(2048*self.width),
                                    dim_out=feat_dim,
                                    head=head)

    def forward(self, x, x_jig=None, mode=0):
        # mode --
        # 0: normal encoder,
        # 1: momentum encoder,
        # 2: testing mode
        x1, x2 = torch.split(x, [1, 2], dim=1)
        feat1 = self.encoder1(x1)
        feat2 = self.encoder2(x2)

        if mode == 0:
            x1_jig, x2_jig = torch.split(x_jig, [1, 2], dim=1)
            feat1_jig = self.encoder1(x1_jig)
            feat2_jig = self.encoder2(x2_jig)

            feat1, feat2 = self.head1(feat1), self.head2(feat2)
            feat1_jig = self.head1_jig(feat1_jig)
            feat2_jig = self.head2_jig(feat2_jig)
            feat = torch.cat((feat1, feat2), dim=1)
            feat_jig = torch.cat((feat1_jig, feat2_jig), dim=1)
            return feat, feat_jig
        elif mode == 1:
            feat1, feat2 = self.head1(feat1), self.head2(feat2)
            return torch.cat((feat1, feat2), dim=1)
        else:
            return torch.cat((feat1, feat2), dim=1)

class Normalize(nn.Module):
    def __init__(self, p=2):
        super(Normalize, self).__init__()
        self.p = p

    def forward(self, x):
        return F.normalize(x, p=self.p, dim=1)

class JigsawHead(nn.Module):
    """Jigswa + linear + l2norm"""
    def __init__(self, dim_in, dim_out, k=9, head='linear'):
        super(JigsawHead, self).__init__()

        if head == 'linear':
            self.fc1 = nn.Linear(dim_in, dim_out)
        elif head == 'mlp':
            self.fc1 = nn.Sequential(
                nn.Linear(dim_in, dim_in),
                nn.ReLU(inplace=True),
                nn.Linear(dim_in, dim_out),
            )
        else:
            raise NotImplementedError('JigSaw head not supported: {}'.format(head))
        self.fc2 = nn.Linear(dim_out * k, dim_out)
        self.l2norm = Normalize(2)
        self.k = k

    def forward(self, x):
        bsz = x.shape[0]
        x = self.fc1(x)
        # ==== shuffle ====
        # this step can be moved to data processing step
        shuffle_ids = self.get_shuffle_ids(bsz)
        x = x[shuffle_ids]
        # ==== shuffle ====
        n_img = int(bsz / self.k)
        x = x.view(n_img, -1)
        x = self.fc2(x)
        x = self.l2norm(x)
        return x

    def get_shuffle_ids(self, bsz):
        n_img = int(bsz / self.k)
        rnd_ids = [torch.randperm(self.k) for i in range(n_img)]
        rnd_ids = torch.cat(rnd_ids, dim=0)
        base_ids = torch.arange(bsz)
        base_ids = torch.div(base_ids, self.k).long()
        base_ids = base_ids * self.k
        shuffle_ids = rnd_ids + base_ids
        return shuffle_ids

#default settings taken from https://github.com/HobbitLong/PyContrast/tree/master/pycontrast
OPT = {'method': 'InfoMin',
        'modal': 'RGB',
        'jigsaw': True,
        'mem': 'moco',
        'arch': 'resnet50',
        'feat_dim': 128,
        'head': 'mlp',
        'ckpt': '/experimentos/pesos/infomin/InfoMin_800.pth', #custom path
        'aug_linear': 'NULL',
        'n_class': 1000,
        'aug': 'D'}

NAME_TO_FUNC = {
    'RGBSin': RGBSingleHead,
    'RGBMul': RGBMultiHeads,
    'CMCSin': CMCSingleHead,
    'CMCMul': CMCMultiHeads,
}

def load_encoder_weights(model):
        """load pre-trained weights for encoder

        Args:
          model: pretrained encoder, should be frozen
        """
        msg = "Empty Message"
        if OPT['ckpt']:
            ckpt = torch.load(OPT['ckpt'], map_location='cpu')
            state_dict = ckpt['model']
            if OPT['modal'] == 'RGB':
                # Unimodal (RGB) case
                encoder_state_dict = OrderedDict()
                for k, v in state_dict.items():
                    k = k.replace('module.', '')
                    if 'encoder' in k:
                        k = k.replace('encoder.', '')
                        encoder_state_dict[k] = v
                msg = model.encoder.load_state_dict(encoder_state_dict)
            else:
                # Multimodal (CMC) case
                encoder1_state_dict = OrderedDict()
                encoder2_state_dict = OrderedDict()
                for k, v in state_dict.items():
                    k = k.replace('module.', '')
                    if 'encoder1' in k:
                        k = k.replace('encoder1.', '')
                        encoder1_state_dict[k] = v
                    if 'encoder2' in k:
                        k = k.replace('encoder2.', '')
                        encoder2_state_dict[k] = v
                msg = model.encoder1.load_state_dict(encoder1_state_dict)
                msg += " " + model.encoder2.load_state_dict(encoder2_state_dict)
            print('Pre-trained weights loaded!', msg)
        else:
            print('==============================')
            print('warning: no pre-trained model!')
            print('==============================')
            msg = "warning: no pre-trained model!"

        return model, msg

def build_model():
    # specify modal key
    branch = 'Mul' if OPT['jigsaw'] else 'Sin'
    model_key = OPT['modal'] + branch

    model = NAME_TO_FUNC[model_key](OPT['arch'], OPT['head'], OPT['feat_dim'])
    if OPT['mem'] == 'moco':
        model_ema = NAME_TO_FUNC[model_key](OPT['arch'], OPT['head'], OPT['feat_dim'])
    else:
        model_ema = None

    return model, model_ema

if __name__ == '__main__':
    model, _ = build_model()
    model, msg = load_encoder_weights(model)
    print(msg)