import cv2
import os
import threading

from classifier import Classifier
from queue import Queue

#classifier = \
 #   Classifier(model_path='/Users/karltrout/Documents/DockerHome/etc/20170511-185253/20170511-185253.pb',
  #             classifier_path='/Users/karltrout/Documents/DockerHome/output/classifier.pkl')

from align_dlib import AlignDlib
align_dlib = AlignDlib(os.path.join(os.path.dirname(__file__), 'shape_predictor_68_face_landmarks.dat'))
bBox = None
threadLock = threading.Lock()
q = Queue(maxsize=1)
name = "unknown"

def _align_image(image, crop_dim):
    bb = align_dlib.getLargestFaceBoundingBox(image)
    aligned = align_dlib.align(crop_dim, image, bb, landmarkIndices=AlignDlib.INNER_EYES_AND_BOTTOM_LIP)
    if aligned is not None:
        aligned = cv2.cvtColor(aligned, cv2.COLOR_BGR2RGB)
    return bb, aligned

def classifier_thread_worker(q):
    """thread worker function"""
    classifier = \
        Classifier(model_path='/Users/karltrout/Documents/DockerHome/etc/20170511-185253/20170511-185253.pb',
                   classifier_path='/Users/karltrout/Documents/DockerHome/output/classifier.pkl')
    while True:
        try:
            image_in = q.get_nowait()
            if image_in is None:
                break
            name, prediction = classifier.classify_image(image=image_in)
            q.task_done()
            print(" Found : {} with {:.3%} probability".format(name, prediction))

            if prediction < .9:
                imgDir = "UnKnownImgs/"

                if (not os.path.exists(imgDir)):
                    os.makedirs(imgDir)
                    print("Created Directory: "+imgDir)

                cnt = 0
                notfound = imgDir+"UnKnownImg.{}.jpg".format(cnt)

                while os.path.isfile(notfound):
                    cnt=cnt+1
                    notfound = imgDir+"UnKnownImg.{}.jpg".format(cnt)

                cv2.imwrite(notfound, image_in)
                print("writing file :"+notfound)

        except:
            pass

cap = cv2.VideoCapture(0)
crop_dim = 180

# Get the width and height of frame
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) + 0.5)
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) + 0.5)

t = threading.Thread(target=classifier_thread_worker, args=(q,))
t.start()

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

    if ret is True:
        frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25, interpolation=cv2.INTER_NEAREST)
        #smallFrame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5, interpolation=cv2.INTER_NEAREST)
        image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        if image is not None:
            bb, aligned_image = _align_image(image, crop_dim)

            if bb is not None:
                    bBox = bb
            else:
                bBox = None

            if bBox is not None :
                cv2.rectangle(frame,
                              pt1=(bBox.left(),
                                   bBox.top()),
                              pt2=(bBox.right(),
                                   bBox.bottom()),
                              color=(0, 255, 0),
                              thickness=1)

            cv2.imshow('frame', frame)
            if aligned_image is not None :
                # do awesome ML Look up stuff here.
                #classifier_thread_worker(aligned_image)
                try:
                    q.put_nowait(aligned_image)
                except:
                    pass

        else:
            raise IOError('Error buffering image.')
        if (cv2.waitKey(1) & 0xFF) == ord('q'):  # Hit `q` to exit
            q.put(None)
            t.join()
            break
    else:
        break

cap.release()
cv2.destroyAllWindows()


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import KFold,StratifiedKFold
import lightgbm as lgb
from sklearn.metrics import mean_squared_error

train = pd.read_csv('atmacup10_dataset/train.csv')
y = train['likes'].values
y = np.log1p(y)

train_feat_df = pd.read_csv('new_dataset/train_feat.csv')
test_feat_df = pd.read_csv('new_dataset/test_feat.csv')
sub = pd.read_csv('atmacup10_dataset/submission.csv')
delete_column = ['LE_copyright_holder']
train_feat_df.drop(delete_column, axis=1, inplace=True)
test_feat_df.drop(delete_column, axis=1, inplace=True)

y_train = y
X_train = train_feat_df
X_test = test_feat_df

y_preds = []
models = []
oof_train = np.zeros((len(X_train),))
cv = KFold(n_splits=5, shuffle=True, random_state=0)
kf = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
params = {'num_leaves': 32,
               'min_data_in_leaf': 64,
               'objective': 'regression',
               'max_depth': -1,
               'learning_rate': 0.05,
               "boosting": "gbdt",
               "bagging_freq": 1,
               "bagging_fraction": 0.8,
               "bagging_seed": 0,
               "verbosity": -1,
              'reg_alpha': 0.1,
              'reg_lambda': 0.3,
              'colsample_bytree': 0.7,
              'metric':"rmse",
              'num_threads':6,
         }
categorical_features = ['CE_acquisition_method',
       'CE_principal_maker', 'CE_principal_or_first_maker',
       'CE_acquisition_credit_line',
       'LE_acquisition_method', 'LE_principal_maker',
       'LE_principal_or_first_maker', 'LE_acquisition_credit_line','TL_title','h','w','d','t', 'Navy Model Room', 'dollhouse', 'drawings',
       'glass', 'jewellery', 'lace', 'musical instruments', 'paintings',
       'paper', 'prints']
y_preds = 0

for fold_id, (train_index, valid_index) in enumerate(kf.split(X_train,y_train)):
    X_tr = X_train.loc[train_index, :]
    X_val = X_train.loc[valid_index, :]
    y_tr = y_train[train_index]
    y_val = y_train[valid_index]
    lgb_train = lgb.Dataset(X_tr, y_tr, categorical_feature=categorical_features)
    lgb_eval = lgb.Dataset(X_val, y_val, reference=lgb_train, categorical_feature=categorical_features)

    model = lgb.train(params, lgb_train,
                      valid_sets=[lgb_train, lgb_eval],
                      verbose_eval = 200,
                      num_boost_round = 10000,
                      early_stopping_rounds=200)

    oof_train[valid_index] = model.predict(X_val, num_iteration=model.best_iteration)
    y_pred = model.predict(X_test, num_iteration=model.best_iteration)
    y_preds += y_pred/5
    models.append(model)
pd.DataFrame(oof_train).to_csv('oof_train_kfold.csv', index=False)

y_preds = np.expm1(y_preds)
y_preds[y_preds<0] = 0
sub['likes'] =y_preds
sub.to_csv('first_try.csv', index=False)


from django.shortcuts import render_to_response
from django.http import HttpResponse, Http404

from models import Coin
from django.db.models.loading import get_model

from PIL import Image
from reportlab.graphics.barcode import createBarcodeDrawing

from coins.utils.box import Box

from coins.utils.storage import DatabaseStorage

def image(request, filename, width=None, height=None, image_format='png'):
    file = DatabaseStorage().open(filename, 'rb')
    if not file:
        raise Http404

    if (not image_format or
       image_format.upper() not in ('PNG', 'JPEG', 'JPG', 'GIF')):
        image_format = 'PNG'
    else:
        image_format = image_format.upper()

        if image_format == 'JPG':
            image_format = 'JPEG'

    thumb = Image.open(file)

    if thumb.format == 'PNG' and image_format != thumb.format:
        imageWhiteBg = Image.new('RGB', thumb.size, (255, 255, 255))
        imageWhiteBg.paste(thumb, thumb)
        thumb = imageWhiteBg

    if width and height:
        thumb.thumbnail((int(width), int(height)), Image.ANTIALIAS)

    response = HttpResponse(mimetype=Image.MIME[image_format])
    thumb.save(response, image_format)

    return response

def barcode(request, model_name, coin_id,
            barcode_format='code128', image_format='png'):
    try:
        model = get_model(Coin._meta.app_label,
                          '%s%s'
                          % (model_name[0].upper(), model_name[1:].lower()))
        model = model.objects.get(pk=coin_id)
    except Coin.DoesNotExist:
        raise Http404

    if image_format == 'jpeg':
        image_format = 'jpg'
    elif image_format not in ('png', 'jpg', 'gif'):
        image_format = 'png'

    if barcode_format == 'qr':
        barcode = createBarcodeDrawing(
            'QR',
            value=model.qr_code,
            barHeight=90,
            barWidth=90,
            barBorder=0
        )
    else:
        barcode = createBarcodeDrawing(
            'Code128',
            value=str(model.barcode),
            barWidth=1,
            quiet=False
        )

    return HttpResponse(
        content=barcode.asString(image_format),
        mimetype='image/%s' % image_format
    )

def box(request, coin_id, view_format='html'):
    try:
        coin = Coin.objects.get(pk=coin_id)
    except Coin.DoesNotExist:
        raise Http404

    if view_format not in ('html', 'pdf'):
        view_format = 'html'

    if view_format == 'html':
        return render_to_response('box.html', {'coin': coin})

    response = HttpResponse(mimetype='application/pdf')

    box = Box(coin)
    box.draw(response)

    return response


import requests
import os
from datetime import datetime

NUT_APP_ID = os.environ.get('NUT_ID')
NUT_APP_KEY = os.environ.get('NUT_KEY')
EXERCISE_ENDPOINT = 'https://trackapi.nutritionix.com/v2/natural/exercise'

GENDER = os.environ.get('VAR_GENDER')
WEIGHT = os.environ.get('VAR_WEIGHT')
HEIGHT = os.environ.get('VAR_HEIGHT')
AGE = os.environ.get('VAR_AGE')
PROJECT_NAME = 'exerciseTracker'
SHEET = 'sheet1'
USERNAME = os.environ.get('GOOG_USER')
AUTH_HEADER = {'Authorization': os.environ.get('AUTH_KEY')}

SHEETY_ENDPOINT = f'https://api.sheety.co/{USERNAME}/{PROJECT_NAME}/{SHEET}'

now = datetime.now()
today = now.strftime("%m/%d/%Y")
time = str(now)
time_of_day = time[11:19]

query = input('WHAT YOU DO: ')

request_body = {
    'query':query,
    'gender':GENDER,
    'weight_kg':WEIGHT,
    'height_cm':HEIGHT,
    'age':AGE,
}

headers = {
    'x-app-id':NUT_APP_ID,
    'x-app-key':NUT_APP_KEY,
}

request = requests.post(url=EXERCISE_ENDPOINT, json=request_body, headers=headers)
new_data = request.json()

cals_burned = new_data['exercises'][0]['nf_calories']
time_spent = new_data['exercises'][0]['duration_min']
exercise_type = new_data['exercises'][0]['name']

sheety_body = {
    'sheet1': {
        'date':today,
        'time':time_of_day,
        'exercise':exercise_type,
        'duration':time_spent,
        'calories':cals_burned,
}
}

update_sheet = requests.post(url=SHEETY_ENDPOINT, json=sheety_body, headers=AUTH_HEADER)


import numpy as np
import torch
import torchvision.transforms as tfms
from PIL import Image

import kktorch
from kktorch.trainer.base import Trainer
from kktorch.data.dataloader import MNISTDataLoader
from kktorch.nn.configmod import ConfigModule
from kktorch.nn.loss import DeepSVDDLoss

def create_circle(C: np.ndarray, R: float, niter: int=100):
    n_dim    = C.shape[0]
    rand_dim = np.stack([np.random.permutation(np.arange(n_dim)) for _ in range(niter)])
    rand_pm  = np.random.randint(0, 2, (niter, n_dim))
    rand_pm[rand_pm == 0] = -1
    ndf      = []
    for i in range(niter):
        rand_vec = np.zeros(n_dim)
        _R = R
        for j in rand_dim[i][:-1]:
            point = np.random.rand(1)[0] * _R * rand_pm[i, j]
            rand_vec[j] = point
            _R = np.sqrt(_R ** 2 - point ** 2)
        j = rand_dim[i][-1]
        point = np.sqrt(R ** 2 - (rand_vec ** 2).sum())
        rand_vec[j] = point * rand_pm[i, j]
        ndf.append(rand_vec)
    return np.stack(ndf) + C

class MyTrainer(Trainer):
    def process_label_pre(self, label, input):
        # return label as input for image reconstruction
        return input

if __name__ == "__main__":
    str_result = []
    for i in range(0, 10):
        # network
        dim_z   = 16
        encoder = ConfigModule(
            f"/{kktorch.__path__[0]}/model_zoo/autoencoder/encoder.json",
            user_parameters={
                "___in_channels": 1,
                "___init_dim": 8,
                "___n_layers": 4,
                "___z": dim_z,
            }
        )
        decoder = ConfigModule(
            f"/{kktorch.__path__[0]}/model_zoo/autoencoder/decoder.json",
            user_parameters={
                "___in_channels": 1,
                "___init_size": 2,
                "___alpha": 32,
                "___n_layers": 4,
                "___z": dim_z,
            }
        )
        # dataloader
        dataloader_train = MNISTDataLoader(
            train=True, download=True, batch_size=256, shuffle=False,
            transforms=[tfms.Resize(32, interpolation=Image.BICUBIC),tfms.ToTensor()],
            classes_targets=[j for j in range(10) if j != i]
        )
        # Pre-training ( autoencoder )
        trainer = MyTrainer(
            torch.nn.Sequential(encoder, decoder),
            losses_train=torch.nn.MSELoss(), losses_train_name=["mse"],
            optimizer={"optimizer": torch.optim.AdamW, "params": dict(lr=1e-4)},
            dataloader_train =dataloader_train,
            epoch=1, valid_step=10, print_step=500,
        )
        # to cuda
        trainer.to_cuda()
        # training
        trainer.train()

        # Main-training ( DeepSVDD )
        loss    = DeepSVDDLoss(nu=0.05, n_update_C=(len(dataloader_train) // dataloader_train.batch_size))
        trainer = Trainer(
            encoder,
            losses_train=loss, losses_train_name=["svdd"],
            optimizer={"optimizer": torch.optim.AdamW, "params": dict(lr=1e-4, weight_decay=1e-1)},
            dataloader_train =dataloader_train,
            epoch=5, valid_step=10, print_step=500,
        )
        # to cuda
        trainer.to_cuda()
        # training
        trainer.train()
        # scatter plot
        import matplotlib.pyplot as plt
        plt.figure(figsize = (6, 4))
        output_train, label_train = trainer.predict(dataloader=dataloader_train, is_label=True, sample_size=-1)
        dataloader_train = MNISTDataLoader(
            train=True, download=True, batch_size=256, shuffle=False,
            transforms=[tfms.ToTensor()], classes_targets=[i,]
        )
        output_test, label_test = trainer.predict(dataloader=dataloader_train, is_label=True, sample_size=-1)
        R = loss.R.to("cpu").item()
        C = loss.C.to("cpu").detach().numpy()
        ## accuracy
        ndf_bool = np.sqrt(((output_test - C) ** 2).sum(axis=-1)) > R
        result   = f"total: {ndf_bool.shape[0]}, acc: {round(ndf_bool.sum() / ndf_bool.shape[0], 2)}"
        ## t-SNE
        from MulticoreTSNE import MulticoreTSNE as TSNE
        tsne   = TSNE(n_jobs=32)
        output = tsne.fit_transform(np.concatenate([output_train, output_test], axis=0))
        n_train, n_test = output_train.shape[0], output_test.shape[0]
        for n_min, n_max, color, label in zip(np.cumsum([0, n_train]), np.cumsum([n_train, n_test]), ["b","r"], ["normal","anomaly"]):
            plt.scatter(output[n_min:n_max, 0], output[n_min:n_max, 1], label=label, color=color, s=5, alpha=0.05)
        str_result.append(result)
        plt.title(f"anomaly: {i}, {result}")
        plt.legend()
        plt.savefig(f"mnist_tsne_{i}_anomaly.png")
        print(result)
    print(str_result)


import urllib.request as req
import simplejson as json
import os.path

# url

url = "https://api.github.com/repositories"

# 경로 & 파일명
savename = "/Users/armian/Inflearn/workspace/section4/repo.json"

if not os.path.exists(savename):
    req.urlretrieve(url, savename)

items = json.load(open(savename, 'r', encoding="utf-8"))           # 파일로 부터 바로 역직렬화
#items = json.loads(open(savename, 'r', encoding="utf-8").read())  # 파일 -> 문자열로부터 역직렬화

#출력
for item in items:
    print(item["full_name"]+ "   -   " + item["owner"]["url"])


import pygame
import os
import math

from AnimatedSprite import AnimatedSprite
from Dust import Dust

class Game:

    def __init__(self, input_handler, win):
        self.win = win
        self.objects = [[],[],[],[],[]]
        self.ticktack_receivers = []
        self.mouse_tracking = []
        self.input_handler = input_handler
        self.game_window = (1024,768)
        self.game_window = (1280,720)
        self.window_center = pygame.math.Vector2(self.game_window[0]//2, self.game_window[1]//2)
        self.clock = pygame.time.Clock()
        self.focused = []
        self.time = 0
        self.initial_zoom = 0.00001
        self.zoom = self.initial_zoom
        self.zoom_speed = 15
        self.zoom_locked = None
        self.zoom_enabled = True
        self.stop_time = None
        self.camera = [ 0, 0 ]
        self.animations = {
            'explosion' : AnimatedSprite( "explosion.png", 8, 6, 96, False ),
            'spark' : AnimatedSprite( "spark.png", 4, 4, 16, False ),
            'shield' : AnimatedSprite( "shield.png", 5, 5, 96, True ),
        }
        self.paused = True
        self.key_handlers = {}
        self.font = pygame.font.Font('assets/Komika-Display/Komika_display.ttf', 16)
        self.drag_rect = None
        self.reset_fieldview()
        self.team = None

    def reset_fieldview( self ) :
        max_width = self.game_window[0]
        max_height = self.game_window[1]
        self.optimal_fieldview = pygame.Rect(-max_width//2,-max_height//2,max_width,max_height)
        self.target_zoom = 1

    def add_object(self, obj):
        self.objects[obj.Z].append( obj )

    def add_ticktack_receiver(self, receiver) :
        self.ticktack_receivers.append(receiver)

    def add_command_receiver(self, receiver) :
        self.command_receivers.append(receiver)

    def remove_object(self, obj):
        if obj in self.objects[obj.Z] :
            self.objects[obj.Z].remove(obj)
        if obj in self.focused :
            self.focused.remove( obj )

    def get_focused( self ) :
        if len(self.focused) > 0 :
            return self.focused[0]
        else :
            return None

    def set_focused(self, focused) :
        self.focused = [ focused ]

    def push_focused(self, focused) :
        if focused == None :
            raise ValueError("focused can't be None")
        self.focused.insert( 0, focused )

    def pop_focused( self ) :
        if len(self.focused) > 0 :
            removed = self.focused.pop(0)
            removed.on_focus_lost()

    def get_collisions(self, pygame_rect ):
        collisions = []
        for obj in self.objects[0]:
            if obj.can_be_hit and obj.get_collision_rect().colliderect(pygame_rect):
                collisions.append(obj)
        return collisions

    def get_selections(self, pygame_rect ):
        selections = []
        for obj in self.objects[0]:
            if obj.can_be_hit and obj.get_collision_rect(1).colliderect(pygame_rect):
                selections.append(obj)
        return selections

    def get_objects_in_range(self, x, y, range) :
        res = []
        for obj in self.objects[0] :
            if obj.can_be_hit and obj.distance_to_xy(x, y) <= range :
                res.append( obj )
        return res

    def get_time( self ) :
        return self.time

    def get_visible_rectangle( self, layer, zoom = None ) :
        if zoom == None :
            zz = self.zoom
        else :
            zz = zoom
        if layer == 0 :
            z = 1
        elif layer == 1 :
            z = 2.5
        else :
            z = 5
        width = self.game_window[0] * z / zz
        height = self.game_window[1] * z / zz
        rect = pygame.Rect(0, 0, width, height)
        rect.center = self.camera
        return rect

    def move_to_contain( self, rect1, rect2 ) :
        "move rect1 so that it contains rect2"
        if rect1.right < rect2.right :
            rect1.move_ip(rect2.right - rect1.right, 0)
        elif rect2.left < rect1.left :
            rect1.move_ip(rect2.left - rect1.left, 0)
        if rect2.top < rect1.top :
            rect1.move_ip(0, rect2.top - rect1.top)
        elif rect1.bottom < rect2.bottom :
            rect1.move_ip(0, rect2.bottom - rect1.bottom)
        return rect1

    def compute_optimal_fieldview( self ) :
        if self.zoom_locked != None and self.zoom_locked > self.get_time() :
            return
        max_width = self.game_window[0]
        max_height = self.game_window[1]
        important_objects = [obj for obj in self.objects[0] if obj.is_important]
        if len(important_objects)==0 :
            print(f'compute_optimal_fieldview: NO IMPORTANT OBJECTS')
            self.reset_fieldview()
            return
        all_ships_fieldview = important_objects[0].get_rect()
        for obj in important_objects[1:] :
            all_ships_fieldview = all_ships_fieldview.union(obj.get_rect())
        all_fit = True
        if all_ships_fieldview.width <= max_width and all_ships_fieldview.height <= max_height :
            if self.target_zoom != 1 :
                self.toggle_zoom({'force':True})
                self.optimal_fieldview = all_ships_fieldview
        elif all_ships_fieldview.width > 2 * max_width or all_ships_fieldview.height >= 2 * max_height :
            all_fit = False
            if self.target_zoom != 1 :
                self.toggle_zoom({'force':True})
                self.optimal_fieldview.inflate_ip(-max_width, -max_height)
        elif self.target_zoom == 1 :
            self.optimal_fieldview.inflate_ip(max_width,max_height)
            max_widht = 2 * self.game_window[0]
            max_height = 2 * self.game_window[1]
            self.toggle_zoom({'force':True})
        else :
            max_width = 2 * self.game_window[0]
            max_height = 2 * self.game_window[1]
        focused_rect = all_ships_fieldview # guard
        if len(self.focused) > 0 :
            focused_rect = self.focused[0].get_rect().inflate(200,200)
            fv = all_ships_fieldview.union(focused_rect)
            if all_fit :
                if fv.width <= max_width and fv.height <= max_height :
                    all_ships_fieldview = fv
            else :
                if fv.width <= max_width and fv.height <= max_height :
                    all_ships_fieldview = fv
                else :
                    all_ships_fieldview = focused_rect
        if not self.optimal_fieldview.contains(all_ships_fieldview) :
            self.optimal_fieldview = self.move_to_contain(self.optimal_fieldview, all_ships_fieldview)
        self.optimal_fieldview.inflate_ip(
            self.game_window[0] / self.target_zoom - self.optimal_fieldview.width,
            self.game_window[1] / self.target_zoom - self.optimal_fieldview.height )
        assert self.optimal_fieldview.width == self.game_window[0] / self.target_zoom
        assert self.optimal_fieldview.height == self.game_window[1] / self.target_zoom

    def get_optimal_camera( self ) :
        return self.optimal_fieldview.center

    def compute_pans(self) :
        self.pan1 = (
            self.camera[0]-self.game_window[0]/2/self.zoom,
            self.camera[1]-self.game_window[1]/2/self.zoom )
        self.pan2 = (
            self.camera[0]/2.5/self.zoom-self.game_window[0]/2/2.5/self.zoom,
            self.camera[1]/2.5/self.zoom-self.game_window[1]/2/2.5/self.zoom)
        self.pan3 = (self.camera[0]/5-self.game_window[0]/2/self.zoom,self.camera[1]/5-self.game_window[1]/2/self.zoom)

    def pan_camera(self) :
        optimal_camera = self.get_optimal_camera()
        d = (optimal_camera[0]-self.camera[0], optimal_camera[1]-self.camera[1])
        recompute_pans = abs(d[0])>=1 or abs(d[1])>=1
        if not recompute_pans :
            return
        if abs(d[0]>10) or abs(d[1]>10) :
            self.camera[0] = self.approach_value(self.camera[0],optimal_camera[0],15)
            self.camera[1] = self.approach_value(self.camera[1],optimal_camera[1],15)
        else :
            d = ( max(1,d[0]/10) if d[0]>=0 else min(-1,d[0]/10),
                  max(1,d[1]/10) if d[1]>=0 else min(-1,d[1]/10) )
            self.camera[0] += d[0]
            self.camera[1] += d[1]
        if recompute_pans :
            self.compute_pans()

    def toggle_zoom(self, args) :
        if not self.zoom_enabled :
            return
        if args.get('force', False) or self.zoom == self.target_zoom :
            Dust.remove_dust(self)
            self.target_zoom = 1.5 - self.target_zoom
            Dust.make_dust(self, self.target_zoom)
        if args.get('lock',False) :
            self.zoom_locked = self.get_time() + 5000

    def toggle_pause(self) :
        print('Toggle pause')
        self.paused = not self.paused

    def get_display_xy( self, x, y, layer = 0 ) :
        "given game coords, give display coords"
        if layer == 0 :
            xy_rel_camera = ( x - self.camera[0], y - self.camera[1] )
            xy_from_layer = xy_rel_camera
            xy_from_zoom = ( self.zoom * xy_from_layer[0], self.zoom * xy_from_layer[1] )
            return (self.game_window[0] / 2 + xy_from_zoom[0], self.game_window[1] / 2 + xy_from_zoom[1])
        elif layer == 1 :
            xy_rel_camera = ( x - self.camera[0], y - self.camera[1] )
            xy_from_layer = ( xy_rel_camera[0] / 2.5, xy_rel_camera[1] / 2.5 )
            xy_from_zoom = ( self.zoom * xy_from_layer[0], self.zoom * xy_from_layer[1] )
            return (self.game_window[0] / 2 + xy_from_zoom[0], self.game_window[1] / 2 + xy_from_zoom[1])
        else :
            return (self.zoom*(x - self.pan3[0]), self.zoom*(y - self.pan3[1]))

    def get_screen_distance_from_center( self, x, y ) :
        p1 = pygame.math.Vector2(*self.get_display_xy( x, y ))
        return self.window_center.distance_to(p1)

    def get_screen_centerism( self, x, y ) :
        v = self.get_screen_distance_from_center(x,y)
        t = min(self.game_window[0]//2, self.game_window[1]//2)
        if v > t : return 0
        return (t-v) / t

    def get_xy_display( self, x, y, layer = 0 ) :
        "given display coords, give game coords (in layer 0)"
        return (
            x - self.game_window[0] / 2 / self.zoom + self.camera[0],
            y - self.game_window[1] / 2 / self.zoom + self.camera[1]
        )

    def get_display_rect( self, rect, layer ) :
        (left, top) = self.get_display_xy( rect.left, rect.top, layer )
        if layer == 0 :
            layer_factor = 1
        elif layer == 1 :
            layer_factor = 2.5
        else :
            layer_factor = 5
        width = rect.width * self.zoom / layer_factor
        height = rect.height * self.zoom / layer_factor
        return pygame.Rect( left, top, width, height )

    def compute_bounding_rect_old(self) :
        important_objects = [obj for obj in self.objects[0] if obj.is_important]

        if not important_objects:
            return None  # Return None if there are no important objects

        # Initialize the bounding rectangle with the first object's rectangle
        bounding_rect = important_objects[0].get_rect()

        # Expand the bounding rectangle to include all other important objects
        for obj in important_objects[1:]:
            bounding_rect.union_ip(obj.get_rect())

        print(f'important_objects: {len(important_objects)}, bounding_rect: {bounding_rect}')
        return bounding_rect

    def game_loop( self ):
        self.running = True
        while self.running:
            self.time += 1

            if self.stop_time != None and self.stop_time <= self.time :
                self.running = False

            self.clock.tick(60)  # Limit the game loop to 60 frames per second

            self.compute_optimal_fieldview()

            self.zoom = Game.approach_value(self.zoom, self.target_zoom, self.zoom_speed)

            self.win.fill((0, 0, 0))
            if self.input_handler.handle_input(len(self.mouse_tracking)>0) == False :
                self.running = False

            # world_surface = pygame.Surface((self.game_window[0]/self.zoom,self.game_window[1]/self.zoom))  # The surface that represents the game world

            self.compute_pans()

            #if not self.paused :
                # self.pan_camera(bounding_rect)
            self.pan_camera()

            for receiver in self.ticktack_receivers :
                receiver.ticktack()

            for z_list in reversed(self.objects) :
                for obj in z_list :
                    if not self.paused or not obj.affected_by_pause :
                        obj.ticktack()

            for z_list in reversed(self.objects) :
                for obj in z_list :
                    if obj.visible :
                        obj.repaint( self.win ) # span1, pan2, pan3)
                    # obj.repaint(world_surface, pan1, pan2, pan3 )
            focus_painted = False
            for obj in self.focused :
                if obj.focus_visible and not focus_painted :
                    obj.draw_select(self.win, (0,255,0))
                    focus_painted = True
                if obj.visible :
                    obj.repaint_focused( self.win )
            if self.drag_rect != None :
                pygame.draw.rect(self.win, (0,180,255), self.drag_rect, 1)

            # Resize world_surface to achieve a zoom effect
            # zoomed_surface = pygame.transform.smoothscale(world_surface, self.game_window)

            # Then draw zoomed_surface onto self.win
            # self.win.blit(zoomed_surface, (0, 0))

            pygame.display.flip()

    def get_animation(self, animation_name) :
        return self.animations[animation_name]

    def set_mouse_tracking(self, obj, tracking_enabled) :
        if tracking_enabled :
            self.mouse_tracking.append( obj )
        else :
            self.mouse_tracking.remove( obj )

    def mouse_track( self, x, y ) :
        dc = self.get_xy_display( x, y )
        for obj in self.mouse_tracking :
            if obj.get_collision_rect().collidepoint( *dc ) :
                obj.mouse_track( dc, (x,y) )

    def click( self, x, y ) :
        if len(self.focused) > 0 :
            print(f'focused click')
            gxy = self.get_xy_display( x, y )
            if self.focused[0].click( *gxy ) :
                return
            print(f'focused click ignored')
        print(f'unfocused click')
        game_coords = self.get_xy_display( x, y )
        objects_here = self.get_selections(pygame.Rect(*game_coords,1,1))
        objects_selectable = [ obj for obj in objects_here if obj.is_selectable ]
        if len(objects_selectable) > 0 :
            self.set_focused( objects_selectable[0] )

    def click3( self, x, y ) :
        if len(self.focused) > 0 :
            game_coords = self.get_xy_display( x, y )
            objects_here = self.get_selections(pygame.Rect(*game_coords,1,1))
            self.focused[0].default_action(game_coords, objects_here)

    def on_key_pressed(self, key, mod = None) :
        print(f"Game: on_key_pressed: {key}")
        handler = self.key_handlers.get( key )
        if handler != None :
            handler.on_key_pressed( key, mod )

    def register_key_handler(self, key, handler) :
        self.key_handlers[ key ] = handler

    def on_stop_request(self) :
        self.zoom_locked = self.get_time() + 1000
        for ol in self.objects :
            for o in ol :
                o.on_stop_request()
        for o in self.ticktack_receivers :
            o.on_stop_request()
        Dust.remove_dust(self)
        self.stop_time = self.get_time() + 50
        self.zoom = 1
        self.target_zoom = self.initial_zoom
        self.zoom_speed = 15

    def get_font( self ) :
        return self.font

    def set_camera(self, pos) :
        self.set_optimal_camera( pos )
        self.camera[0] = self.optimal_camera[0]
        self.camera[1] = self.optimal_camera[1]

    def set_optimal_camera(self, pos) :
        self.optimal_fieldview.center = pos
        self.optimal_camera = pos
        self.zoom_locked = self.get_time() + 1000

    def drag_start(self, pos) :
        self.drag_p1 = pos

    def drag_continue(self, pos) :
        self.drag_p2 = pos
        top_left_x = min(self.drag_p1[0], self.drag_p2[0])
        top_left_y = min(self.drag_p1[1], self.drag_p2[1])
        width = abs(self.drag_p1[0] - self.drag_p2[0])
        height = abs(self.drag_p1[1] - self.drag_p2[1])
        self.drag_rect = pygame.Rect(top_left_x, top_left_y, width, height)
        self.default_group.remove_all()
        for obj in self.objects[0] :
            if not obj.is_selectable or obj.is_hostile(self.team) :
                continue
            if obj.is_inside_display_rect(self.drag_rect) :
                self.default_group.add_ship(obj)
        if self.default_group.is_empty() :
            if len(self.focused) > 0 and self.focused[0]==self.default_group :
                self.pop_focused()
        else :
            self.set_focused( self.default_group )

    def drag_stop(self, pos) :
        self.drag_p1 = None
        self.drag_p2 = None
        self.drag_rect = None

    def set_team(self, team, default_group) :
        self.team = team
        self.default_group = default_group

    def set_default_group(self, default_group) :
        print(f"Set default group: {default_group.number}")
        self.default_group = default_group
        self.set_focused(self.default_group)

    @staticmethod
    def is_acute_angle(dir1, dir2):
        # Calculate the difference between the two directions
        diff = abs(dir1 - dir2) % 360
        # Adjust for the case where the angle crosses the 0/360 boundary
        if diff > 180:
            diff = 360 - diff
        # Check if the angle is acute
        return diff < 90

    @staticmethod
    def approach_value(value, target, delay) :
        speed = max(0.002,abs(value - target) / delay)
        if value < target :
            return min( target, value + min(speed, target - value) )
        elif value > target :
            return max( target, value - min(speed, value - target) )
        else :
            return value


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

import os
import random
import copy
import csv
import numpy as np
import time
import matplotlib.pyplot as plt
from collections import Counter
import sys

def readFile(fname):
    file = open("20newsgroups/" + fname , 'r');
    for line in file:
        temp = line.split(" ")
    return temp

def readDirectory(directory):
    data = []
    files = []
    for file in os.listdir(directory):
        if file != "index.csv" :
            data.append(readFile(file))
            files.append(file)
    return data, files

def preprocess(data):
    words  = []
    for doc in data:
        for word in doc:
            words.append(word)
    cntWords = Counter(words)
    uniqueWords = []
    for uniqueWord in cntWords:
        uniqueWords.append(uniqueWord)

    return words ,uniqueWords

def createCSV(lists):
    with open('topicwords.csv', 'w', newline='') as file:
     write = csv.writer(file)
     for i in range(K):
         write.writerow([i ,lists[i]])

def optimization(w,r,d,phiTrain):
    phiT = phiTrain.T
    a = 0.01
    I = np.eye(len(phiTrain[0]), dtype = float)
    x = phiT.dot(r)
    x = -x.dot(phiTrain) - I.dot(a)
    H = np.linalg.inv(x)
    g = phiT.dot(d) - w.dot(a)
    Wn = w - H.dot(g)
    return Wn

def LogisticRegression(phiTrain,phiTarget):
    wList = []
    position = []
    timee = []
    w = [0] * int(np.shape(phiTrain[0])[1])
    w = np.array(w)
    for i in range(len(phiTrain)):
        start = time.time()
        for j in range(500):
            phi = phiTrain[i]
            phi = np.array(phi)
            x = phi.dot(w)
            y = 1/(1 + np.exp(-x))
            t = phiTarget[i]
            d = t - y
            r = y.dot(1-y)
            Wn = optimization(w,r,d,phi)
            temp = w
            w = Wn
            wCon = np.sqrt(np.sum(np.square(w - temp)))/np.sqrt(np.sum(np.square(temp)))
            if wCon <= 0.001 or j == 499:
                position.append(j)
                end = time.time()
                duration = end - start
                timee.append(duration)
                break
        wList.append(w)
    return wList, position, timee

def generateData(phiTrainData):
    PhiTrainData = []
    PhiTargetData = []
    phiTrain = phiTrainData[:,:-1]
    phiTarget = phiTrainData[:,-1]
    for i in np.arange(0.1,1.1,0.1):
        PhiTrainData.append(phiTrain[:int(len(phiTrain)*i)])
        PhiTargetData.append(phiTarget[:int(len(phiTarget)*i)])

    return PhiTrainData, PhiTargetData

def calMeanStd(listt):
    mean = np.mean(np.array(listt),axis=0)
    std = np.std(np.array(listt),axis=0)

    return mean,std

def LogisticRegressionTest(phiTestData, wList):
    error = []
    phiTest = phiTestData[:,:-1]
    phiTestLabel = phiTestData[:,-1]
    for i in range(len(wList)):
        err = 0
        x = phiTest.dot(wList[i])
        y = 1/(1+np.exp(-x))
        yFinal = []
        for j in range(len(y)):
            if y[j] >= 0.5:
                yFinal.append(1)
            else:
                yFinal.append(0)
        for k in range(len(y)):
            if yFinal[k] != phiTestLabel[k]:
                err = err + 1
        error.append(1 - (err/len(phiTestLabel)))
    return error

def logistic(CD ,labels, alpha, method):
    data = []
    if method == 1:
        for i in range(D):
            temp = [0] * K
            data.append(temp)
        for i in range(D):
            for j in range(K):
                num = CD[i][j] + alpha
                den = K*alpha + sum(CD[i])
                data[i][j] = num/den
    else :
        data = CD
    data = np.array(data)
    labels = np.array(labels).reshape(len(labels),1)
    errorList = []
    for i in range(30):
        index = np.random.permutation(len(labels))
        data, labels =  data[index], labels[index]
        data1 = np.concatenate((data, labels), axis = 1)
        phiTrainData = data1[:int(len(data1)*(0.67))]
        phiTestData = data1[int(len(data)*(0.67)):]

        PhiTrainData, PhiTargetData = generateData(phiTrainData)
        wList, position, timee  = LogisticRegression(PhiTrainData, PhiTargetData)
        error = LogisticRegressionTest(phiTestData, wList)
        errorList.append(error)

    mean, std = calMeanStd(errorList)
    return mean, std

def calCT(K, V, words, uniqueWords, tags):
    ct = []
    for i in range(K):
        temp = [0] * V
        ct.append(temp)

    for i in range(len(words)):
        for j in range(V):
            if words[i] == uniqueWords[j]:
                for k in range(len(tags)):
                    if topicList[i] == tags[k]:
                        ct[k][j] += 1
                        break
    return ct

def calCD(D, K, topics, tags):
    cd = []
    for i in range(D):
        temp = [0] * K
        cd.append(temp)

    for i in range(len(topics)):
        for j in range(len(topics[i])):
            for k in range(len(tags)):
                if topics[i][j] == tags[k]:
                    cd[i][k] += 1
                    break
    return cd

def gibbsSampling(CD, CT, words, uniqueWords, pi, K, doc, topicList):
    iterations = 500
    beta = 0.01
    alpha = 5/K
    prob = [0] * K

    for i in range(iterations):
        #print(i)
        for j in range(len(words)):
            index = pi[j]
            currWord = words[index]
            currWordIndex = uniqueWords.index(currWord)
            document = doc[pi[j]]
            topic = topicList[pi[j]]
            CD[document][topic] -= 1
            CT[topic][currWordIndex] -= 1

            for k in range(K):
                numerator = (CT[k][currWordIndex] + beta) * (CD[document][k] + alpha)
                temp1 = sum(CT[k])
                temp2 = sum(CD[document])
                denominator = (V*beta + temp1) * (K*alpha + temp2)
                prob[k] = numerator/denominator

            temp3 = sum(prob)
            prob = list(np.array(prob)/temp3)
            ran = random.uniform(0,1)
            temp4 = 0
            for x in range(len(prob)):
                temp4 = sum(prob[:x+1])
                if ran <= temp4 :
                    ind = x
                    break

            topic = ind
            topicList[pi[j]] = topic
            CD[document][topic] += 1
            CT[topic][currWordIndex] += 1

    return CD, CT, topicList

def alternateMethod(D, V, data, uniqueWords):
    altMethod = []
    for i in range(D):
        temp = [0] * V
        altMethod.append(temp)

    for i in range(len(data)):
        for j in range(len(data[i])):
            for k in range(len(uniqueWords)):
                if data[i][j] == uniqueWords[k]:
                    altMethod[i][k] += 1
                    break
    return altMethod

def toCSV(uniqueWords, CT):
    temp = copy.deepcopy(CT)
    lists = []
    for i in range(len(CT)):

        temp[i].sort(reverse = True)
        temp1 = temp[i]
        max1 = temp1[0]
        ind1 = CT[i].index(max1)
        tags1 = uniqueWords[ind1]
        max2 = temp1[1]
        ind2 = CT[i].index(max2)
        tags2 = uniqueWords[ind2]
        max3 = temp1[2]
        ind3 = CT[i].index(max3)
        tags3 = uniqueWords[ind3]
        max4 = temp1[3]
        ind4 = CT[i].index(max4)
        tags4 = uniqueWords[ind4]
        max5 = temp1[4]
        ind5 = CT[i].index(max5)
        tags5 = uniqueWords[ind5]
        temp2 = [tags1, tags2,tags3,tags4,tags5]
        lists.append(temp2)
    createCSV(lists)

def plotGraph(mean1, std1, method1, mean2, std2, method2):
    x = [i for i in np.arange(0.1,1.1,0.1)]
    plt.errorbar(x, mean1, std1, label = method1)
    plt.errorbar(x , mean2, std2, label = method2)
    plt.xlabel("DataSize")
    plt.ylabel("Accuracy")
    plt.legend(loc="lower right")
    plt.show()

directory = os.path.dirname(os.path.abspath(__file__))
data , files = readDirectory(directory+"/20newsgroups")
words , uniqueWords = preprocess(data)
V = len(uniqueWords)
D = 200
K = 20
pi = []
for i in range(len(words)):
    pi.append(i)
random.shuffle(pi)

doc = []
for i in range(len(data)):
    for j in range(len(data[i])):
        doc.append(i)

alpha = 5/K

topicList = []
topics = []
for i in range(len(data)):
    temp = []
    for j in range(len(data[i])):
        r = random.randint(0,19)
        topicList.append(r)
        temp.append(r)
    topics.append(temp)

tags = [i for i in range(0,20)]
CD = calCD(D, K, topics, tags)
CT = calCT(K, V, words, uniqueWords, tags)
CD, CT, topicList = gibbsSampling(CD, CT, words, uniqueWords, pi, K, doc, topicList)

toCSV(uniqueWords , CT)

label = np.genfromtxt(directory + "20newsgroups/index.csv", delimiter = ",")

labels = []
for i in range(len(files)):
    labels.append(label[int(files[i]) - 1][1])

mean1 , std1 = logistic(CD , labels, alpha, 1)

data2 = alternateMethod(D, V, data, uniqueWords)

mean2 , std2 = logistic(data2 , labels, alpha, 0)

method1 = "LDA"
method2= "Bag of Words"
plotGraph(mean1 , std1, method1, mean2, std2, method2)


from django.conf.urls import url
from django.contrib import admin
from MyNewApp.views import *
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', Login, name="login"),
    url(r'^Register/$', Register, name="register"),
    url(r'^logout/$', Logout, name="logout"),
    url(r'^india/(?P<Username>[\w-]+)/$', UserProfile, name="UserProfile"),

    url(r'^in/Edit/(?P<Username>[\w-]+)/$', Update_User_Details, name="UpdateUserProfile"),

    url(r'^all_professionals/(?P<what>[\w-]+)/$', All_Profession, name="professional"),
    url(r'^professional_html/(?P<what>[\w-]+)/$', All_Professional_Html, name="professional_html"),
    url(r'^connection/(?P<action>[\w-]+)/(?P<u_id>[0-9]+)/$', Manage_your_connections, name="connections"),

    url(r'^add_company/$', Add_Company, name="addCompany"),
    url(r'^your_company_details/$', CompanyDetails, name="CompanyDetails"),
    url(r'^post_new_blog/$', NewPost, name="post"),
    url(r'^likes/(?P<b_id>[\w-]+)/(?P<Username>[\w-]+)/$', Like_By_Me, name="likes"),

    url(r'^donate_amount/(?P<u_id>[\w-]+)/$', SendPaymentRequest, name="donate"),
    url(r'^paycheck/$', PaymentCheck, name="paycheck"),

] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)


from django.shortcuts import render
from .models import *

def homepage(request):
    work = Work.objects.all()
    skill = Skill.objects.all()
    award = Award.objects.all()
    info = Info.objects.first()

    context = {
        'info': info,
        'work': work,
        'award': award,
        'skill': skill,
    }
    return render(request, 'home_page/index.html', context)


from typing import List

class Solution:
    def minCashFlow(self, n : int, g : List[List[int]]) -> List[List[int]]:
        # code here
        ans=[[0 for _ in range(n)] for _ in range(n)]
        graph=[[] for _ in range(n)]
        amounts=[0 for i in range(n)]
        for i in range(n):
            for j in range(n):
                if g[i][j]:
                    graph[i].append((j,g[i][j]))
        for i in range(n):
            size=len(graph[i])
            for j in range(size):
                v =  graph[i][j][0]
                w=  graph[i][j][1]
                amounts [i] -= w
                amounts[v] += w
        for i in range(n):
            maxCredit=maxDebit=0
            credit=debit=None
            for j in range(n):
                if amounts[j] > maxCredit:
                    credit=j
                    maxCredit=amounts[j]
                if amounts[j] < maxDebit:
                    debit=j
                    maxDebit=amounts[j]
            if maxCredit and maxDebit:
                if maxCredit >= abs(maxDebit):
                    ans[debit][credit]= abs(maxDebit)
                    amounts[debit] = 0
                    amounts[credit] -= abs(maxDebit)
                else:
                    ans[debit][credit]= maxCredit
                    amounts[debit] += maxCredit
                    amounts[credit] = 0
        return ans

#{
 # Driver Code Starts
class IntMatrix:
    def __init__(self) -> None:
        pass
    def Input(self,n,m):
        matrix=[]
        #matrix input
        for _ in range(n):
            matrix.append([int(i) for i in input().strip().split()])
        return matrix
    def Print(self,arr):
        for i in arr:
            for j in i:
                print(j,end=" ")
            print()

if __name__=="__main__":
    t = int(input())
    for _ in range(t):

        n = int(input())

        g=IntMatrix().Input(n, n)

        obj = Solution()
        res = obj.minCashFlow(n, g)

        IntMatrix().Print(res)

# } Driver Code Ends


#!/usr/bin/env python
from __future__ import division
import sys
import os
import logging
import datetime

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from yelp.data.collection import MongoQuery

LOG_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'logs'))

class UserUpdater:
    def __init__(self):
        self.query = MongoQuery()
        self.users = None
        self.logger = logging.Logger('UserUpdateLogger', level=logging.INFO)
        self.logger.addHandler(logging.FileHandler(filename=os.path.join(LOG_PATH, 'user_update.log'), mode='a+'))

    def get_all_users(self):
        if not self.users:
            where = [
                ('review_count', {"$ne": 0}),
                ('average_usefulness', {"$exists": False})
            ]
            projection = ['user_id', 'votes', 'review_count']
            collection = 'user'
            self.users = list(self.query.find_all_by(collection_name=collection, query_list=where, fields=projection))

        return self.users

    def update_user(self, user, avg_usr_usefulness):
        runtime = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')

        user_id = user['user_id']
        query_list = [('user_id', user_id)]
        set_list = [('average_usefulness', avg_usr_usefulness)]
        col = 'user'

        self.logger.info(runtime + " - Set average_usefulness = " + avg_usr_usefulness + " to user with ID: " + user_id)
        self.query.find_and_update(col, query_list, set_list)

    def update_all_users_with_user_usefulness(self):
        users = self.get_all_users()
        print(len(users))

        counter = 1
        for user in users:
            counter += 1

            avg_usr_usefulness = self.calculate_average_usefulness(user)
            self.update_user(user, avg_usr_usefulness)

            if counter % 1000 == 0:
                print(str(counter) + " users updated.")

        self.logger.info("Execution finished. " + str(counter) + " users updated.")
        return users

    @staticmethod
    def calculate_average_usefulness(user):
        return "%.2f" % (user['votes']['useful'] / user['review_count'])

if __name__ == '__main__':
    run_time = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')

    user_updater = UserUpdater()

    user_updater.logger.info(run_time + ' - Starting to update users with average_usefulness')
    user_updater.update_all_users_with_user_usefulness()


import pyarrow.parquet as pq

COLUMNS = "columns"

def apply(path, parameters=None):
    """
    Import a Parquet file

    Parameters
    -------------
    path
        Path of the file to import
    parameters
        Parameters of the algorithm, possible values:
            columns -> columns to import from the Parquet file

    Returns
    -------------
    df
        Pandas dataframe
    """
    if parameters is None:
        parameters = {}

    columns = parameters[COLUMNS] if COLUMNS in parameters else None

    if columns:
        columns = [x.replace(":", "AAA") for x in columns]
        df = pq.read_pandas(path, columns=columns).to_pandas()
    else:
        df = pq.read_pandas(path, columns=columns).to_pandas()
    df.columns = [x.replace("AAA", ":") for x in df.columns]

    return df


from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect
from django.urls import reverse
from .models import Libro
from .forms import LibroForm, UserRegisterForm
from django.contrib.auth.forms import UserCreationForm
from django.contrib import messages

# Create your views here.
def index (request):
    return render(request, 'Inventario/index.html',{
        'libro': Libro.objects.all()
    })

def view_producto(request, id):
    libro=Libro.objects.get(px=id)
    return HttpResponseRedirect(reverse('index'))

def add(request):
    if request.method == 'POST':
        form = LibroForm(request.POST)
        if form.is_valid():
            nuevo_nombre_libro = form.cleaned_data['nombre_libro']
            nuevo_nombre_autor = form.cleaned_data['nombre_autor']
            nueva_categoria = form.cleaned_data['categoria']
            nuevo_precio = form.cleaned_data['precio']
            nuevo_codigo = form.cleaned_data['codigo']

            nuevolibro = Libro(
                nombre_libro = nuevo_nombre_libro,
                nombre_autor = nuevo_nombre_autor,
                categoria = nueva_categoria,
                precio = nuevo_precio,
                codigo = nuevo_codigo,
                estado = 'A'
            )

            nuevolibro.save()
            return render(request, 'inventario/add.html',{
                'form':LibroForm(),
                'success':True
            })
    else:
        form = LibroForm()
        return render(request,'inventario/add.html',{
            'form':LibroForm(),
        })

def edita(request,id):
    if request.method == 'POST':
        producto = Libro.objects.get(pk=id)
        form = LibroForm(request.POST, instance=producto)
        if form.is_valid():
            form.save()
            return render(request, 'inventario/edit.html',{
                'form':form,
                'success':True
            })
    else:
        producto = Libro.objects.get(pk=id)
        form = LibroForm(instance=producto)

    return render(request, 'inventario/edit.html',{
        'form':form
    })

def elimina(request,id):
    if request.method == 'POST':
        producto = Libro.objects.get(pk=id)
        producto.estado='I'
        producto.save()
    return HttpResponseRedirect(reverse('index'))

def registrar(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            usuario = form.cleaned_data['username']
            form.save()
            messages.success(request, f'Usuario {usuario} creado')
            return redirect(index)
    else:
        form = UserRegisterForm()
    contex = { 'form':form}
    return render(request, 'Inventario/register.html', contex)


import os
import pytz
import yaml
from dateutil import parser
from django import template
from django.conf import settings
from collections import OrderedDict
from django.utils.safestring import mark_safe
from lava_scheduler_app.models import TestJob
from lava_scheduler_app.models import (
    DeviceDictionary,
    DeviceDictionaryTable,
    JobPipeline,
    PipelineStore,
)

register = template.Library()

@register.filter
def get_priority_select(current):
    select = ""
    val = TestJob.PRIORITY_CHOICES
    for priority, label in val:
        check = " checked" if priority == current else ""
        default = " [default]" if current != 50 and priority == 50 else ""
        select += ''
        select += '<input type="radio" name="priority" style="..." id="%s" value="%d"%s>%s%s</input>
' %\
                  (label.lower(), priority, check, label, default)
        select += ''
    return mark_safe(select)

@register.filter
def get_type(value):
    """
    Detects iterable types from not iterable types
    enough for the templates to work out if it is a value or a key.
    """
    if type(value) == str:
        return 'str'
    if type(value) == unicode:
        return 'str'
    if type(value) == bool:
        return 'str'
    if type(value) == int:
        return 'str'
    if type(value) == dict:
        return 'dict'
    if type(value) == list:
        return 'list'
    return type(value)

@register.filter
def get_item(dictionary, key):
    return dictionary.get(key)

@register.filter
def get_device_dictionary(data):
    key = os.path.basename(os.path.dirname(data))
    device_dict_obj = DeviceDictionaryTable.objects.get(id=key)
    device_dict = device_dict_obj.lookup_device_dictionary()
    return device_dict.to_dict()

@register.filter
def get_pipeline_store(data):
    key = os.path.basename(os.path.dirname(data))
    device_dict_obj = PipelineStore.objects.get(id=key)
    device_dict = device_dict_obj.lookup_job_pipeline()
    return device_dict.to_dict()

@register.filter
def get_device_parameters(data, key):
    if type(data) == str:
        return data
    if type(data) == dict:
        if type(key) == str and key in data:
                return data.get(key)
        return key.keys()
    return (type(data), type(key), data)

@register.filter
def get_yaml_parameters(parameters):
    # FIXME: it should be possible to dump this dict as YAML.
    try:
        ret = yaml.safe_dump(parameters, default_flow_style=False, canonical=False, default_style=None)
    except:
        return parameters
    return ret

@register.filter
def get_settings(value):
    if hasattr(settings, value):
        return getattr(settings, value)

def _get_pipeline_data(pipeline, levels):
    """
    Recursive check on the pipeline description dictionary
    """
    for action in pipeline:
        levels[action['level']] = {
            'name': action['name'],
            'description': action['description'],
            'summary': action['summary'],
            'timeout': action['timeout'],
        }
        if 'url' in action:
            levels[action['level']].update({'url': action['url']})
        if 'pipeline' in action:
            _get_pipeline_data(action['pipeline'], levels)

@register.assignment_tag()
def get_pipeline_sections(pipeline):
    """
    Just a top level view of the pipeline sections
    """
    sections = []
    for action in pipeline:
        if 'section' in action:
            sections.append({action['section']: action['level']})
    return sections

@register.assignment_tag()
def get_pipeline_levels(pipeline):
    """
    Retrieve the full set of action levels in this pipeline.
    """
    levels = OrderedDict()
    _get_pipeline_data(pipeline, levels)
    return levels

@register.assignment_tag()
def parse_timestamp(time_str):
    """
    Convert the pipeline log timestamp into datetime
    :param time_str: timestamp generated by the log handler
       of the form 2015-10-13T08:21:48.646202
    :return: datetime.datetime object or None
    """
    try:
        retval = parser.parse(time_str, ignoretz=True)
    except (AttributeError, TypeError):
        return None
    return pytz.utc.localize(retval)

@register.assignment_tag()
def logging_levels(request):
    levels = ['info', 'warning', 'exception']
    if 'info' in request.GET and request.GET['info'] == 'off':
        del levels[levels.index('info')]
    if 'debug' in request.GET and request.GET['debug'] == 'on':
        levels.append('debug')
    if 'warning' in request.GET and request.GET['warning'] == 'off':
        del levels[levels.index('warning')]
        del levels[levels.index('exception')]
    return levels

@register.filter()
def dump_exception(entry):
    data = [entry['exception']]
    if 'debug' in entry:
        data.append(entry['debug'])
    return yaml.dump(data)

@register.filter()
def result_url(result_dict, job_id):
    if not isinstance(result_dict, dict):
        return None
    if 'test_definition' in result_dict:
        testdef = result_dict['test_definition']
        testcase = None
        for key, _ in result_dict.items():
            if key == 'test_definition':
                continue
            testcase = key
            break
        # 8125/singlenode-intermediate/tar-tgz
        return mark_safe('/results/%s/%s/%s' % (
            job_id, testdef, testcase
        ))
    elif len(result_dict.keys()) == 1:
            # action based result
            testdef = 'lava'
            if isinstance(result_dict.values()[0], OrderedDict):
                testcase = result_dict.keys()[0]
                return mark_safe('/results/%s/%s/%s' % (
                    job_id, testdef, testcase
                ))
    else:
        return None

@register.assignment_tag()
def result_name(result_dict):
    if not isinstance(result_dict, dict):
        return None
    testcase = None
    testresult = None
    if 'test_definition' in result_dict:
        testdef = result_dict['test_definition']
        for key, value in result_dict.items():
            if key == 'test_definition':
                continue
            testcase = key
            testresult = value
            break
        # 8125/singlenode-intermediate/tar-tgz
        return mark_safe('%s - %s - %s' % (
            testdef, testcase, testresult
        ))
    elif len(result_dict.keys()) == 1:
        # action based result
        testdef = 'lava'
        if isinstance(result_dict.values()[0], OrderedDict):
            testcase = result_dict.keys()[0]
            if 'success' in result_dict.values()[0]:
                testresult = 'pass'
            if 'status' in result_dict.values()[0]:
                testresult = 'pass'  # FIXME
            # 8125/singlenode-intermediate/tar-tgz
            return mark_safe('%s - %s - %s' % (
                testdef, testcase, testresult
            ))
    else:
        return None


#!/usr/bin/python3
"""
The main running scripts which executes all the other helper modules
"""
import sys
import time

import login
import soup
import requests
import config
import get_project
import buildReadme
import buildFiles

def main():
    """
    Code entry points
    """

    url = config.get_url()
    headers = config.get_headers()

    if len(sys.argv) == 2:
        projectName = sys.argv[1]
    else:
        projectName = input("Enter the required project name: ")

    with requests.Session() as session:
        dashBoard = login.login(session)
        #print(dashBoard.text)
        #exit()
        if not login.check_login(dashBoard.text):
            config.remove_login_credentials()
            #print("Invalid user name or password")
            exit(1)
        projectPage = get_project.get_project(projectName, session, dashBoard)
        #projectText = soup.get_tag(projectPage.text, "body")

        #print(projectText.h1)
        #readme = buildReadme.ReadmeBuilder(projectPage.text, projectName)
        #readme.parse()
        files = buildFiles.FileBuilder(projectPage.text)
        files.parse()

if __name__ == "__main__":
    start = time.time()
    main()
    end = time.time()
    print("Time consumed Buillding README.md:",
          " {:4f} seconds".format(end - start))


from DAL.JSONDAL import JSONDal
from common import BIG_PIPE_NAME,  SMALL_PIPE_NAME, CURRENT_DATE_KEY
from data_reader import get_config, get_and_validate_input_date, get_models
from csv_data_writer import write_output_data
from pipes.big_pipe import BigPipe
from pipes.small_pipe import SmallPipe

if __name__ == '__main__':
    print("Starting Program.")
    config = get_config()
    config[CURRENT_DATE_KEY] = get_and_validate_input_date(config)
    print("Finished reading configuration.")
    pipes = {
        BIG_PIPE_NAME: BigPipe(config)
        , SMALL_PIPE_NAME: SmallPipe(config)
    }
    print("Validating date is valid.")
    print("Date is valid.")
    dal = JSONDal(config)
    print("Reading and aggregating models.")
    models = get_models(config, dal)
    print("Finsihed aggregating models.")
    out_rows = []
    for id, model in models.items():
        for name, pipe in pipes.items():
            if pipe.validate(model):
                print("{} goes to  {}".format(id, pipe.name))
                pipe.process(model)
                out_rows.extend(pipe.save_and_get_row(model))
                break
    print("Finished processing models.")
    print("Writing data to database.")
    write_output_data(config, out_rows)
    print("Creating csv output")
    dal.update_models(models)
    dal.flush()
    print("Finished.")


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

# In[1]:

import pandas as pd
import numpy as np
import os
from itertools import product
from tqdm.notebook import tqdm
import calendar

# In[2]:

DATA_FOLDER = '../Data/'

sales           = pd.read_csv(os.path.join(DATA_FOLDER, 'sales_train.csv.gz'))
items           = pd.read_csv(os.path.join(DATA_FOLDER, 'items.csv'))
# item_categories = pd.read_csv(os.path.join(DATA_FOLDER, 'item_categories.csv'))
# shops           = pd.read_csv(os.path.join(DATA_FOLDER, 'shops.csv'))
test            = pd.read_csv(os.path.join(DATA_FOLDER, 'test.csv.gz'))
# samplesub       = pd.read_csv(os.path.join(DATA_FOLDER, 'sample_submission.csv.gz'))

# In[3]:

sales['item_category_id'] = sales.item_id.map(items.item_category_id)
sales.item_cnt_day = sales.item_cnt_day.clip(0, 20)
sales = sales[sales.item_price < 30000]

# In[4]:

# index_cols = ['date_block_num', 'shop_id', 'item_id']

# train = []
# block = sales.date_block_num.unique()

# for shop in tqdm(sales.shop_id.unique()):
#     cur_items = sales.loc[sales.shop_id == shop, 'item_id'].unique()
#     train.append(
#         np.array(list(
#             product(*[block, [shop], cur_items])
#         ))
#     )

# train = pd.DataFrame(np.vstack(train), columns=index_cols, dtype=np.int32)

# In[5]:

index_cols = ['date_block_num', 'shop_id', 'item_id']

train = []
# block = sales.date_block_num.unique()

for block in tqdm(sales.date_block_num.unique()):
    cur_items = sales.loc[sales.date_block_num == block, 'item_id'].unique()
    cur_shops = sales.loc[sales.date_block_num == block, 'shop_id'].unique()
    train.append(
        np.array(list(
            product(*[[block], cur_shops, cur_items])
        ))
    )

train = pd.DataFrame(np.vstack(train), columns=index_cols, dtype=np.int32)

# In[6]:

test['date_block_num'] = np.full(test.shape[0], 34)

# In[7]:

group = sales.groupby(index_cols).agg({'item_cnt_day': 'sum'})
group.columns = ['item_cnt_month']
group.reset_index(inplace= True)
train = pd.merge(train, group, on= index_cols, how= 'left')
train.item_cnt_month = train.item_cnt_month.fillna(0).clip(0, 20)

# In[8]:

# train = train.sample(3000000)

# In[9]:

length_train = train.shape[0]
length_train

# In[10]:

train = train.append(test.drop(columns=['ID']), ignore_index=True)

# In[11]:

train.shape

# In[12]:

y = train.item_cnt_month[:length_train]
# train.drop(columns=['item_cnt_month'], inplace=True)

# In[13]:

train.head()

# In[14]:

def num_days(block):
    y = int(2013 + block/12)
    m = 1 + block % 12
    return max(calendar.monthcalendar(y, m)[-1])

def wday(block, d):
    y = int(2013 + block/12)
    m = 1 + block % 12
    cal = calendar.monthcalendar(y, m)
    return sum([1 for w in cal if w[d]])

# In[15]:

def lag_feature(df, lags, col='item_cnt_month'):
    tmp = df[['date_block_num','shop_id','item_id',col]]
    for i in tqdm(lags):
        name = col+'_lag_'+str(i)
        shifted = tmp.copy()
        shifted.columns = ['date_block_num','shop_id','item_id', name]
        shifted['date_block_num'] += i
        df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'], how='left')
        df[name] = df[name].astype('float16')
    return df

# In[16]:

def add(train, index_cols, name, col='item_cnt_month', func='mean'):
    group = train[train.date_block_num.isin(sales.date_block_num.unique())].groupby(index_cols).agg({col: func})
    group.columns = [name]
    group.reset_index(inplace= True)
    train = pd.merge(train, group, on= index_cols, how= 'left')
    train[name] = train[name].fillna(0).astype('float16')
    return train

# In[17]:

train['year'] = (train.date_block_num/12 + 2013).astype(int).astype('int32')
train['month'] = (train.date_block_num % 12 + 1).astype(int).astype('int32')

sales['month'] = (sales.date_block_num % 12 + 1).astype(int).astype('int32')

# In[18]:

train['num_days'] = train.date_block_num.map(pd.Series([num_days(i) for i in range(40)])).astype('int32')

train['num_sat'] = train.date_block_num.map(pd.Series([wday(i, 5) for i in range(40)])).astype('int32')
# for d, wk in enumerate(['mon', 'tue', 'wed', 'thur', 'fri', 'sat', 'sun']):
#     train['num_'+ wk] = train.date_block_num.map(pd.Series([wday(i, d) for i in range(40)]))

# In[19]:

prices       = sales.groupby('item_id' ).item_price.mean()
prices_month = sales.groupby('month'   ).item_price.mean()
prices_shop  = sales.groupby('shop_id' ).item_price.mean()

train['item_category_id'] = train.item_id.map(items.item_category_id).astype('int32')
train['price'] = train.item_id.map(prices).astype('float16')
train['price_month'] = train.month.map(prices_month).astype('float16')
train['price_shop'] = train.shop_id.map(prices_shop).astype('float16')

# ## target encoding

# In[20]:

# [train.date_block_num.isin(sales.date_block_num.unique())]
block_sales = sales.date_block_num.unique()

count_month = train[:length_train].groupby('month'  ).item_cnt_month.mean()
count_shop  = train[:length_train].groupby('shop_id').item_cnt_month.mean()
count_item  = train[:length_train].groupby('item_id').item_cnt_month.mean()

train['cnt_month'] = train.month.map(count_month).astype('float16')
train['cnt_shop'] = train.shop_id.map(count_shop).astype('float16')
train['cnt_item'] = train.item_id.map(count_item).astype('float16')

# In[21]:

# train = add(train, ['date_block_num', 'item_id'], 'avg_cnt_month-item')
# train = add(train, ['date_block_num', 'shop_id'], 'avg_cnt_month-shop')
train = add(train, ['item_id', 'shop_id'], 'avg_cnt_item-shop')

train = add(train, ['month', 'item_id'], 'avg_cnt_month')
train = add(train, ['month', 'shop_id'], 'avg_cnt_shop')

# In[22]:

## lag
# for col in [
#     'item_cnt_month',
#     'cnt_shop',
#     'cnt_month',
#     'cnt_item',
#     'avg_cnt_month-item',
#     'avg_cnt_month-shop',
#     'avg_cnt_item-shop'
# ]:
#     train = lag_feature(train, [1, 2, 3, 12, 24], 'item_cnt_month')

# ## impute

# In[23]:

train.price = train.price.fillna(train.groupby('item_category_id').price.transform('mean'))
train.cnt_item = train.cnt_item.fillna(train.groupby('item_category_id').cnt_item.transform('mean'))

# In[24]:

ZD = train.copy()
##ZD = pd.get_dummies(ZD, columns= ['year', 'month', 'num_days', 'num_sat', 'shop_id', 'item_category_id'])

# In[25]:

test = ZD[length_train:]
train = ZD[:length_train]

# In[26]:

def information(rtd):
    info = pd.DataFrame({'Column': rtd.columns})
    info['nunique'] = rtd.nunique().values
    info['dtypes']  = rtd.dtypes.values
    info['isNull']  = rtd.isna().sum().values
    info['mode']    = rtd.mode().values[0]
    return info.set_index('Column')

# In[27]:

train.to_hdf('../Dump/train2.h5', 'train', 'w')
test.to_hdf('../Dump/test2.h5', 'test', 'w')

# In[28]:

information(test)

# In[29]:

##train.info()

# In[ ]:


""" TensorMONK :: layers :: ConvolutionalSAE """

__all__ = ["ConvolutionalSAE", ]

import torch
import torch.nn as nn
import torch.nn.functional as F
from .convolution import Convolution

class ConvolutionalSAE(nn.Module):
    r"""Base block for Stacked Convolutional Auto-encoder """
    def __init__(self, tensor_size, filter_size, out_channels, strides=(1, 1),
                 pad=True, activation="relu", dropout=0., normalization=None,
                 pre_nm=False, groups=1, weight_norm=False, equalized=False,
                 shift=False, bias=False, dropblock=True, learningRate=0.1,
                 **kwargs):

        super(ConvolutionalSAE, self).__init__()
        self.encoder = Convolution(tensor_size, filter_size, out_channels,
                                   strides, pad, activation, dropout,
                                   normalization, pre_nm, groups, weight_norm,
                                   equalized, shift, bias, dropblock, **kwargs)
        self.decoder = Convolution(self.encoder.tensor_size, filter_size,
                                   out_channels, strides, pad, activation,
                                   dropout, normalization, pre_nm, groups,
                                   weight_norm, equalized, shift, bias,
                                   dropblock, transpose=True,
                                   maintain_out_size=True, **kwargs)
        self.decoder.tensor_size = tensor_size

        self.Optimizer = torch.optim.SGD(self.parameters(), lr=learningRate)
        self.tensor_size = self.encoder.tensor_size

    def forward(self, tensor):
        features = self.encoder(tensor.detach())

        if self.training:
            self.Optimizer.zero_grad()
            loss = F.mse_loss(self.decoder(features), tensor.detach())
            loss.backward()
            self.Optimizer.step()
        return features

    def flops(self):
        return self.encoder.flops() + self.decoder.flops()

# from tensormonk.layers import Convolution
# tensor_size = (1, 3, 10, 10)
# tensor = torch.rand(*tensor_size)
# test = ConvolutionalAE(tensor_size, (3, 3), 16, (2, 2), True,
#                        "relu", 0.1, True, False)
# test.train()
# test(tensor).size()


from bs4 import BeautifulSoup
import re

pattern = r"privacy policy|個人情報|プライバシーポリシー"

with open('./test.html') as f:
    soup = BeautifulSoup(f.read(), 'lxml')

    for link in soup.findAll('a', text=re.compile(pattern, re.IGNORECASE)):
        print(link['href'])


# We just need this to calculate the Jacobean for us
# so we can manually encode it into the equations in ekf.cpp

from sympy import sin, cos, Matrix, Symbol, eye
from sympy.abc import phi, x, y, t, r, l
from sympy import pprint

# Constants
wheelbase_len = 0.84455 # Wheelbase length (meters)
wheel_rad = 0.127 # Wheel radius (meters)
#R = 6378137 # Earth radius (meters)

x_dot = Symbol("\dot{x}")
y_dot = Symbol("\dot{y}")
phi_dot = Symbol("\dot{phi}")

# State variables
state_vars = ["x","x_dot","y","y_dot","phi","phi_dot","l","r"] #l=v_l, r=v_r

# Differential drive approximation
# Velocity calculations
v_k = 0.5 * wheel_rad * (l + r)
x_dot_k = v_k * cos(phi)
y_dot_k = v_k * sin(phi)
phi_dot_k = (wheel_rad / wheelbase_len) * (l - r)

# Position calculations
x_k = x + x_dot * t
y_k = y + y_dot * t
phi_k = phi + phi_dot * t

# Setup vector of the functions
f = Matrix([
    x_k,
    x_dot_k,
    y_k,
    y_dot_k,
    phi_k,
    phi_dot_k,
    l,
    r
    ])

# Setup vector of the variables
X = Matrix([x,x_dot,y,y_dot,phi,phi_dot,l,r])

# IGVC Fk matrix
model = f
jac_model = f.jacobian(X)

# # Input dictionary setup
# input_dict = {self.state_vars[i]: last_xk[i] for i in range(0, len(self.state_vars))}
# input_dict['t'] = dt
# ctrl_dict = {self.ctrl_vars[i]: ctrl[i] for i in range(0, len(self.ctrl_vars))}

# F = self.jac_model.subs(input_dict).subs(ctrl_dict)
# new_state = self.model.subs(input_dict).subs(ctrl_dict)
# P = (F * P * F.T) + self.Q

# Print the Jacobean to the console
pprint(jac_model)


from flask import Blueprint, request, jsonify

from data import db_session
from data.posts import Post

blueprint = Blueprint('posts_api', __name__,
                      template_folder='templates')

# API для использования в других приложениях

# GET для получения списка постов
@blueprint.route('/api/posts')
def get_posts():
    session = db_session.create_session()
    posts = session.query(Post).all()

    return jsonify(
        {
            'posts':
                [item.to_dict(only=('id', 'title', 'content', 'user_id', 'is_private'))
                 for item in posts]
        }
    )

# GET для получения одного поста
@blueprint.route('/api/posts/<int:post_id>', methods=['GET'])
def get_one_post(post_id):
    session = db_session.create_session()
    post = session.query(Post).get(post_id)

    if not post:
        return jsonify({'error': 'Not found'})

    return jsonify(
        {
            'post': post.to_dict(only=('id', 'title', 'content', 'user_id', 'is_private'))
        }
    )

# POST для создания поста
@blueprint.route('/api/posts', methods=['POST'])
def create_post():
    session = db_session.create_session()

    if not request.json:
        return jsonify({'error': 'Empty request'})
    elif not all(key in request.json for key in
                 ["title", "text", "user_id", "is_found"]):
        return jsonify({'error': 'Bad request'})

    post = Post(
        title=request.json['title'],
        text=request.json['text'],
        user_id=int(request.json['user_id']),
        is_found=request.json['is_found']

    )

    session.add(post)
    session.commit()

    return jsonify({'success': 'OK'})

# DELETE для удаления поста
@blueprint.route('/api/posts/<int:post_id>', methods=['DELETE'])
def delete_post(post_id):
    session = db_session.create_session()
    post = session.query(Post).get(post_id)

    if not post:
        return jsonify({'error': 'Not found'})

    session.delete(post)
    session.commit()

    return jsonify({'success': 'OK'})

# PUT для редактирования поста
@blueprint.route('/api/posts/<int:post_id>', methods=['PUT'])
def edit_post(post_id):
    session = db_session.create_session()
    post = session.query(Post).get(post_id)

    if not post:
        return jsonify({'error': 'Not found'})

    if not request.json:
        return jsonify({'error': 'Empty request'})
    elif not all(key in request.json for key in
                 ["title", "text", "user_id", "is_found"]):
        return jsonify({'error': 'Bad request'})

    title = request.json['title']
    text = request.json['text']
    user_id = request.json['user_id']
    is_found = request.json['is_found']

    if title:
        post.title = title
    if text:
        post.text = text
    if user_id:
        post.user_id = int(user_id)
    if is_found:
        post.is_found = is_found

    session.commit()

    return jsonify({'success': 'OK'})


__author__ = "V.A. Sole - ESRF Data Analysis"
__contact__ = "sole@esrf.fr"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
from . import DataObject
from PyMca5.PyMcaIO import EdfFile
import types
import sys
import os
import numpy
import logging

SOURCE_TYPE = "EdfFile"

_logger = logging.getLogger(__name__)

class EdfFileDataSource(object):
    def __init__(self,nameInput, fastedf=False):
        if type(nameInput) == list:
            nameList = nameInput
        else:
            nameList = [nameInput]
        if sys.version < '3.0':
            stringTypes = [types.StringType, types.UnicodeType]
        else:
            stringTypes = [type("a"), type(eval('b"a"'))]
        for name in nameList:
            if type(name) not in stringTypes:
                raise TypeError("Constructor needs string as first argument")
        self.sourceName   = nameInput
        self.sourceType = SOURCE_TYPE
        self.__sourceNameList = nameList
        #this is to be added
        #self._fastedf = True
        self._fastedf = fastedf
        if fastedf:
            _logger.warning("fastedf is unsafe!")
        self.refresh()

    def refresh(self):
        self._sourceObjectList=[]
        for name in self.__sourceNameList:
            self._sourceObjectList.append(EdfFile.EdfFile(name, access='rb', fastedf=self._fastedf))
        self.__lastKeyInfo = {}

    def getSourceInfo(self):
        """
        Returns information about the EdfFile object created by
        SetSource, to give application possibility to know about
        it before loading.
        Returns a dictionary with the key "KeyList" (list of all available keys
        in this source). Each element in "KeyList" has the form 'n1.n2' where
        n1 is the source number and n2 image number in file starting at 1.
        """
        return self.__getSourceInfo()

    def __getSourceInfo(self):
        SourceInfo={}
        SourceInfo["SourceType"]=SOURCE_TYPE
        SourceInfo["KeyList"]=[]
        i = 0
        for sourceObject in self._sourceObjectList:
            i+=1
            nimages = sourceObject.GetNumImages()
            for n in range(nimages):
                SourceInfo["KeyList"].append("%d.%d" % (i,n+1))
        SourceInfo["Size"]=len(SourceInfo["KeyList"])
        return SourceInfo

    def getKeyInfo(self,key):
        if key in self.getSourceInfo()['KeyList']:
            return self.__getKeyInfo(key)
        else:
            #should we raise a KeyError?
            _logger.debug("Error key not in list ")
            return {}

    def __getKeyInfo(self,key):
        try:
            index,image = key.split(".")
            index = int(index)-1
            image = int(image)-1
        except Exception:
            #should we rise an error?
            _logger.debug("Error trying to interpret key = %s", key)
            return {}

        sourceObject = self._sourceObjectList[index]
        info= sourceObject.GetStaticHeader(image)
        info.update(sourceObject.GetHeader(image))
        info["SourceType"]  = SOURCE_TYPE
        #doubts about if refer to the list or to the individual file
        info["SourceName"]  = self.sourceName
        info["Key"]         = key
        #specific info of interest
        info['FileName'] = sourceObject.FileName
        info["rows"]     = info['Dim_2']
        info["cols"]     = info['Dim_1']
        info["type"]     = info['DataType']
        if 'MCA start ch' in info:
            info['Channel0'] = int(info['MCA start ch'])
        else:
            info['Channel0'] = 0

        if not ( 'McaCalib' in info):
            if ('MCA a' in info) and\
               ('MCA b' in info) and\
               ('MCA c' in info):
                info['McaCalib'] = [float(info['MCA a']),
                                    float(info['MCA b']),
                                    float(info['MCA c'])]
            else:
                info['McaCalib'] = [ 0.0, 1.0, 0.0]
        else:
            if type(info['McaCalib']) in [type(" ")]:
                info['McaCalib'] = info['McaCalib'].replace("[","")
                info['McaCalib'] = info['McaCalib'].replace("]","")
                cala, calb, calc = info['McaCalib'].split(",")
                info['McaCalib'] = [float(cala),
                                    float(calb),
                                    float(calc)]

        self.__lastKeyInfo[key] = os.path.getmtime(sourceObject.FileName)
        return info

    def getDataObject(self,key,selection=None):
        """
        selection: a dictionary with the keys pos and size: (x), (x,y) or (x,y,z)
                   tuples defining a roi
                   If not defined, takes full array
        """

        sourcekeys = self.getSourceInfo()['KeyList']
        #a key corresponds to an image
        key_split= key.split(".")
        image_key= key_split[0]+"."+key_split[1]
        if image_key not in sourcekeys:
            #if image_key == "0.0":
            #    #this is in fact a special selection: The SUM
            #    pass
            #else:
                raise KeyError("Key %s not in source keys" % image_key)
        #create data object
        data = DataObject.DataObject()
        data.info = self.__getKeyInfo(image_key)
        data.info ['selection'] = selection
        data.info['selectiontype'] = "2D"
        index = key_split[0]
        image = key_split[1]
        index = int(index)-1
        image = int(image)-1
        MCAIMP = 0
        if len(key_split) == 4:
            _logger.debug("mca like selection")
            #print data.info
            if 1:
                MCAIMP = 1
                if key_split[2].upper() == 'R':
                    pos  = (0, int(key_split[3]))
                    size = (int(data.info['Dim_1']), 1)
                elif key_split[2].upper() == 'C':
                    pos  = (int(key_split[3]), 0)
                    size = (1,int(data.info['Dim_2']))
                data.info['selectiontype'] = "1D"
            else:
                _logger.debug("mca like selection not yet implemented")
                pos = None
                size = None
                data.info['selectiontype'] = "1D"

        elif selection is None:
            pos = None
            size = None
        else:
            if "pos" in selection:
                data.info["pos"]=selection['pos']
            else:
                data.info['pos']=None
            if "size" in selection:
                data.info["size"]=selection['size']
            else:
                data.info['size']=None
            pos  = data.info['pos']
            size = data.info['size']
        sourceObject = self._sourceObjectList[index]
        data.data=sourceObject.GetData(image,Pos=pos,Size=size)
        data.info['rows'], data.info['cols'] = data.data.shape[0:2]
        if data.info['selectiontype'] == "1D":
            if MCAIMP:
                data.y = [numpy.ravel(data.data[:]).astype(numpy.float64)]
            else:
                if key_split[2].upper() == 'C':
                    data.y=[data.data[:,int(key_split[3])-1].astype(numpy.float64)]
                elif key_split[2].upper() == 'R':
                    data.y=[data.data[int(key_split[3])-1, :].astype(numpy.float64)]
                else:
                    raise ValueError("Unknown key %s" % key)
            ch0 = int(data.info['Channel0'])
            data.x = [ch0+numpy.arange(len(data.y[0])).astype(numpy.float64)]
            data.m = None
            data.data = None
            #print "data.x.shape ", data.x[0].shape
            #print "data.y.shape ", data.y[0].shape
        return data

    def isUpdated(self, sourceName, key):
        #sourceName is redundant?
        index,image = key.split(".")
        index = int(index)-1
        lastmodified = os.path.getmtime(self.__sourceNameList[index])
        if lastmodified != self.__lastKeyInfo[key]:
            self.__lastKeyInfo[key] = lastmodified
            return True
        else:
            return False

source_types = { SOURCE_TYPE: EdfFileDataSource}

def DataSource(name="", source_type=SOURCE_TYPE):
  try:
     sourceClass = source_types[source_type]
  except KeyError:
     #ERROR invalid source type
     raise TypeError("Invalid Source Type, source type should be one of %s" %\
                     source_types.keys())

  return sourceClass(name)

if __name__ == "__main__":
    import time
    try:
        sourcename=sys.argv[1]
        key       =sys.argv[2]
    except Exception:
        _logger.error("Usage: EdfFileDataSource <file> <key>")
        sys.exit()
    #one can use this:
    obj = EdfFileDataSource(sourcename)
    #or this:
    obj = DataSource(sourcename)
    #data = obj.getData(key,selection={'pos':(10,10),'size':(40,40)})
    #data = obj.getDataObject(key,selection={'pos':None,'size':None})
    t0 = time.time()
    data = obj.getDataObject(key,selection=None)
    print("elapsed = ",time.time() - t0)
    print("info = ",data.info)
    if data.data is not None:
        print("data shape = ",data.data.shape)
        print(numpy.ravel(data.data)[0:10])
    else:
        print(data.y[0].shape)
        print(numpy.ravel(data.y[0])[0:10])
    data = obj.getDataObject('1.1',selection=None)
    r = int(key.split('.')[-1])
    print(" data[%d,0:10] = " % (r-1),data.data[r-1   ,0:10])
    print(" data[0:10,%d] = " % (r-1),data.data[0:10, r-1])


# coding: utf-8
""" 限速器 """
import time
import redis
import logging
import functools
from .lua import get_throttle_script

logger = logging.getLogger('distlimiter')

class Throttler:
    """ 限速器 """

    def __init__(self, redis_url: str, key: str):
        self._redis_url = redis_url
        self._key = key
        self._client = redis.Redis.from_url(redis_url)
        self._delay = self.estimate_delay()

    def throttle(self, rate_per_second: float):
        raise RuntimeError('应该在子类中继承实现这个方法')

    def estimate_delay(self, num_rounds: int = 10):
        """ 估算redis和本机之间的网络延迟 """
        # 先建立连接
        self._client.ping()

        t1 = time.time()
        for _ in range(num_rounds):
            self._client.ping()
        t2 = time.time()
        return (t2 - t1) / num_rounds

class SmoothThrottler(Throttler):
    """ 平滑的限速器

    限速100/s == 每隔0.01秒做一个任务
    """

    def throttle(self, rate_per_second: float):
        script_info = get_throttle_script()

        # (原子化地)获取执行所需的等待时间
        try:
            us_to_sleep = self._client.evalsha(script_info['sha1'], 1,
                                               self._key, rate_per_second)
        except redis.exceptions.NoScriptError:
            sha1 = self._client.script_load(script_info['script'])
            us_to_sleep = self._client.evalsha(sha1, 1, self._key,
                                               rate_per_second)

        # 需要减掉和redis通信所花的网络延迟
        second_to_sleep = us_to_sleep / 1000000. - self._delay
        if second_to_sleep > 0:
            logger.debug('触发限速, 等待{:.6f}秒'.format(second_to_sleep))
            time.sleep(second_to_sleep)

def throttle(key: str,
             rate_per_second: float,
             method: str = 'smooth',
             redis_url: str = 'redis://localhost:6379/0'):
    """ 按key以rate_per_second的要求来平滑限速

    :param key: 限速的key
    :param rate_per_second: 每秒钟限速个数
    :param method: 限速方式, 目前可以用 'smooth'
    :param redis_url: redis://[:password@]host[:port][/database]
    """
    throttler_class = {'smooth': SmoothThrottler}.get(method)
    if not throttler_class:
        raise NotImplementedError('限速方式{}未定义'.format(method))
    throttler = throttler_class(redis_url=redis_url, key=key)

    def outer(func):
        @functools.wraps(func)
        def inner(*args, **kwargs):
            throttler.throttle(rate_per_second=rate_per_second)
            return func(*args, **kwargs)

        return inner

    return outer


from sklearn import datasets
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.cluster import KMeans

iris = datasets.load_iris()
X = iris.data[:, :2]
y = iris.target

plt.scatter(X[:,0], X[:,1],c=y)
plt.xlabel('Spea1 Length', fontsize=18)
plt.ylabel('Sepal Width', fontsize=18)

km = KMeans(n_clusters = 3)
km.fit(X)

centers = km.cluster_centers_
print(centers)

new_labels = km.labels_
print(new_labels)
print(y)

iris=datasets.load_iris()
x=iris.data[:,:2]
y=iris.target

fig, axes = plt.subplots(1,2 )
axes[0].scatter(X[:,0], X[:,1],c=y)
axes[1].scatter(X[:,0],x[:,1],c=y)
plt.scatter(x[:,0],x[:,1],c=new_labels)
# plt.scatter(X[:, 0], X[:, 1], c=y, cmap='prism',edgecolor='k', s=75)
# plt.scatter(X[:, 0], X[:, 1], c=new_labels, cmap='jet',edgecolor='k', s=75)
axes[0].set_xlabel('Sepal length', fontsize=12)
axes[0].set_ylabel('Sepal width', fontsize=12)
axes[1].set_xlabel('Sepal length', fontsize=12)
axes[1].set_ylabel('Sepal width', fontsize=12)
axes[0].tick_params(direction='in', length=10, width=5, colors='k', labelsize=15)
axes[1].tick_params(direction='in', length=10, width=5, colors='k', labelsize=15)
axes[0].set_title('Actual', fontsize=18)
axes[1].set_title('Predicted', fontsize=18)


#!/usr/bin/env python3

from sympy import solve
from symplyphysics import (units, convert_to, Quantity, prefixes)
from symplyphysics.laws.waves import frequency_shift_from_velocity as doppler_law

# This example show usefulness of Doppler law.
## Doppler effect is widely used in systems like radars. This effect is applicable not only to waves emitted by source, but also to waves reflected by it.
## So radar emits wave with known frequency and then receives reflected wave with frequency modified according to Doppler's effect.
## Then radar system can calculate velocity of object. Radars may use sound waves (ultrasonic), radio or infrared (electromagnetic).
## So for example ultrasonic radar emits 40 kHz wave. An object reflects this wave and at some moment radar receives this signal.
## We can calculate object's velocity.

sound_velocity = Quantity(340 * units.meter / units.second)
# Zero velocity we are going to use as a velocity of the radar related to air.
zero_velocity = Quantity(0)
emitter_frequency = Quantity(40 * prefixes.kilo * units.hertz)
# Choose any frequency and obtain the result
signal_frequency = Quantity(41.2 * prefixes.kilo * units.hertz)

solution = solve(doppler_law.law, doppler_law.source_velocity,
    dict=True)[0][doppler_law.source_velocity]
applied_solution = solution.subs({
    doppler_law.observed_frequency: signal_frequency,
    doppler_law.real_frequency: emitter_frequency,
    doppler_law.wave_velocity: sound_velocity,
    doppler_law.observer_velocity: zero_velocity
})

result_velocity = Quantity(applied_solution)
result = convert_to(result_velocity, units.kilometer / units.hour).evalf(3)

# Since object is not an emitter of signal, we cannot directly use Doppler law. Let's assume, we emit
# 40000 Hz at the approaching car. The car should observe this signal with 41200 Hz when moving at the
# resulting velocity. However, it should transmit the signal back to the radar. After applying the
# Doppler law, we should get twice the expected frequency shift - 42400 Hz.
# Therefore we should divide the resulting velocity by factor of two for the reflected signal.
result = result / 2

if result > 0:
    print(f"Object is moving away from radar with {result} km/h velocity")
elif result < 0:
    print(f"Object is moving towards radar with {-result} km/h velocity")
else:
    print("Object is not moving")


#!/usr/bin/python

import os
import sys
import argparse
import random
import numpy as np
import os.path as osp

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader

# custom functions defined by user
from model import DeepCNN
from datasets import EPIDataSet
from trainer import Trainer
from hyperopt import hp
from hyperopt.pyll.stochastic import sample

def randomparams():
    space = {
        'lr': hp.choice('lr', (0.001, 0.0001)),
        'beta1': hp.choice('beta1', (0.9, 0.9)),
        'beta2': hp.choice('beta2', (0.99, 0.999)),
        'weight': hp.choice('weight', (0, 0.0001))
    }
    params = sample(space)

    return params

def get_args():
    """Parse all the arguments.

        Returns:
          A list of parsed arguments.
    """
    parser = argparse.ArgumentParser(description="CNN for predicting CSSBS")

    parser.add_argument("-d", dest="data_dir", type=str, default=None,
                        help="A directory containing the training data.")
    parser.add_argument("-n", dest="name", type=str, default=None,
                        help="The name of a specified data.")

    parser.add_argument("-g", dest="gpu", type=str, default='1',
                        help="choose gpu device.")
    parser.add_argument("-s", dest="seed", type=int, default=5,
                        help="Random seed to have reproducible results.")

    parser.add_argument("-b", dest="batch_size", type=int, default=1,
                        help="Number of sequences sent to the network in one step.")
    parser.add_argument("-e", dest="max_epoch", type=int, default=30,
                        help="Number of training steps.")

    parser.add_argument("-c", dest="checkpoint", type=str, default='./models/',
                        help="Where to save snapshots of the model.")

    return parser.parse_args()

def main():
    """Create the model and start the training."""
    args = get_args()
    random.seed(args.seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed(args.seed)
        device = torch.device("cuda:" + args.gpu)
    else:
        device = torch.device("cpu")
        torch.manual_seed(args.seed)
    motifLen = 20
    Data = np.load(osp.join(args.data_dir, '%s_train.npz' % args.name))
    data_tr, label_tr = Data['data'], Data['label']
    Data = np.load(osp.join(args.data_dir, '%s_val.npz' % args.name))
    data_val, label_val = Data['data'], Data['label']
    dim = data_val.shape[1]
    f = open(osp.join(args.checkpoint, 'score.txt'), 'w')
    f.write('auc\tprauc\n')
    # build training data generator
    train_data = EPIDataSet(data_tr, label_tr)
    train_loader = DataLoader(train_data, batch_size=args.batch_size, shuffle=True, drop_last=True, num_workers=0)
    # build test data generator
    val_data = EPIDataSet(data_val, label_val)
    val_loader = DataLoader(val_data, batch_size=1, shuffle=False, num_workers=0)
    # implement
    auc_best = 0
    for _ in range(10):
        params = randomparams()
        model = DeepCNN(in_channels=dim, motiflen=motifLen)
        optimizer = optim.Adam(model.parameters(), lr=params['lr'],
                               betas=(params['beta1'], params['beta2']), weight_decay=params['weight'])
        criterion = nn.BCELoss()
        executor = Trainer(model=model,
                           optimizer=optimizer,
                           criterion=criterion,
                           device=device,
                           checkpoint=args.checkpoint,
                           start_epoch=0,
                           max_epoch=8,
                           train_loader=train_loader,
                           test_loader=val_loader,
                           lr_policy=None)

        auc, prauc, state_dict = executor.train()
        if auc_best < auc:
            print("Store the weights of the model in the current run.\n")
            auc_best = auc
            lr = params['lr']
            betas = (params['beta1'], params['beta2'])
            weight_decay = params['weight']
            checkpoint_file = osp.join(args.checkpoint, 'model_best.pth')
            torch.save({
                'model_state_dict': state_dict
            }, checkpoint_file)
    # loading the best model
    checkpoint_file = osp.join(args.checkpoint, 'model_best.pth')
    checkpoint = torch.load(checkpoint_file)
    state_dict = checkpoint["model_state_dict"]
    model.load_state_dict(state_dict, strict=True)
    optimizer = optim.Adam(model.parameters(), lr=lr, betas=betas, weight_decay=weight_decay)
    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.9)
    criterion = nn.BCELoss()
    executor = Trainer(model=model,
                       optimizer=optimizer,
                       criterion=criterion,
                       device=device,
                       checkpoint=args.checkpoint,
                       start_epoch=0,
                       max_epoch=args.max_epoch,
                       train_loader=train_loader,
                       test_loader=val_loader,
                       lr_policy=scheduler)
    auc, prauc, state_dict = executor.train()
    torch.save({'model_state_dict': state_dict}, checkpoint_file)
    f.write("{:.3f}\t{:.3f}\n".format(auc, prauc))
    f.close()

if __name__ == "__main__":
    main()


from typing import List
from bkgames.readers import TeamModel
from copy import deepcopy

class PastOnlyPlanner:
    """Finds teams to watch, based only on past games."""

    def get_teams_to_watch(self, teams_history: List[TeamModel]) -> List[TeamModel]:
        """Get teams to watch, based on historic results.
        Teams watched the longest time ago is the last in the returned collection.

        Args:
            teams_history (List[TeamModel]): List of teams with dates of games
                played by them.

        Returns:
            List[TeamModel]: List of teams ordered by:
                - the most played games
                - and most recently played game (in case of ties for the 1st condition)

        Example:
            first_team      6 games     23 days ago
            second_team     6 games     25 days ago
            third_team      4 games     30 days ago
        """

        # Games played by teams are read from a file in chronological order.
        # Oldest game is first, newest game is last.
        # To easier sort by the most recent games, it's convenient to sort those
        # games at first.

        if teams_history is None or len(teams_history) == 0:
            raise ValueError("Empty collection of games")

        # Copy in order not to change input collection.
        copied_teams = deepcopy(teams_history)

        for team in copied_teams:
            team.games.sort()

        return sorted(
            copied_teams,
            key=lambda team: (team.number_of_games_played, team.games[0]),
            reverse=True,
        )