#!./icao-venv/bin/python3

# needs:    opencv2-python
#           opencv2-contrib-python
#           dlib

import os
from pathlib import Path
import sys
import cv2
import dlib
import numpy as np
import json

show_rectangle = False

# set here, where to look for pretrained models:
datadir = Path("data")

# following files should be present in the datadir:
needed_files = {
    "shape_predictor_68_face_landmarks.dat":
        "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2"
}

def check_for_needed_files(datadir: Path):
    from urllib import request

    if not datadir.is_dir():
        datadir.mkdir()

    # Downloading needed Files
    for filename, url in needed_files.items():
        file = datadir.joinpath(filename)
        newfile = dlfile = file.parent.joinpath(Path(url).name)

        if not file.exists():
            print(
                "Downloading  " + url,
                file=sys.stderr
            )
            request.urlretrieve(
                url,
                filename=dlfile,
                reporthook=lambda blocks, blocksize, fullsize:
                    print(str((blocks * blocksize * 100) /
                              fullsize)[:5] + "%" + "\033[F", file=sys.stderr)
            )
            print("Done" + (" " * 12), file=sys.stderr)

            # decompress BZ2 files
            if dlfile.suffix == ".bz2":
                import bz2
                newfile = dlfile.parent.joinpath(dlfile.stem)
                if not newfile.exists():
                    print("Decompressing " + str(dlfile), file=sys.stderr)
                    newfile.write_bytes(bz2.decompress(dlfile.read_bytes()))
                    dlfile.unlink()

            # mv file to desired name
            if newfile.resolve() != file.resolve():
                newfile.rename(file)

class Face:
    pass

class image_analyzer:
    def __init__(self, image_path: str,
                 pretrained_predictor: str =
                 datadir.joinpath("shape_predictor_68_face_landmarks.dat"),
                 upsample_num_times: int = 1
                 ):
        self._path = Path(image_path)
        if self._path.exists():
            self._img = cv2.imread(str(self._path.resolve()))
            if(self._img is None):
                raise ValueError(
                    "OpenCV can't read Imagefile: " + str(self._path))
        else:
            raise FileNotFoundError("Imagefile not found: " + str(self._path))
        self._pretrained_predictor = Path(pretrained_predictor)
        self._upsample_num_times = upsample_num_times

    def img(self):
        return self._img

    def width(self):
        return len(self._img[0])

    def height(self):
        return len(self._img)

    def image_ratio(self):
        return len(self._img[0]) / len(self._img)

    def check_image_ratio(self, min: float=0.74, max: float=0.80):
        return (min <= self.image_ratio() <= max)

    def face_detector(self):
        # get static face detector
        if not hasattr(image_analyzer, '_face_detector') or image_analyzer._face_detector is None:
            image_analyzer._face_detector = dlib.get_frontal_face_detector()
        return image_analyzer._face_detector

    def shape_predictor(self):
        # load static shape predictor:
        if not hasattr(self, '_shape_predictor') or self._shape_predictor is None:
            self._shape_predictor = dlib.shape_predictor(
                str(self._pretrained_predictor)
            )
        return self._shape_predictor

    def face_rectangles(self):
        # check if image changed:
        if not hasattr(self, '_face_rectangles') or self._face_rectangles is None:
            self._face_rectangles = self.face_detector()(
                image=self._img,
                upsample_num_times=self._upsample_num_times
            )
        return self._face_rectangles

    def shape_of_face(self, face: dlib.full_object_detection):
        return self.shape_predictor()(self._img, face)

    def shapes(self):
        # generator for facial landmarks
        for face in self.face_rectangles():
            yield self.shape_of_face(face)

    def faces(self):
        for face in self.face_rectangles():
            yield Face(face, self)

class Face:
    def __init__(self,
                 rectangle: dlib.full_object_detection,
                 analyzer: image_analyzer):
        self._rect = rectangle
        self._img = analyzer.img()
        self._shape = [np.array([p.x, p.y])
                       for p in analyzer.shape_of_face(rectangle).parts()]

    def shape(self):
        return self._shape

    def rect(self):
        return self._rect

    def eye_left(self):
        return (self._shape[36] + self._shape[39]) / 2

    def eye_right(self):
        return (self._shape[42] + self._shape[45]) / 2

    def eye_center(self):
        return (self.eye_left() + self.eye_right()) / 2

    def mouth_center(self):
        return np.sum(self._shape[48:60], axis=0) / 12

def main(argv):
    if len(argv) < 2:
        print("missing image file", file=sys.stderr)
        print("run: $ python3 geometry_check.py /path/to/image/file", file=sys.stderr)
        return 1

    if not hasattr(cv2, 'face'):
        print("you need to install opencv2-contrib-python", file=sys.stderr)
        print("$ pip install opencv2-contrib-python", file=sys.stderr)
        return 1

    checks = dict({
        "ratio_correct": False,
        "face_detected": False,
        "single_face": False,
        "h_line_almost_horizontal": False,
        "h_line_rotation": None,
        "v_line_almost_vertical": False,
        "v_line_rotation": None,
        "midpoint_in_vertical_center": False,
        "midpoint_in_upper_half": False,
        "midpoint": None,
        "head_width_correct": False,
        "head_width_ratio": None
    })

    # CHECKS STARTING HERE:
    analyzer = image_analyzer(argv[1])
    checks["ratio_correct"] = bool(
        analyzer.check_image_ratio(min=0.74, max=0.80))
    faces = [f for f in analyzer.faces()]
    checks["face_detected"] = bool(len(faces) > 0)
    if len(faces) == 1:
        checks["single_face"] = True
        face = faces[0]
        #   I: H line almost horizontal:
        #      (f.eye_left()-f.eye_right())[1] rund 0
        #          abs( -||- / ana.height() ) < 0.01 # weniger als 1% rotation
        h_rotation = (face.eye_left() - face.eye_right()
                      )[1] / analyzer.height()
        checks["h_line_almost_horizontal"] = bool(abs(h_rotation) < 0.01)
        checks["h_line_rotation"] = float(h_rotation)

        #  II: V line almost vertical,
        #      H line is more important, because V and H don't need to be perpendicular
        #       (f.eye_center()-f.mouth_center())[0] rund 0
        #          abs( -||- / ana.width() ) < 0.05 # weniger als 5% rotation
        v_rotation = (face.eye_center() - face.mouth_center()
                      )[0] / analyzer.width()
        checks["v_line_almost_vertical"] = bool(abs(v_rotation) < 0.05)
        checks["v_line_rotation"] = float(v_rotation)

        # III: Midpoint M needs to be in horizontal center and vertically 30%-50% from top
        #      f.eye_center()/[ana.width(),ana.height()] = [ 0.45 <= x <= 0.55 , 0.30 <= y <= 0.50 ]
        m_rel = face.eye_center() / [analyzer.width(), analyzer.height()]
        checks["midpoint_in_vertical_center"] = bool(
            (m_rel[0] >= 0.45) and (m_rel[0] <= 0.55))
        checks["midpoint_in_upper_half"] = bool(
            (m_rel[1] >= 0.30) and (m_rel[1] <= 0.50))
        checks["midpoint"] = [float(d) for d in m_rel]

        #  IV: Headwith ratio
        head_width_ratio = face.rect().width() / analyzer.width()
        checks["head_width_correct"] = bool(
            (head_width_ratio >= 0.5) and (head_width_ratio <= 0.75))
        checks["head_width_ratio"] = float(head_width_ratio)

        if show_rectangle:
            img = analyzer.img()
            cv2.namedWindow("Bild", cv2.WND_PROP_FULLSCREEN)
            cv2.setWindowProperty("Bild", cv2.WND_PROP_FULLSCREEN,
                                  cv2.WINDOW_FULLSCREEN)
            rect = face.rect()
            cv2.rectangle(img, (rect.left(), rect.top()),
                          (rect.right(), rect.bottom()),  (0, 255, 255))
            cv2.imshow("Bild", img)
            while(1):
                k = cv2.waitKey(33)
                if k == 27 or k == ord('q'):    # Esc key or q to stop
                    cv2.destroyAllWindows()
                    break

    return checks

if __name__ == '__main__':
    check_for_needed_files(datadir)
    result = main(sys.argv)
    print(json.dumps(result))
    sys.exit(0)


from odoo import models, fields, api

class AccountInvoice(models.Model):
    _inherit = "account.invoice"

    channel_id = fields.Many2one(
        "purchase.order.channel", string="Channel", ondelete="restrict"
    )

    @api.onchange("origin")
    def onchange_origin(self):
        order = self.env["purchase.order"].search([("name","=",self.origin)])
        self.channel_id = order.channel_id


#!/usr/bin/env python
import unittest
import os
import shutil
import multiprocessing
import glob
from matador.scrapers.castep_scrapers import res2dict

ROOT_DIR = os.getcwd()
REAL_PATH = "/".join(os.path.realpath(__file__).split("/")[:-1]) + "/"
TEST_DIR = REAL_PATH + "/tmp_test"
NUM_CORES = multiprocessing.cpu_count()

if os.uname()[1] == "cluster2":
    NUM_CORES -= 2

def hull_test(self):
    """ Perform a test run of Ilustrado with dummy DFT,
    on all cores of current machine.
    """
    from matador.hull import QueryConvexHull
    from ilustrado.ilustrado import ArtificialSelector

    res_files = glob.glob(REAL_PATH + "/data/hull-KP-KSnP_pub/*.res")
    cursor = [res2dict(_file, db=True)[0] for _file in res_files]

    # prepare best structures from hull as gene pool
    hull = QueryConvexHull(
        cursor=cursor,
        elements=["K", "P"],
        subcmd="hull",
        no_plot=True,
        source=True,
        summary=True,
        hull_cutoff=0,
    )

    cursor = hull.hull_cursor[1:-1]

    print("Running on {} cores on {}.".format(NUM_CORES, os.uname()[1]))

    minsep_dict = {("K", "K"): 2.5}

    ArtificialSelector(
        gene_pool=cursor,
        hull=hull,
        debug=False,
        fitness_metric="hull",
        nprocs=NUM_CORES,
        check_dupes=0,
        check_dupes_hull=False,
        sandbagging=True,
        minsep_dict=minsep_dict,
        ncores=1,
        testing=True,
        mutations=["nudge_positions", "permute_atoms", "random_strain", "vacancy"],
        max_num_mutations=1,
        max_num_atoms=50,
        mutation_rate=0.5,
        crossover_rate=0.5,
        num_generations=3,
        population=15,
        num_survivors=10,
        elitism=0.5,
        loglevel="debug",
    )

    run_hash = glob.glob("*.json")[0].split("-")[0]

    new_life = ArtificialSelector(
        gene_pool=cursor,
        hull=hull,
        debug=False,
        fitness_metric="hull",
        recover_from=run_hash,
        load_only=True,
        check_dupes=0,
        check_dupes_hull=False,
        minsep_dict=minsep_dict,
        mutations=["nudge_positions", "permute_atoms", "random_strain", "vacancy"],
        sandbagging=True,
        nprocs=NUM_CORES,
        ncores=1,
        testing=True,
        max_num_mutations=1,
        max_num_atoms=50,
        mutation_rate=0.5,
        crossover_rate=0.5,
        num_generations=10,
        population=15,
        num_survivors=10,
        elitism=0.5,
        loglevel="debug",
    )
    self.assertTrue(len(new_life.generations[-1]) >= 15)
    self.assertTrue(len(new_life.generations[-1].bourgeoisie) >= 10)

    new_life.start()

    self.assertTrue(os.path.isdir(new_life.run_hash + "-results"))
    num_structures = len(glob.glob(new_life.run_hash + "-results/*.res"))
    self.assertTrue(num_structures > 5)

class MatadorHullUnitTest(unittest.TestCase):
    """ Tests matador hull init of ilustrado. """

    def setUp(self):
        if os.path.isdir(TEST_DIR):
            shutil.rmtree(TEST_DIR)
        os.makedirs(TEST_DIR, exist_ok=True)
        os.chdir(TEST_DIR)

    def tearDown(self):
        os.chdir(ROOT_DIR)
        shutil.rmtree(TEST_DIR)

    def testIlustradoFromHull(self):
        # if main throws an error, so will unit test
        print(os.getcwd())
        hull_test(self)

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


from rest_framework import serializers

from .models import RedditPost, RedditPostSnapshot

class RedditPostSerializer(serializers.HyperlinkedModelSerializer):
    # snapshots = serializers.HyperlinkedRelatedField(
    #     many=True,
    #     read_only=True,
    #     view_name='reddit_post_snapshots'
    # )
    class Meta:
        model = RedditPost
        fields = (
            'submission_id',
            'image_similarity',
            'image_url',
            'title',
            'subreddit',
            'subreddit_id',
            'permalink',
            'submission_created',
            'author',
            # 'snapshots',
        )

class RedditPostSnapshotSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = RedditPostSnapshot
        fields = '__all__'


# dash_callbacks.py

from dash.dependencies import Input, Output
from dashboard.dash_app import app
from dashboard.dash_aux import get_pending_html_table
from session.sc_helpers import QuitMode
from dashboard.sc_df_manager import DataframeManager
from binance import enums as k_binance
from datetime import datetime, timedelta
from basics.sc_perfect_trade import PerfectTradeStatus
from basics.sc_asset import Asset

print('dash_callbacks.py')

dfm = DataframeManager()

# first line data
@app.callback(Output('current-time', 'children'),
              Output('neb', 'children'),  # perfect trade net profit
              Output('qty', 'children'),  # orders quantity
              Output('target', 'children'),  # session target net profit
              Output('max-negative-profit-allowed', 'children'),  # if reached, session ended at price
              Input('update', 'n_intervals'))
def display_value(value):
    symbol_name = dfm.dashboard_active_symbol.name
    quote_name = dfm.dashboard_active_symbol.quote_asset().name()
    base_name = dfm.dashboard_active_symbol.base_asset().name()

    return \
        f'{datetime.now().strftime("%H:%M:%S")}',\
        f'n: {dfm.sm.active_sessions[symbol_name].P_NET_QUOTE_BALANCE:,.2f} {quote_name}',\
        f'q: {dfm.sm.active_sessions[symbol_name].P_QUANTITY:,.4f} {base_name}',\
        f't: {dfm.sm.active_sessions[symbol_name].checks_manager.P_TARGET_TOTAL_NET_PROFIT:,.2f} {quote_name}',\
        f'({dfm.sm.active_sessions[symbol_name].checks_manager.P_MAX_NEGATIVE_PROFIT_ALLOWED:,.2f})'

# **********************************
# ********** Session data **********
# **********************************

# elapsed time
@app.callback(Output('session-count', 'children'),
              Output('session-cycle-count', 'children'),
              Input('update', 'n_intervals'))
def display_value(value):
    symbol_name = dfm.dashboard_active_symbol.name
    return f'#{dfm.sm.session_count[symbol_name]:03d}', \
           f'{timedelta(seconds=dfm.sm.active_sessions[symbol_name].cmp_count)}'

# perfect trade status info
@app.callback(Output('pt-new', 'children'),
              Output('pt-buy', 'children'),
              Output('pt-sell', 'children'),
              Output('pt-end', 'children'),
              Input('update', 'n_intervals'))
def display_value(value):
    symbol_name = dfm.dashboard_active_symbol.name
    ptm = dfm.sm.active_sessions[symbol_name].ptm
    return \
        len(ptm.get_pt_by_request(pt_status=[PerfectTradeStatus.NEW])),\
        len(ptm.get_pt_by_request(pt_status=[PerfectTradeStatus.BUY_TRADED])),\
        len(ptm.get_pt_by_request(pt_status=[PerfectTradeStatus.SELL_TRADED])),\
        len(ptm.get_pt_by_request(pt_status=[PerfectTradeStatus.COMPLETED]))

# span, depth, momentum & TBD data
@app.callback(Output('pt-span', 'children'),
              Output('pt-span-buy', 'children'),
              Output('pt-span-sell', 'children'),
              Output('pt-depth', 'children'),
              Output('pt-depth-buy', 'children'),
              Output('pt-depth-sell', 'children'),
              Output('pt-mtm', 'children'),
              Output('pt-mtm-buy', 'children'),
              Output('pt-mtm-sell', 'children'),
              Input('update', 'n_intervals'))
def display_value(value):
    session_orders = dfm.get_session_orders()

    symbol_name = dfm.dashboard_active_symbol.name
    active_session = dfm.sm.active_sessions[symbol_name]
    cmp = active_session.cmp
    gap = active_session.gap

    buy_gap_span, sell_gap_span = active_session.helpers.get_gap_span_from_list(orders=session_orders, cmp=cmp, gap=gap)
    total_gap_span = buy_gap_span + sell_gap_span
    buy_gap_depth, sell_gap_depth = active_session.helpers.get_gap_depth_from_list(orders=session_orders, cmp=cmp, gap=gap)
    total_gap_depth = buy_gap_depth + sell_gap_depth
    buy_gap_momentum, sell_gap_momentum = active_session.helpers.get_gap_momentum_from_list(orders=session_orders, cmp=cmp, gap=gap)
    total_gap_momentum = buy_gap_momentum + sell_gap_momentum
    return \
        f'{total_gap_span:.2f}', \
        f'{buy_gap_span:.2f}', \
        f'{sell_gap_span:.2f}', \
        f'{total_gap_depth:.2f}', \
        f'{buy_gap_depth:.2f}', \
        f'{sell_gap_depth:.2f}', \
        f'{total_gap_momentum:.2f}', \
        f'{buy_gap_momentum:.2f}', \
        f'{sell_gap_momentum:.2f}'

# ********** Session STOP profits **********
@app.callback(Output('actual-profit', 'children'),
              Output('stop-price-profit', 'children'),
              Output('ntc', 'children'),
              Output('time-to-next-try', 'children'),
              Output('is-active', 'children'),
              Input('update', 'n_intervals'))
def display_value(value):
    symbol = dfm.dashboard_active_symbol
    symbol_name = symbol.name
    cmp = dfm.sm.active_sessions[symbol_name].cmp
    qp = symbol.quote_asset().pv()
    base_ntc = dfm.sm.active_sessions[symbol_name].checks_manager.base_negative_try_count
    quote_ntc = dfm.sm.active_sessions[symbol_name].checks_manager.quote_negative_try_count

    cycles_count_for_inactivity = dfm.sm.active_sessions[symbol_name].cycles_count_for_inactivity
    cycles_to_new_pt = cycles_count_for_inactivity - dfm.sm.active_sessions[symbol_name].cycles_from_last_trade
    cycles_to_new_pt = 0.0 if cycles_to_new_pt < 0 else cycles_to_new_pt
    time_to_next_try = timedelta(seconds=cycles_to_new_pt)

    is_active = 'ON' if dfm.sm.active_sessions[symbol_name].is_active else 'OFF'

    return f'{dfm.sm.active_sessions[symbol_name].ptm.get_total_actual_profit_at_cmp(cmp=cmp):,.{qp}f}',\
           f'{dfm.sm.active_sessions[symbol_name].ptm.get_stop_price_profit(cmp=cmp):,.{qp}f}', \
           f'{base_ntc} - {quote_ntc}', \
           f'{time_to_next_try}', \
           f'{is_active}'

# @app.callback(Output('cycles-to-new-pt', 'children'), Input('update', 'n_intervals'))
# def display_value(value):
#     symbol_name = dfm.dashboard_active_symbol.name
#     cycles_count_for_inactivity = dfm.sm.active_sessions[symbol_name].cycles_count_for_inactivity
#     cycles_to_new_pt = cycles_count_for_inactivity - dfm.sm.active_sessions[symbol_name].cycles_from_last_trade
#     time_to_new_pt = timedelta(seconds=cycles_to_new_pt)
#     return f'({cycles_count_for_inactivity})  {time_to_new_pt}'

# **********************************
# ********** Global data **********
# **********************************

# ********** Global elapsed time **********
@app.callback(Output('global-cycle-count', 'children'), Input('update', 'n_intervals'))
def display_value(value):
    symbol_name = dfm.dashboard_active_symbol.name
    global_cmp = dfm.sm.terminated_sessions[symbol_name]["global_cmp_count"]
    session_cmp = dfm.sm.active_sessions[symbol_name].cmp_count
    return f'{timedelta(seconds=global_cmp + session_cmp)}'

# isolated orders info
@app.callback(
    Output('isol-orders-placed', 'children'),
    Output('isol-orders-pending', 'children'),
    Output('isol-orders-pending-buy', 'children'),
    Output('isol-orders-pending-sell', 'children'),
    Input('update', 'n_intervals'))
def display_value(value):
    symbol_name = dfm.dashboard_active_symbol.name

    placed = dfm.sm.terminated_sessions[symbol_name]['global_placed_orders_count_at_price']
    sell = len(
        [order for order in dfm.sm.iom.isolated_orders
         if order.k_side == k_binance.SIDE_SELL and order.symbol.name == symbol_name])
    buy = len(
        [order for order in dfm.sm.iom.isolated_orders
         if order.k_side == k_binance.SIDE_BUY and order.symbol.name == symbol_name])
    pending = buy + sell
    return placed, pending, buy, sell

# Global span, depth, momentum & TBD data
@app.callback(Output('is-span', 'children'),
              Output('is-span-buy', 'children'),
              Output('is-span-sell', 'children'),
              Output('is-depth', 'children'),
              Output('is-depth-buy', 'children'),
              Output('is-depth-sell', 'children'),
              Output('is-mtm', 'children'),
              Output('is-mtm-buy', 'children'),
              Output('is-mtm-sell', 'children'),
              Input('update', 'n_intervals'))
def display_value(value):
    all_orders = dfm.get_all_orders()

    symbol_name = dfm.dashboard_active_symbol.name
    active_session = dfm.sm.active_sessions[symbol_name]
    cmp = active_session.cmp
    gap = active_session.gap

    buy_gap_span, sell_gap_span = active_session.helpers.get_gap_span_from_list(orders=all_orders, cmp=cmp, gap=gap)
    total_gap_span = buy_gap_span + sell_gap_span
    buy_gap_depth, sell_gap_depth = active_session.helpers.get_gap_depth_from_list(orders=all_orders, cmp=cmp, gap=gap)
    total_gap_depth = buy_gap_depth + sell_gap_depth
    buy_gap_momentum, sell_gap_momentum = \
        active_session.helpers.get_gap_momentum_from_list(orders=all_orders, cmp=cmp, gap=gap)
    total_gap_momentum = buy_gap_momentum + sell_gap_momentum
    return \
        f'{total_gap_span:.2f}',\
        f'{buy_gap_span:.2f}',\
        f'{sell_gap_span:.2f}', \
        f'{total_gap_depth:.2f}', \
        f'{buy_gap_depth:.2f}', \
        f'{sell_gap_depth:.2f}', \
        f'{total_gap_momentum:.2f}', \
        f'{buy_gap_momentum:.2f}', \
        f'{sell_gap_momentum:.2f}'

# ********** Global STOP profits **********
@app.callback(Output('consolidated-profit', 'children'),
              Output('expected-profit-at-cmp', 'children'),
              Output('expected-profit', 'children'),
              Output('actions-info', 'children'),
              Output('actions-rate', 'children'),
              Output('canceled-count', 'children'),
              Input('update', 'n_intervals'))
def display_value(value):
    symbol = dfm.dashboard_active_symbol
    symbol_name = symbol.name
    cmp = dfm.sm.active_sessions[symbol_name].cmp  # s[-1]
    qp = symbol.quote_asset().pv()
    # coin_symbol = symbol.quote_asset().name()
    # called the method in session to check buy_count == sell_count
    consolidated = dfm.sm.terminated_sessions[symbol_name]['global_consolidated_profit']
    expected = dfm.sm.terminated_sessions[symbol_name]['global_expected_profit']
    expected_at_cmp = dfm.sm.iom.get_expected_profit_at_cmp(cmp=cmp, symbol_name=symbol_name)
    buy_actions_count, sell_actions_count, actions_balance = \
        dfm.sm.active_sessions[symbol_name].checks_manager.get_actions_balance()
    buy_actions_rate = consolidated / (buy_actions_count +1)
    sell_actions_rate = consolidated / (sell_actions_count +1)
    canceled_buy_orders = [order for order in dfm.sm.iom.canceled_orders if order.k_side == k_binance.SIDE_BUY]
    canceled_sell_orders = [order for order in dfm.sm.iom.canceled_orders if order.k_side == k_binance.SIDE_SELL]

    if consolidated + expected_at_cmp > 100.0:
        global_cmp = dfm.sm.terminated_sessions[symbol_name]["global_cmp_count"]
        session_cmp = dfm.sm.active_sessions[symbol_name].cmp_count
        raise Exception(f'TARGET ACHIEVED!!! in {timedelta(seconds=global_cmp + session_cmp)}'
                        f' DONE: {consolidated} ACTUAL AL CMP: {expected_at_cmp}')

    return f'{consolidated:,.{qp}f}',\
           f'{expected_at_cmp:,.{qp}f}',\
           f'{expected:,.{qp}f}', \
           f'{buy_actions_count}/{sell_actions_count} {actions_balance:,.2f}', \
           f'{buy_actions_rate:,.0f} / {sell_actions_rate:,.0f}', \
           f'{len(canceled_buy_orders)} / {len(canceled_sell_orders)}'

# ********** PT count / traded orders count **********
# @app.callback(Output('trade-info', 'children'), Input('update', 'n_intervals'))
# def display_value(value):
#     symbol_name = dfm.dashboard_active_symbol.name
#     pt_count = len(dfm.sm.active_sessions[symbol_name].ptm.perfect_trades)
#     buy_count = dfm.sm.active_sessions[symbol_name].buy_count
#     sell_count = dfm.sm.active_sessions[symbol_name].sell_count
#     return f'pt: {pt_count}   b: {buy_count}   s: {sell_count}'

# @app.callback(Output('short-prediction', 'children'),
#               Output('long-prediction', 'children'),
#               Input('update', 'n_intervals'))
# def display_value(value):
#     symbol_name = dfm.dashboard_active_symbol.name
#     session = dfm.sm.active_sessions[symbol_name]
#     short_prediction = session.strategy_manager.get_tendency(session.cmp_pattern_short) - session.cmp
#     long_prediction = session.strategy_manager.get_tendency(session.cmp_pattern_long) - session.cmp
#     return f'short: {short_prediction:,.0f}', f'long: {long_prediction:,.0f}'

# @app.callback(Output('accounts-info', 'children'), Input('update', 'n_intervals'))
# def display_value(value):
#     accounts_info = [f'{account.name}: {account.free:,.2f} ' for account in dfm.sm.am.accounts.values()]
#     accounts_info_s = ' '.join(map(str, accounts_info))
#     return accounts_info_s

# ********** symbol & accounts data **********
@app.callback(
    Output('symbol', 'children'),
    Output('cmp-max', 'children'),
    Output('cmp', 'children'),
    Output('cmp-min', 'children'),
    Output('base-asset', 'children'),
    Output('base-asset-locked', 'children'),
    Output('base-asset-alive', 'children'),
    Output('base-asset-free', 'children'),
    Output('base-asset-total', 'children'),
    Output('quote-asset', 'children'),
    Output('quote-asset-locked', 'children'),
    Output('quote-asset-alive', 'children'),
    Output('quote-asset-free', 'children'),
    Output('quote-asset-total', 'children'),
    Output('bnb-locked', 'children'),
    Output('bnb-alive', 'children'),
    Output('bnb-free', 'children'),
    Output('bnb-total', 'children'),
    Input('update', 'n_intervals')
)
def display_value(value):
    symbol = dfm.dashboard_active_symbol
    symbol_name = symbol.name
    # am = dfm.sm.active_sessions[symbol_name].am
    am = dfm.sm.am
    base_account = am.get_account(symbol.base_asset().name())
    quote_account = am.get_account(symbol.quote_asset().name())
    bnb_account = am.get_account('BNB')
    quote_pv = symbol.quote_asset().pv()

    base_alive = dfm.sm.get_liquidity_for_alive_orders(asset=symbol.base_asset())
    quote_alive = dfm.sm.get_liquidity_for_alive_orders(asset=symbol.quote_asset())
    bnb_alive = dfm.sm.get_liquidity_for_alive_orders(asset=Asset(name='BNB', pv=6))
    # bnb_alive = 1.0

    return \
        symbol_name, \
        f'{dfm.sm.active_sessions[symbol_name].max_cmp:,.{quote_pv}f}', \
        f'{dfm.sm.active_sessions[symbol_name].cmp:,.{quote_pv}f}', \
        f'{dfm.sm.active_sessions[symbol_name].min_cmp:,.{quote_pv}f}', \
        symbol.base_asset().name(),\
        f'{base_account.locked:,.{symbol.base_asset().pv()}f}', \
        f'{base_alive:,.{symbol.base_asset().pv()}f}', \
        f'{base_account.free - base_alive:,.{symbol.base_asset().pv()}f}', \
        f'{base_account.get_total():,.{symbol.base_asset().pv()}f}', \
        symbol.quote_asset().name(), \
        f'{quote_account.locked:,.{symbol.quote_asset().pv()}f}', \
        f'{quote_alive:,.{symbol.quote_asset().pv()}f}', \
        f'{quote_account.free - quote_alive:,.{symbol.quote_asset().pv()}f}',\
        f'{quote_account.get_total():,.{symbol.quote_asset().pv()}f}',\
        f'{bnb_account.locked:,.6f}',\
        f'{bnb_alive:,.6f}', \
        f'{bnb_account.free - bnb_alive:,.6f}', \
        f'{bnb_account.get_total():,.6f}'

# ********** alert message **********
@app.callback(Output('alert-msg', 'children'),
              Input('update', 'n_intervals'))
def display_value(value):
    symbol = dfm.dashboard_active_symbol

    # check bnb liquidity and raise ALERT
    bnb_liquidity = dfm.sm.active_sessions[symbol.name].am.get_account('BNB').free
    if bnb_liquidity < 1.0:
        return f'BNB LIQUIDITY ALERT {bnb_liquidity:,.6f}'
    else:
        return ''

# ********** symbol selection buttons *********
@app.callback(Output('button-symbols', 'children'),
              Input('button-symbols', 'n_clicks'),
              )
def on_button_click(n):
    # set BTCEUR as active symbol if button pressed
    current_symbol_name = dfm.dashboard_active_symbol.name
    if n is not None:
        next_symbol = dfm.get_next_symbol(symbol_name=current_symbol_name)
        dfm.set_dashboard_active_symbol(symbol_name=next_symbol)
        return next_symbol
    else:
        return current_symbol_name

# Stop buttons
@app.callback(Output('button-stop-cmp', 'children'), Input('button-stop-cmp', 'n_clicks'))
def on_button_click(n):
    if n is not None:
        symbol = dfm.dashboard_active_symbol
        symbol_name = symbol.name
        session = dfm.sm.active_sessions[symbol_name]
        session.helpers.quit_particular_session(
            quit_mode=QuitMode.TRADE_ALL_PENDING,
            session_id=session.session_id,
            symbol=session.symbol,
            cmp=session.cmp,
            iom=session.iom,
            cmp_count=session.cmp_count)

    return 'STOP-CMP'

@app.callback(Output('button-stop-price', 'children'), Input('button-stop-price', 'n_clicks'))
def on_button_click(n):
    if n is not None:
        symbol = dfm.dashboard_active_symbol
        symbol_name = symbol.name
        session = dfm.sm.active_sessions[symbol_name]
        session.helpers.quit_particular_session(
            quit_mode=QuitMode.PLACE_ALL_PENDING,
            session_id=session.session_id,
            symbol=session.symbol,
            cmp=session.cmp,
            iom=session.iom,
            cmp_count=session.cmp_count)

    return 'STOP-PRICE'

@app.callback(Output('button-stop-cancel', 'children'), Input('button-stop-cancel', 'n_clicks'))
def on_button_click(n):
    if n is not None:
        symbol = dfm.dashboard_active_symbol
        symbol_name = symbol.name
        session = dfm.sm.active_sessions[symbol_name]
        session.helpers.quit_particular_session(
            quit_mode=QuitMode.CANCEL_ALL,
            session_id=session.session_id,
            symbol=session.symbol,
            cmp=session.cmp,
            iom=session.iom,
            cmp_count=session.cmp_count)
    return 'STOP-CANCEL'

@app.callback(Output('button-reboot-global-session', 'children'), Input('button-reboot-global-session', 'n_clicks'))
def on_button_click(n):
    if n is not None:
        # as first step, perform STOP-PRICE actions
        symbol = dfm.dashboard_active_symbol
        symbol_name = symbol.name
        for session in dfm.sm.active_sessions.values():
            # session = dfm.sm.active_sessions[symbol_name]
            session.helpers.quit_particular_session(
                quit_mode=QuitMode.PLACE_ALL_PENDING,
                session_id=session.session_id,
                symbol=session.symbol,
                cmp=session.cmp,
                iom=session.iom,
                cmp_count=session.cmp_count)

        # finish app ans it will cause a gunicorn workers reboot (reset-like app)
        dfm.sm.reboot_global_session()
    return 'REBOOT-SESSION'

@app.callback(Output('button-new-pt', 'children'), Input('button-new-pt', 'n_clicks'))
def on_button_click(n):
    if n:
        symbol = dfm.dashboard_active_symbol
        symbol_name = symbol.name
        dfm.sm.active_sessions[symbol_name].manually_create_new_pt()
    return 'NEW-PT'

@app.callback(Output('button-increase-cmp', 'children'), Input('button-increase-cmp', 'n_clicks'))
def on_button_click(n):
    if n:
        symbol_name = dfm.dashboard_active_symbol.name
        dfm.sm.client_manager.on_button_step(symbol_name=symbol_name, step=10.0)
    return '+ 10.0 €'

@app.callback(Output('button-decrease-cmp', 'children'), Input('button-decrease-cmp', 'n_clicks'))
def on_button_click(n):
    if n:
        symbol_name = dfm.dashboard_active_symbol.name
        dfm.sm.client_manager.on_button_step(symbol_name=symbol_name, step=-10.0)
    return '- 10.0 €'

# ********** others **********
@app.callback(
    Output('new-table', 'children'),
    Input('update', 'n_intervals')
)
def update_table(timer):
    df = dfm.get_all_orders_df_with_cmp()
    # sort by price
    df1 = df.sort_values(by=['price'], ascending=False)
    # filter by status for each table (monitor-placed & traded)
    df_pending = df1[df1.status.isin(['monitor', 'active', 'cmp', 'to_be_traded', 'canceled'])]
    qp = dfm.dashboard_active_symbol.quote_asset().pv()
    df_pending['price'] = df_pending['price'].map(f'{{:,.{qp}f}}'.format)  # two {{ }} to escape { in f-string
    df_pending['total'] = df_pending['total'].map(f'{{:,.{qp}f}}'.format)

    return get_pending_html_table(df=df_pending[['pt_id', 'name', 'price', 'amount', 'total', 'status']])


k, n = map(int, input().split())
arr = []

# cnt번째 자리 숫자 결정
def select(a):
    # 종료조건
    # 횟수n 넘으면
    if a == n+1:
        print(*arr)
        return
    # 재귀호출
    # 1부터 시작해서 사전순으로 정렬됨
    for i in range(1, k+1):
        # i를 위한 연산
        arr.append(i)
        select(a+1)
        # i에 대한 역 연산
        arr.pop()

select(1)

# 내풀이
k, n = map(int, input().split())
answer = []

def choose(cnt):
    if cnt == n:
        print(*answer)
        return

    for i in range(1, k+1):
        answer.append(i)
        choose(cnt+1)
        answer.pop()

choose(0)

코드트리/IL/백트래킹/k개중에1개를n번뽑기.py

k개중에1개를n번뽑기.py


#!/usr/bin/python
# -*- coding:utf-8
__author__ = 'lvfei'

# id int(8) not null primary key auto_increment,
# rec_algorithm_test_id int(8) not null,
#    package_name char(100) not null,
#    package_title char(255) not null,
#    package_icon Text not null,
#    package_info Text not null,
#    partin_num TINYINT not null,
#    score float not null,
#    create_by char(50) not null,
#    create_datetime datetime not null,
#    update_datetime datetime not null

def get_lines(filename):
    with open(filename) as f:
        return f.readlines()

def parse_feild(lines):
    class Item:
        pass

    feilds = []
    for line in lines:
        raw = line.strip()
        keys = raw.split(" ")
        item = Item()
        item.sql_key = keys[0]
        item.sql_value = keys[1]
        item.key = get_feild_java_name(keys[0])
        item.value = get_java_type_from_sql(keys[1])
        feilds.append(item)

    return feilds

def get_java_type_from_sql(type):
    value = type.lower()
    if value == "float":
        return "Float"

    if value.startswith("char"):
        return "String"

    if value == "datetime":
        return "Timestamp"

    if value == "tinyint":
        return "Short"

    if value == "text":
        return "String"

    if value.startswith("int"):
        return "Long"

def get_feild_java_name(feild):
    words = feild.split("_")
    if len(words) == 1:
        return feild

    feild = words[0]
    for word in words[1:]:
        begin = word[0].upper()
        word = begin + word[1:]
        feild += word

    return feild

def generate_felid_java_code(name, type):
    java_type = type
    java_name = name
    declare = "private " + java_type + " " + java_name + ";"

    method_name = java_name[0].upper() + java_name[1:]
    getter = "public " + java_type + " get" + method_name + "(){\n"
    getter += "\t return this." + java_name + ";\n"
    getter += "}\n"

    setter = "public void set" + method_name + "(" + java_type + " " + java_name + ") {\n"
    setter += "\tthis." + java_name + " = " + java_name + ";\n"
    setter += "}\n"

    return declare + "\n" + getter + "\n" + setter + "\n"

def generate_entify(name, feilds):
    code = ""
    code += "import java.sql.Timestamp;\n"
    code += "\n"

    code += "public class " + name + "{\n"

    for item in feilds:
        tmp = generate_felid_java_code(item.key, item.value)
        code += tmp
    code += "}\n"

    return code

def generate_mapper(name, feilds):
    code = ""
    code += "import org.springframework.jdbc.core.RowMapper;\n"
    code += "import java.sql.ResultSet;\n"
    code += "import java.sql.SQLException;\n"

    code += "\n"
    code += "public class " + name + "Mapper implements RowMapper {\n"

    code += "\t@Override\n"
    code += "\tpublic Object mapRow(ResultSet rs, int rowNum) throws SQLException {\n"
    code += "\t\t" + name + " entity = new " + name + "();\n"
    for feild in feilds:
        code += "\t\tentity.set" + feild.key[0].upper() + feild.key[
                                                          1:] + "(rs.get" + feild.value + "(\"" + feild.sql_key + "\"));\n"

    code += "\t\treturn entity;\n"
    code += "\t}\n"
    code += "}\n"
    return code

def generate_get(name):
    code = "public " + name + " getById(long id) { \n"
    code += "\tString sql = \"select * from \"+ TABLE_NAME + \" where id = ?\";\n"
    code += "\ttry{\n"
    code += "\t\treturn (" + name + ") jdbcTemplate.queryForObject(sql, new Object[]{id}, new " + name + "Mapper());\n"
    code += "\t} catch (EmptyResultDataAccessException e) {\n"
    code += "\t\treturn null;\n"
    code += "\t}"
    code += "}\n"
    return code

def generate_add(name, feilds):
    code = "public long add(" + name + " entity) {\n"
    code += "String sql = \"insert into \"+ TABLE_NAME + \" (\" +\n"

    keys = ""
    placeholders = ""
    values = ""
    for item in feilds[:len(feilds) - 1]:
        if item.sql_key != "id":
            keys += "\t\"" + item.sql_key + ", \" +\n"
            placeholders += "?, "
            getter = "get" + item.key[0].upper() + item.key[1:] + "()"
            values += "\tentity." + getter + ",\n"

    item = feilds[len(feilds) - 1]
    keys += "\t\"" + item.sql_key
    placeholders += "?"
    getter = "get" + item.key[0].upper() + item.key[1:] + "()"
    values += "\tentity." + getter

    code += keys + ")\" +\n"
    code += "\t\" values\"+\n"
    code += "\t\"(" + placeholders + ")\";\n"

    code += "\n"
    code += "jdbcTemplate.update(sql, new Object[]{\n"
    code += values
    code += "});\n"
    code += "\n"

    code += "String queryId = \"select max(id) from \"+TABLE_NAME+\" where create_by =?\";\n"

    code += "long id = jdbcTemplate.queryForObject(\n"
    code += "\t\tqueryId, new Object[]{entity.getCreateBy()}, Long.class);\n"
    code += "return id;\n"

    code += "}\n"
    return code

def generate_update(name, feilds):
    code = "public boolean update(" + name + " entity) {\n"
    sql = "\t String sql = \"update \"+ TABLE_NAME + \" set \" + \n"
    java = "\tint rows = jdbcTemplate.update(sql, new Object[]{\n"
    for item in feilds[:len(feilds) - 2]:
        if item.sql_key != "id":
            sql += "\t\t\"" + item.sql_key + "=?, \"+\n"
            java += "\t\tentity.get" + item.key[0].upper() + item.key[1:] + "(),\n"

    item = feilds[len(feilds) - 1]
    sql += "\t\t\"" + item.sql_key + "=? \"+\n"
    java += "\t\tentity.get" + item.key[0].upper() + item.key[1:] + "(),\n"

    sql += "\t\t\"where id = ?\";"
    java += "\t\tentity.getId()});\n"
    code += sql
    code += "\n"
    code += "\n"
    code += java
    code += "\n"
    code += "\treturn rows >= 1;\n"

    code += "}\n"
    return code

def generate_remove():
    code = "public boolean remove(long id) {\n"
    code += "\tString sql = \"delete from \"+ TABLE_NAME +\" where id = ?\";\n"
    code += "\tint rows = jdbcTemplate.update(sql, new Object[]{id});\n"
    code += "\treturn rows >= 1;\n"
    code += "}\n"
    return code

def generate_list(name):
    code = ""
    code += "public QueryResult<" + name + "> getList(int page, int rowNum, String sortBy, String order) {\n"
    code += "\tint pos = Math.max((page - 1) * rowNum, 0);\n"
    code += "\tString sql = \"select * from \"+ TABLE_NAME +\" order by \" + sortBy + \" \" + order +" \
            " \" limit \" + Integer.toString(pos) + \",\" + Integer.toString(rowNum);\n"

    code += "\tList<" + name + "> list = (List<" + name + ">) jdbcTemplate.query(sql, new " + name + "Mapper());"

    code += "\tString countSql = \"select count(*) from \"+ TABLE_NAME " + ";"
    code += "\tint count = jdbcTemplate.queryForObject(countSql, Integer.class);\n"
    code += "\n"

    code += "\tQueryResult<" + name + "> result = new QueryResult<" + name + ">(rowNum);\n"
    code += "\tint totalPage = (count + rowNum -1) / rowNum;\n"
    code += "\tresult.setTotal(totalPage);\n"
    code += "\tresult.setPage(page);\n"
    code += "\tresult.setRecords(count);\n"
    code += "\tresult.setRows(list);\n"
    code += "\n"
    code += "\treturn result;\n"
    code += "}\n"
    return code

def generate_dao(name, table, feilds):
    code = ""
    code += "import org.springframework.jdbc.core.JdbcTemplate;\n"
    code += "import com.xunlei.recommend.web.console.common.QueryResult;\n"
    code += "import java.util.List;\n"
    code += "\n"
    code += "public class " + name + "Dao {\n"
    code += "\tprivate JdbcTemplate jdbcTemplate;\n"
    code += "\tpublic void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}\n"
    code += "\tprivate static String TABLE_NAME = \"" + table + "\";\n"

    code += "\n"
    code += generate_get(name)

    code += "\n"
    code += generate_add(name, feilds)

    code += "\n"
    code += generate_remove()

    code += "\n"
    code += generate_update(name, feilds)

    code += "\n"
    code += generate_list(name)

    code += "}\n"

    return code

def generate_jqgrid(feilds):
    code = ""

    for item in feilds:
        code += "{\n"
        code += "\tname: \'" + item.key + "\',\n"
        code += "\tindex: \'" + item.sql_key + "\',\n"
        code += "\twidth: 100,\n"
        code += "\talign: \'center\',\n"
        code += "\trequired: true,\n"
        code += "},\n"

    return code

def param_check():
    pass

def main():
    lines = get_lines("sql.txt")
    feilds = parse_feild(lines)
    name = "RecommendAlgorithmUserTestDetail"
    dao = generate_dao(name, "user_test_detail", feilds)
    mapper = generate_mapper(name, feilds)
    entity = generate_entify(name, feilds)

    with open("./result/" + name + "Dao.java", "w") as fd:
        fd.write(dao)

    with open("./result/" + name + "Mapper.java", "w") as fm:
        fm.write(mapper)

    with open("./result/" + name + ".java", "w") as fe:
        fe.write(entity)

    with open("./result/" + name + ".jqgrid.txt", "w") as fj:
        jqgrid = generate_jqgrid(feilds)
        fj.write(jqgrid)

if __name__ == '__main__':
    main()


from __future__ import nested_scopes

from twisted.internet import defer
import sys

class _DeferredCache:
    """ Wraps a call that returns a deferred in a cache. Any subsequent
        calls with the same argument will wait for the first call to
        finish and return the same result (or errback).
    """

    hashableArgs = False
    inProgressOnly = True

    def __init__(self, op, hashableArgs=None, inProgressOnly=None):
        self.op = op
        self.cache = {}
        if hashableArgs is not None:
            self.hashableArgs = hashableArgs
        if inProgressOnly is not None:
            self.inProgressOnly = inProgressOnly

    def cb_triggerUserCallback(self, res, deferred):
        #print "triggering", deferred
        deferred.callback(res)
        return res

    def cb_triggerUserErrback(self, failure, deferred):
        deferred.errback(failure)
        return failure

    def _genCache(self, args, kwargs):
        # This could be better, probably
        try:
            arghash = hash(args)
        except TypeError:
            return None
        kwit = kwargs.items()
        kwit.sort()
        try:
            kwhash = hash(tuple(kwit))
        except TypeError:
            return None
        return (arghash, kwhash)

    def _removeCacheVal(self, res, cacheVal):
        del self.cache[cacheVal]
        return res

    def clearCache(self):
        self.cache = {}

    def call(self, *args, **kwargs):
        # Currently not in progress - start it
        #print "called with", args
        cacheVal = self._genCache(args, kwargs)
        if cacheVal is None and self.hashableArgs:
            raise TypeError('DeferredCache(%s) arguments must be hashable'%(
                                self.op.func_name))

        opdef = self.cache.get(cacheVal)
        if not opdef:
            # XXX assert that it returns a deferred?
            opdef = self.op(*args, **kwargs)
            if cacheVal is not None:
                self.cache[cacheVal] = opdef
            if self.inProgressOnly and cacheVal:
                opdef.addCallbacks(lambda x: self._removeCacheVal(x, cacheVal),
                                   lambda x: self._removeCacheVal(x, cacheVal))

        userdef = defer.Deferred()
        opdef.addCallbacks(lambda x: self.cb_triggerUserCallback(x, userdef),
                           lambda x: self.cb_triggerUserErrback(x, userdef))
        return userdef

def DeferredCache(op=None, hashableArgs=None, inProgressOnly=None):
    """ Use this as a decorator for a function or method that returns a
        deferred. Any subsequent calls using the same arguments will
        be all triggered off the original deferred, all returning the
        same result.
    """
    if op is None:
        return lambda x: DeferredCache(x, hashableArgs, inProgressOnly)
    c = _DeferredCache(op, hashableArgs, inProgressOnly)
    def func(*args, **kwargs):
        return c.call(*args, **kwargs)
    if sys.version_info > (2,4):
        func.func_name = op.func_name
    func.clearCache = c.clearCache
    func.cache_hashableArgs = c.hashableArgs
    func.cache_inProgressOnly = c.inProgressOnly
    return func


# -*- coding: utf-8 -*-
import asyncio

from playwright.async_api import async_playwright

from cf_clearance import async_retry, stealth_async

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False, proxy={"server": "socks5://localhost:7890"}, args=[
            "--disable-gpu",
            '--no-sandbox',
            '--disable-dev-shm-usage',
            '--no-first-run',
            '--no-service-autorun',
            '--no-default-browser-check',
            '--password-store=basic',
            '--start-maximized',
        ])
        content = await browser.new_context(no_viewport=True)
        page = await content.new_page()
        await stealth_async(page)
        await page.goto('https://nowsecure.nl')
        res = await async_retry(page)
        if res:
            cppkies = await page.context.cookies()
            for cookie in cppkies:
                if cookie.get('name') == 'cf_clearance':
                    print(cookie.get('value'))
            ua = await page.evaluate('() => {return navigator.userAgent}')
            print(ua)
        else:
            print("获取失败")
        await browser.close()

asyncio.get_event_loop().run_until_complete(main())


import os
import cv2 as cv
import numpy as np

imagedb_train = ('imagedb_train')

sift = cv.xfeatures2d_SIFT.create()
folders = os.listdir(imagedb_train)

def extract_local_features(path):
    img = cv.imread(path)

    kp = sift.detect(img)
    desc = sift.compute(img, kp)
    desc = desc[1]
    return desc

train_descs = np.zeros((0, 128), dtype=np.float32)

for folder in folders:
    current_folder = os.path.join(imagedb_train, folder)
    print(current_folder)  #gia na vlepw se poio simio ine o kodikas
    files = os.listdir(current_folder)
    for file in files:
        current_file = os.path.join(current_folder, file)
        desc = extract_local_features(current_file)
        train_descs = np.concatenate((train_descs, desc), axis=0)

# Create vocabulary
print("K-Means Calculating...")  #gia na vlepw se poio simio ine o kodikas

term_crit = (cv.TERM_CRITERIA_EPS, 50, 0.1)
loss, assignments, vocabulary = cv.kmeans(train_descs.astype(np.float32), 50, None, term_crit, 1, 0)
np.save('vocabulary.npy', vocabulary)

#vocabulary = np.load('vocabulary.npy')

# Create Histograms
current_label = np.full((1, 1), 1, dtype=int)
train_class_labels = np.zeros((0, 1), dtype=int)

def getBovwDescriptor(desc, vocabulary):
    bow_desc = np.zeros((1, vocabulary.shape[0]), dtype=np.float32)
    for d in range(desc.shape[0]):
        distances = desc[d, :] - vocabulary
        distances = np.abs(distances)
        distances = np.sum(distances, axis=1)
        mini = np.argmin(distances)
        bow_desc[0, mini] += 1
    return bow_desc

bow_descs = np.zeros((0, vocabulary.shape[0]), dtype=np.float32)
for folder in folders:
    current_folder = os.path.join(imagedb_train, folder)
    print(current_folder)
    files = os.listdir(current_folder)
    for file in files:
        current_file = os.path.join(current_folder, file)
        print(current_file)
        desc = extract_local_features(current_file)
        bow_desc = getBovwDescriptor(desc, vocabulary)
        bow_descs = np.concatenate((bow_descs, bow_desc), axis=0)
        train_class_labels=np.concatenate((train_class_labels,current_label), axis=0)
    current_label[0] = current_label[0] + 1

np.save('bow_descs.npy', bow_descs)
np.save('train_class_labels.npy', train_class_labels)


from django.shortcuts import render, redirect
from django.contrib.auth.forms import AuthenticationForm  # 비어있는 폼 제공
from django.contrib.auth import login as auth_login
from django.contrib.auth import logout as auth_logout

# Create your views here.
def login(request):
    if request.method == 'POST':  # 로그인폼을 입력하고 로그인을 누르는 것
        # 로그인 처리를 해줌
        # 1) 입력한 아이디와 패스워드가 일치하는지 검사
        # 2) 일치하면 session table가서 session 만들고,
        # 3) 쿠키에 session id 담아서 전송
        # => django에서 가지고 있다.
        form = AuthenticationForm(request, request.POST)  # (요청들어온 곳에 쿠키 담아서 보냄)
        if form.is_valid():
            auth_login(request, form.get_user())
            # 응답
            return redirect('articles:index')

    else:
        # 비어있는 로그인 페이지를 제공 -> 이미 가지고 있음
        form = AuthenticationForm()
    context = {'form' : form}
    return render(request, 'accounts/login.html', context)

def logout(request):
    # 현재 request로 들어왔는데,
    # 요청에 있는 쿠키를 열어서 session id가 있으면 꺼내서
    # db의 session table과 비교해서 있으면 로그아웃
    auth_logout(request)
    return redirect('articles:index')


from config import *
from lib import cords, led
from utils.time import sleep_ms

def _is_row(side):
    return side == cords.TOP or side == cords.BOTTOM

def lines(gen, source=cords.TOP):
    side = DISPLAY_ROWS if _is_row(source) else DISPLAY_COLUMNS
    other_side = DISPLAY_COLUMNS if _is_row(source) else DISPLAY_ROWS
    for i in range(side):
        color = gen.generate()
        for j in range(other_side):
            pos = cords.Cords(j, i)
            if source == cords.RIGHT or source == cords.BOTTOM:
                pos.mirror_x()
            if source == cords.LEFT or source == cords.RIGHT:
                pos.flip()
            led._pixel(color, pos)
        led._show()
        sleep_ms(100)


import os
import pickle
import numpy as np
import pandas as pd
import sdv
from imblearn.over_sampling import RandomOverSampler
from imblearn.under_sampling import RandomUnderSampler
from scipy.stats import pearsonr
from sklearn import preprocessing
from sklearn.feature_selection import VarianceThreshold
from sklearn.linear_model import Lasso
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.utils import shuffle
from hyperparameters import classifiers, param_grids

from settings import file_extension, undersample, oversample, noise, synthetic, lasso

def create_folders():
    folders = ['results', 'models'] if "validate" not in file_extension else ['results']
    for name in folders:
        directory = file_extension + '_' + name + '/'
        if not os.path.exists(directory): os.makedirs(directory)

def read_files():
    df_radiomics = pd.read_excel('data/' + file_extension + "_radiomics.xlsx", engine='openpyxl')
    df_radiomics = df_radiomics.loc[:, ~df_radiomics.columns.duplicated()]
    start_idx = df_radiomics.columns.get_loc("original_shape_Elongation")
    df_radiomics = df_radiomics.iloc[:, start_idx:]

    df_events = pd.read_excel('data/' + "db_basis_" + file_extension + ".xlsx", engine='openpyxl')
    df_events = df_events.rename(columns={"Randomisatie nummer": "PP"})
    df_events = df_events.loc[:, ~df_events.columns.str.contains('^Unnamed')]

    df = pd.merge(df_radiomics, df_events, on='PP', how='outer').drop_duplicates()
    df = df[df['Event'].notna()]

    return df

def train_test_split(df, test_size):
    df = shuffle(df)

    df_test = df.iloc[:test_size, :]
    df_test = df_test.reset_index()
    df_test = df_test.drop(columns='index')

    df_train = df.iloc[test_size:, :]
    df_train = df_train.reset_index()
    df_train = df_train.drop(columns='index')

    if synthetic:
        model = sdv.tabular.GaussianCopula(primary_key='PP')
        model.fit(df_train)
        df_train += model.sample(200)

    return df_train, df_test

def normalize_data(df, min_max_scaler=None):
    events = df.Event.values
    vals = df.drop(columns=['Event', 'PP']).values
    cols = df.drop(columns=['Event', 'PP']).columns
    if not min_max_scaler: min_max_scaler = preprocessing.MinMaxScaler()
    vals_scaled = min_max_scaler.fit_transform(vals)
    df = pd.DataFrame(vals_scaled, columns=cols)
    df['Event'] = events
    return df, min_max_scaler

def variance_threshold_selector(data, threshold=0.5):
    selector = VarianceThreshold(threshold)
    selector.fit(data)
    return data[data.columns[selector.get_support(indices=True)]]

def remove_variance(df):
    min_variance = 1e-3
    low_variance = variance_threshold_selector(df, min_variance)
    return low_variance

def pearson_correlation(df):
    df_pearson = []
    for index in range(0, len(df.columns) - 1):
        name = df.columns[index]
        df_pearson.append({'Name': name,
                           'Correlation': pearsonr(df.Event, df.iloc[:, index])[0],
                           'P-value': pearsonr(df.Event, df.iloc[:, index])[1]})
    df_pearson = pd.DataFrame(df_pearson)
    df_pearson = df_pearson[df_pearson['P-value'] < 0.05]
    features = df_pearson.Name.to_list() + ['Event']
    df_train = df.loc[:, features]
    return df_train, features

def lasso_reduction(df_train, df_test, features):
    x_train = df_train.to_numpy()[:, :-1]
    y_train = df_train.Event.to_numpy()

    pipeline = Pipeline([('scaler', StandardScaler()), ('model', Lasso())])
    search = GridSearchCV(pipeline, {'model__alpha': np.arange(0.1, 10, 0.1)}, cv=5, scoring="neg_mean_squared_error",
                          verbose=0)
    search.fit(x_train, y_train)
    coefficients = search.best_estimator_.named_steps['model'].coef_
    importance = np.abs(coefficients)
    features = np.array(features[0:len(features) - 1])
    features_lasso = list(features[importance > 0]) + ['Event']

    df_train = df_train.loc[:, features_lasso]
    df_test = df_test.loc[:, features_lasso]

    return df_train, df_test, features_lasso

def preprocess_train_data(df_train):
    df_train, min_max_scaler = normalize_data(df_train)
    df_train = remove_variance(df_train)
    if not lasso: df_train, features = pearson_correlation(df_train)
    if lasso: features = df_train.columns

    return df_train, min_max_scaler, features

def preprocess_test_data(df, min_max_scaler, features):
    df, _ = normalize_data(df, min_max_scaler=min_max_scaler)

    return df.loc[:, features]

def preprocess_data(df_train, df_test):
    df_train, min_max_scaler, features = preprocess_train_data(df_train)
    df_test = preprocess_test_data(df_test, min_max_scaler, features)
    if lasso: df_train, df_test, features = lasso_reduction(df_train, df_test, features)

    x_train, y_train = df_train.to_numpy()[:, :-1], df_train.Event.to_numpy()
    x_test, y_test = df_test.to_numpy()[:, :-1], df_test.Event.to_numpy()

    return x_train, y_train, x_test, y_test, min_max_scaler, features

def get_best_grid(x_train, y_train, model):
    if 0.9 * len(y_train) - sum(y_train) > sum(y_train):
        oversampler = RandomOverSampler(sampling_strategy=0.90)
        if oversample: x_train, y_train = oversampler.fit_resample(x_train, y_train)
    undersampler = RandomUnderSampler(sampling_strategy='majority')
    if undersample: x_train, y_train = undersampler.fit_resample(x_train, y_train)
    if noise: x_train = x_train + np.random.normal(0.0, 0.2, size=x_train.shape)

    param_grid = param_grids[model]
    grid = GridSearchCV(classifiers[model], param_grid=param_grid, scoring="roc_auc", refit=True, cv=5, n_jobs=-1,
                        verbose=0)
    grid.fit(x_train, y_train)
    return grid

def save_model(best_grid, x_test, y_test, features, scaler, model):
    pickle.dump(best_grid, open(file_extension + '_models/' + model + '_model.pkl', 'wb'))
    pickle.dump(x_test, open(file_extension + '_models/' + model + '_x_test.pkl', 'wb'))
    pickle.dump(y_test, open(file_extension + '_models/' + model + '_y_test.pkl', 'wb'))
    pickle.dump(features, open(file_extension + '_models/' + model + '_features.pkl', 'wb'))
    pickle.dump(scaler, open(file_extension + '_models/' + model + '_scaler.pkl', 'wb'))

def jitter(x, scale=0.1):
    return x + np.random.normal(0, scale, x.shape)

def jitter_test(classifier, x_test, y_test, scales=np.linspace(0, 0.5, 30), n=5):
    out = []
    for s in scales:
        avg = 0.0
        for r in range(n):
            avg += roc_auc_score(y_test, classifier.predict_proba(jitter(x_test, s))[:, 1])
        out.append(avg / n)
    return out, scales


from logger import Logger

l = Logger()

@l.logging_func
def test_func():
    [i**i for i in range(1000)]

for _ in range(3):
    test_func()

l.logging_start()
for _ in range(3):
    [i**i for i in range(1000)]
l.logging_end('cycle')

l.print_results()


from mako import runtime, filters, cache
UNDEFINED = runtime.UNDEFINED
__M_dict_builtin = dict
__M_locals_builtin = locals
_magic_number = 4
_modified_time = 1224684001.22454
_template_filename='/media/disk/Pylons Book/Code/chapter06/FormExample-01/formexample/templates/derived/form.html'
_template_uri='/derived/form.html'
_template_cache=cache.Cache(__name__, _modified_time)
_source_encoding='utf-8'
from webhelpers.html import escape
_exports = ['person', 'study']

def _mako_get_namespace(context, name):
    try:
        return context.namespaces[(__name__, name)]
    except KeyError:
        _mako_generate_namespaces(context)
        return context.namespaces[(__name__, name)]
def _mako_generate_namespaces(context):
    pass
def _mako_inherit(template, context):
    _mako_generate_namespaces(context)
    return runtime._inherit_from(context, '/base/index.html', _template_uri)
def render_body(context,**pageargs):
    context.caller_stack._push_frame()
    try:
        __M_locals = __M_dict_builtin(pageargs=pageargs)
        h = context.get('h', UNDEFINED)
        def study():
            return render_study(context.locals_(__M_locals))
        __M_writer = context.writer()
        # SOURCE LINE 1
        __M_writer(u'\n\n')
        # SOURCE LINE 19
        __M_writer(u'\n\n')
        # SOURCE LINE 51
        __M_writer(u'\n\n
Create a Study
\n\n')
        # SOURCE LINE 55
        __M_writer(escape(h.form(h.url_for(controller='study', action='process'))))
        __M_writer(u'\n')
        # SOURCE LINE 56
        __M_writer(escape(study()))
        __M_writer(u'\n')
        # SOURCE LINE 57
        __M_writer(escape(h.submit(name="action", value="Save")))
        __M_writer(u'\n')
        # SOURCE LINE 58
        __M_writer(escape(h.submit(name="action", value="Add New Person")))
        __M_writer(u'\n')
        # SOURCE LINE 59
        __M_writer(escape(h.end_form()))
        __M_writer(u'\n\n')
        return ''
    finally:
        context.caller_stack._pop_frame()

def render_person(context,id):
    context.caller_stack._push_frame()
    try:
        h = context.get('h', UNDEFINED)
        __M_writer = context.writer()
        # SOURCE LINE 21
        __M_writer(u'\n    <fieldset><legend>Person</legend>\n\n        Title
\n        ')
        # SOURCE LINE 25
        __M_writer(escape(h.text(name="person-%s.title"%(id), id="person-%s.title"%(id))))
        __M_writer(u'
\n\n        First Name
\n        ')
        # SOURCE LINE 28
        __M_writer(escape(h.text(
            name="person-%s.firstname"%(id),
            id="person-%s.firstname"%(id
        ))))
        # SOURCE LINE 31
        __M_writer(u'
\n\n        Surname
\n        ')
        # SOURCE LINE 34
        __M_writer(escape(h.text(name="person-%s.surname"%(id), id="person-%s.surname"%(id))))
        __M_writer(u'
\n\n        Role
\n        ')
        # SOURCE LINE 37
        __M_writer(escape(h.select(
            "person-%s.role"%(id),
            [],
            [
                ['1', 'Chief Investigator'],
                ['2', 'Assistant'],
                ['3', 'Student'],
            ],
            id="person-%s.role"%(id),
        )))
        # SOURCE LINE 46
        __M_writer(u'
\n\n        ')
        # SOURCE LINE 48
        __M_writer(escape(h.submit(name="action", value="Remove %s"%(id))))
        __M_writer(u'\n\n    </fieldset>
\n')
        return ''
    finally:
        context.caller_stack._pop_frame()

def render_study(context):
    context.caller_stack._push_frame()
    try:
        h = context.get('h', UNDEFINED)
        range = context.get('range', UNDEFINED)
        c = context.get('c', UNDEFINED)
        def person(id):
            return render_person(context,id)
        __M_writer = context.writer()
        # SOURCE LINE 3
        __M_writer(u'\n    <fieldset><legend>Study</legend>\n\n        Title
\n        ')
        # SOURCE LINE 7
        __M_writer(escape(h.text(name="title", id="title")))
        __M_writer(u'
\n\n        Start Date
\n        ')
        # SOURCE LINE 10
        __M_writer(escape(h.text(name="start_date", id="startdate")))
        __M_writer(u'
\n\n        End Date
\n        ')
        # SOURCE LINE 13
        __M_writer(escape(h.text(name="end_date", id="enddate")))
        __M_writer(u'
\n\n    </fieldset>
\n')
        # SOURCE LINE 16
        for id in range(c.number_of_people):
            # SOURCE LINE 17
            __M_writer(u'            ')
            __M_writer(escape(person(id=id)))
            __M_writer(u'\n')
        return ''
    finally:
        context.caller_stack._pop_frame()


## modified the code: args.batch_size_val == 1

import os
import random
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.parallel
import torch.utils.data
from visdom_logger import VisdomLogger
from collections import defaultdict
from .dataset.dataset import get_val_loader
from .util import AverageMeter, batch_intersectionAndUnionGPU, get_model_dir, main_process
from .util import find_free_port, setup, cleanup, to_one_hot, intersectionAndUnionGPU
from .classifier import Classifier
from .model.pspnet import get_model
import torch.distributed as dist
from tqdm import tqdm
from .util import load_cfg_from_cfg_file, merge_cfg_from_list
import argparse
from torch.nn.parallel import DistributedDataParallel as DDP
import torch.multiprocessing as mp
import time
from .visu import make_episode_visualization
from typing import Tuple

def parse_args() -> None:
    parser = argparse.ArgumentParser(description='Testing')
    parser.add_argument('--config', type=str, required=True, help='config file')
    parser.add_argument('--opts', default=None, nargs=argparse.REMAINDER)
    args = parser.parse_args()
    assert args.config is not None
    cfg = load_cfg_from_cfg_file(args.config)
    if args.opts is not None:
        cfg = merge_cfg_from_list(cfg, args.opts)
    return cfg

def main_worker(rank: int,
                world_size: int,
                args: argparse.Namespace) -> None:

    print(f"==> Running DDP checkpoint example on rank {rank}.")
    setup(args, rank, world_size)

    if args.manual_seed is not None:
        cudnn.benchmark = False
        cudnn.deterministic = True
        torch.cuda.manual_seed(args.manual_seed + rank)
        np.random.seed(args.manual_seed + rank)
        torch.manual_seed(args.manual_seed + rank)
        torch.cuda.manual_seed_all(args.manual_seed + rank)
        random.seed(args.manual_seed + rank)

    # ========== Model  ==========
    model = get_model(args).to(rank)
    model = nn.SyncBatchNorm.convert_sync_batchnorm(model)
    model = DDP(model, device_ids=[rank])

    root = get_model_dir(args)

    if args.ckpt_used is not None:
        filepath = os.path.join(root, f'{args.ckpt_used}.pth')
        assert os.path.isfile(filepath), filepath
        print("=> loading weight '{}'".format(filepath))
        checkpoint = torch.load(filepath)
        model.load_state_dict(checkpoint['state_dict'])
        print("=> loaded weight '{}'".format(filepath))
    else:
        print("=> Not loading anything")

    # ========== Data  ==========
    episodic_val_loader, _ = get_val_loader(args)

    # ========== Test  ==========
    val_Iou = episodic_validate(args=args,
                                          val_loader=episodic_val_loader,
                                          model=model,
                                          use_callback=(args.visdom_port != -1),
                                          suffix=f'test')
    if args.distributed:
        dist.all_reduce(val_Iou)
        val_Iou /= world_size

    cleanup()

def episodic_validate(args: argparse.Namespace,
                      val_loader: torch.utils.data.DataLoader,
                      model: DDP,
                      use_callback: bool,
                      suffix: str = 'test') -> Tuple[torch.tensor, torch.tensor]:

    print('==> Start testing')

    model.eval()
    nb_episodes = int(args.test_num)

    # ========== Metrics initialization  ==========

    H, W = args.image_size, args.image_size
    c = model.module.bottleneck_dim
    if args.image_size == 473:
        h, w = 60, 60
    else:
        h, w = model.module.feature_res   # (53, 53)

    runtimes = torch.zeros(args.n_runs)
    deltas_init = torch.zeros((args.n_runs, nb_episodes, 1))
    deltas_final = torch.zeros((args.n_runs, nb_episodes, 1))
    val_IoUs0 = np.zeros(args.n_runs)

    # ========== Perform the runs  ==========
    for run in tqdm(range(args.n_runs)):

        # =============== Initialize the metric dictionaries ===============

        loss_meter = AverageMeter()
        iter_num = 0
        cls_intersection0 = defaultdict(int)  # Default value is 0
        cls_union0 = defaultdict(int)
        IoU0 = defaultdict(int)

        cls_intersection1 = defaultdict(int)  # Default value is 0
        cls_union1 = defaultdict(int)
        IoU1 = defaultdict(int)

        # =============== episode = group of tasks ===============
        runtime = 0
        for e in tqdm(range(nb_episodes)):
            t0 = time.time()
            iter_num += 1

            try:
                q_img, q_label, s_img, s_label, subcls, _, _ = iter_loader.next()
            except:
                iter_loader = iter(val_loader)
                q_img, q_label, s_img, s_label, subcls, _, _ = iter_loader.next()
            iter_num += 1
            q_img = q_img.to(dist.get_rank(), non_blocking=True)      # [1, 3, h, w]
            q_label = q_label.to(dist.get_rank(), non_blocking=True)  # [1, h, w]
            s_img = s_img.to(dist.get_rank(), non_blocking=True)      # [1, 1, 3, h, w]
            s_label = s_label.to(dist.get_rank(), non_blocking=True)  # [1, 1, h, w]

            classes = [[class_.item() for class_ in subcls]]  # All classes considered in the tasks, list of list

            def fit_model(s_img_, s_label_, q_img_, q_label_, classes):
                features_s = torch.zeros(1, args.shot * args.meta_aug, c, h, w).to(dist.get_rank())  # [1, 2, c, h, w]
                features_q = torch.zeros(1, 1, c, h, w).to(dist.get_rank())                          # [1, 1, c, h, w]
                gt_s = 255 * torch.ones(1, args.shot * args.meta_aug, args.image_size, args.image_size).long().to(dist.get_rank())  # [1, 2, h, w]
                gt_q = 255 * torch.ones(1, 1, args.image_size, args.image_size).long().to(dist.get_rank())                          # [1, 1, h, w]
                n_shots = torch.zeros(1).to(dist.get_rank())

                # =========== Generate tasks and extract features for each task ===============
                with torch.no_grad():
                    f_s = model.module.extract_features(s_img_.squeeze(0))    # [shot, ch, h, w]
                    f_q = model.module.extract_features(q_img_)               # [1, ch, h, w]

                    shot = f_s.size(0)
                    n_shots[0] = shot
                    features_s[0, :shot] = f_s.detach()
                    features_q[0] = f_q.detach()
                    gt_s[0, :shot] = s_label_   # [1, 2/shot, h, w]   ====
                    gt_q[i, 0] = q_label_       # [1, h, w]           ====

                # =========== Normalize features along channel dimension ===============
                if args.norm_feat:
                    features_s = F.normalize(features_s, dim=2)
                    features_q = F.normalize(features_q, dim=2)

                # =========== Create a callback is args.visdom_port != -1 ===============
                callback = VisdomLogger(port=args.visdom_port) if use_callback else None

                # ===========  Initialize the classifier + prototypes + F/B parameter Π ===============
                classifier = Classifier(args)
                classifier.init_prototypes(features_s, features_q, gt_s, gt_q, classes, callback)
                batch_deltas = classifier.compute_FB_param(features_q=features_q, gt_q=gt_q)
                deltas_init[run, e, :] = batch_deltas.cpu()

                # =========== Perform RePRI inference ===============
                batch_deltas = classifier.RePRI(features_s, features_q, gt_s, gt_q, classes, n_shots, callback)
                deltas_final[run, e, :] = batch_deltas

                logits = classifier.get_logits(features_q)  # [n_tasks, shot, h, w]
                logits = F.interpolate(logits, size=(H, W), mode='bilinear', align_corners=True)
                probas = classifier.get_probas(logits).detach()
                intersection, union, _ = batch_intersectionAndUnionGPU(probas, gt_q, 2)  # [n_tasks, shot, num_class]
                intersection, union = intersection.cpu(), union.cpu()
                return intersection, union

            intersection0, union0 = fit_model(s_img[:,:1], s_label[:, :1], q_img, q_label, classes)   # s_img [1, 1, 3, h, w] s_label # [1, 1, h, w]
            intersection1, union1 = fit_model(s_img, s_label, q_img, q_label, classes)
            runtime += time.time() - t0
            # ================== Log metrics ==================

            for i, task_classes in enumerate(classes):
                for j, class_ in enumerate(task_classes):
                    cls_intersection0[class_] += intersection0[i, 0, j + 1]  # Do not count background
                    cls_union0[class_] += union0[i, 0, j + 1]
                    cls_intersection1[class_] += intersection1[i, 0, j + 1]  # Do not count background
                    cls_union1[class_] += union1[i, 0, j + 1]

            for class_ in cls_union0:
                IoU0[class_] = cls_intersection0[class_] / (cls_union0[class_] + 1e-10)
                IoU1[class_] = cls_intersection1[class_] / (cls_union1[class_] + 1e-10)

            if (iter_num % 200 == 0):
                mIoU0 = np.mean([IoU0[i] for i in IoU0])
                mIoU1 = np.mean([IoU1[i] for i in IoU1])
                print('Test: [{}/{}] '
                      'mIoU0 {:.4f}, mIoU1 {:.4f}'.format(iter_num, args.test_num, mIoU0, mIoU1))

            # ================== Visualization ==================

        runtimes[run] = runtime
        mIoU0 = np.mean(list(IoU0.values()))
        mIoU1 = np.mean(list(IoU1.values()))
        print('mIoU---Val result: mIoU0 {:.4f}, mIoU1 {:.4f}.'.format(mIoU0, mIoU1))
        for class_ in cls_union0:
            print("Class {} : IoU0 {:.4f}, IoU1 {:.4f}".format(class_, IoU0[class_], IoU1[class_]))

        val_IoUs0[run] = mIoU0

    # ================== Save metrics ==================
    if args.save_oracle:
        root = os.path.join('plots', 'oracle')
        os.makedirs(root, exist_ok=True)
        np.save(os.path.join(root, 'delta_init.npy'), deltas_init.numpy())
        np.save(os.path.join(root, 'delta_final.npy'), deltas_final.numpy())

    print('Average mIoU over {} runs --- {:.4f}.'.format(args.n_runs, val_IoUs0.mean()))
    print('Average runtime / run --- {:.4f}.'.format(runtimes.mean()))

    return val_IoUs0.mean()

def standard_validate(args: argparse.Namespace,
                      val_loader: torch.utils.data.DataLoader,
                      model: DDP,
                      use_callback: bool,
                      suffix: str = 'test') -> Tuple[torch.tensor, torch.tensor]:

    print('==> Standard validation')
    model.eval()
    loss_fn = torch.nn.CrossEntropyLoss(ignore_index=255)
    iterable_val_loader = iter(val_loader)

    bar = tqdm(range(len(iterable_val_loader)))

    loss = 0.
    intersections = torch.zeros(args.num_classes_tr).to(dist.get_rank())
    unions = torch.zeros(args.num_classes_tr).to(dist.get_rank())

    with torch.no_grad():
        for i in bar:
            images, gt = iterable_val_loader.next()
            images = images.to(dist.get_rank(), non_blocking=True)
            gt = gt.to(dist.get_rank(), non_blocking=True)
            logits = model(images).detach()
            loss += loss_fn(logits, gt)
            intersection, union, _ = intersectionAndUnionGPU(logits.argmax(1),
                                                             gt,
                                                             args.num_classes_tr,
                                                             255)
            intersections += intersection
            unions += union
        loss /= len(val_loader.dataset)

    if args.distributed:
        dist.all_reduce(loss)
        dist.all_reduce(intersections)
        dist.all_reduce(unions)

    mIoU = (intersections / (unions + 1e-10)).mean()
    loss /= dist.get_world_size()
    return mIoU, loss

if __name__ == "__main__":
    args = parse_args()
    os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(str(x) for x in args.gpus)

    if args.debug:
        args.test_num = 500
        args.n_runs = 2

    world_size = len(args.gpus)
    distributed = world_size > 1
    args.distributed = distributed
    args.port = find_free_port()
    mp.spawn(main_worker,
             args=(world_size, args),
             nprocs=world_size,
             join=True)


class Solution:
    def lexicalOrder(self, n: int) -> list:
        datas = [[] for _ in range(10)]
        for i in range(1, n+1):
            s = str(i)
            datas[int(s[0])].append(s)

        for tmp in datas:
            tmp.sort()

        ans = []
        for tmp in datas:
            tmp = list(map(int, tmp))
            ans += tmp

        return ans

n = 13
ans = Solution().lexicalOrder(n)
print(ans)


# Idea was thought of back in June of 2016 in regards to creating a way for qvpython script to easily manipulate an excel spreadsheet. After obtaining help from Vaso Vasich, this script was actually made in an effort by Vaso Vasich under the SDEP business, which is a subset of Arce Enterprises.
# 2016
# First Author: Vaso Vasich
# Second Author: David Arce

# Program is designed to make a fluid way for product to be added to an excel sheet with minimal effort in order to make it quicker to add mass amounts of product. This excel sheet is then uploaded to an ecommerce website with no changes needed.

part_types = {
    'Rocker Arms': {
        'specs': [
            'Stud Diameter(in.):',
            'Ratio:'
        ],
        'set': 16,
        'images': {
        # still need to add more
            'Ultra Pro Magnum': [
                'https://s25.postimg.org/vd6zj9x8v/ultra_pro_magnum_rocker_arms_1.jpg',
                'https://s25.postimg.org/l4eidg973/ultra_pro_magnum_rocker_arms_2.jpg'
            ],
            'Ultra Gold Aluminum': [
                'https://s25.postimg.org/k73hqu8b3/ultra_gold_rocker_arms_1.jpg'
            ],
        }
    }

}

def inven():
    import openpyxl
    from openpyxl.styles import Alignment
    wb = openpyxl.load_workbook('testSheet.xlsx')
    sheet = wb.active

    #VARIABLES
    running = True
    placer = 0
    brand = ""
    productNum = ""
    prodPrice = 0
    prodName = ""
    trackInventory = "TRUE"
    quantity = 4
    backorder1 = "FALSE"
    prodWeight = ""
    taxable= "TRUE"
    hidden = "FALSE"
    prodCategory = ""
    prodCategoryWeb = "http://sandiegoengineparts.com/t/"
    prodBrandWeb = "http://sandiegoengineparts.com/t/"
    prodNameWeb = "http://sandiegoengineparts.com/products?keywords="
    prodImageUrl = ""
    imageIndex = 0

    while(running):
        question = input("New Product to add! \n Is new product similar to last? y or n: ")
        if(question == 'y'):
            placer = int(placer)
            placer += 1
            placer = str(placer)
            productNum = input("Part #: ")
            prodPrice = float(input("Price: "))
            prodPrice = round(prodPrice,2)
            imageIndex = checkImages(part_types[prodType]['images'][prodLine], prodType, imageIndex)
            prodImageUrl = part_types[prodType]['images'][prodLine][imageIndex]
            imageIndex += 1
            sheet['A'+placer].alignment = Alignment(wrapText=True)
            sheet['A'+placer].value = prodName + " " + productNum
            sheet['B'+placer].alignment = Alignment(wrapText=True)
            sheet['B'+placer].value = productNum
            sheet['C'+placer].alignment = Alignment(wrapText=True)
            sheet['C'+placer].value = prodPrice
            sheet['F'+placer].alignment = Alignment(wrapText=True)
            sheet['F'+placer].value = trackInventory
            sheet['G'+placer].alignment = Alignment(wrapText=True)
            sheet['G'+placer].value = quantity
            sheet['H'+placer].alignment = Alignment(wrapText=True)
            sheet['H'+placer].value = backorder1
            appliesTo = applies()
            prodSpecs = getSpecifications(prodType)
            sheet['E'+placer].alignment = Alignment(wrapText=True)
            sheet['E'+placer].value ="""

{1} {2} Specifications

Brand: {3}

Manufacturer's Part Number: {1}

Part Type: {2}

Product Line: {0}

    {9}

Sold in Set of {4}

Applies to:

{5}

""".format(prodName,productNum,prodType,prodBrand,prodSet,appliesTo,prodBrandWeb,prodCategoryWeb,prodNameWeb,prodSpecs)

            sheet['I'+placer].alignment = Alignment(wrapText=True)
            sheet['I'+placer].value = prodWeight
            sheet['J'+placer].alignment = Alignment(wrapText=True)
            sheet['J'+placer].value = taxable
            sheet['K'+placer].alignment = Alignment(wrapText=True)
            sheet['K'+placer].value = hidden
            sheet['L'+placer].alignment = Alignment(wrapText=True)
            sheet['L'+placer].value = prodCategory
            sheet['M'+placer].alignment = Alignment(wrapText=True)
            sheet['M'+placer].value = prodImageUrl
            sheet['N'+placer].alignment = Alignment(wrapText=True)
            sheet['N'+placer].value = prodName + " " + productNum
            sheet['O'+placer].alignment = Alignment(wrapText=True)
            sheet['O'+placer].value = "Find " + prodName + " " + productNum +" at San Diego Engine Parts where high quality engine parts meet low costs."
            wb.save('testSheet.xlsx')

        # New Product
        else:
            if(question == 'n'):
                prodType = input("Type of Part(eg. Rocker Arms): ")
                # If (prodType in part_types) do all this...don't need check necessarily now
                prodBrand = input("Brand (eg. COMP Cams): ")
                prodLine = input("Product Line (eg. Ultra Pro Magnum): ")
                productNum = input("Part #: ")
                prodCategory = makeCategories(prodType, prodBrand)

                prodCategoryWeb += prodType.replace(' ', '-').lower()
                # prodCategoryWeb += input("Type of Part URL: ")
                prodBrandWeb += prodBrand.replace(' ', '-').lower()
                # prodBrandWeb += input("Brand URL: ")
                prodName = prodBrand + ' ' + prodLine + ' ' + prodType
                # prodName = input("(Brand - Product Line - Type): ")
                prodNameWeb += prodName.replace(' ', '+').lower()
                # prodNameWeb += input("Product Line URL: ")
                prodWeight = int(input("Weight: "))
                prodSet = part_types[prodType]['set']
                placer = input("Row: ")
                # prodRAdiameter = input("Diameter: ")
                # prodRAratio = input("Ratio: ")
                prodPrice = float(input("Price: "))
                prodPrice = round(prodPrice,2)
                if (prodLine in part_types[prodType]['images']):
                    prodImageUrl = part_types[prodType]['images'][prodLine][0]
                    imageIndex += 1
                else:
                    print('No Image was found for ' + prodLine)
                # prodImageUrl = input("Image Url: ")
                sheet['A'+placer].alignment = Alignment(wrapText=True)
                sheet['A'+placer].value = prodName + " " + productNum
                sheet['B'+placer].alignment = Alignment(wrapText=True)
                sheet['B'+placer].value = productNum
                sheet['C'+placer].alignment = Alignment(wrapText=True)
                sheet['C'+placer].value = prodPrice
                sheet['F'+placer].alignment = Alignment(wrapText=True)
                sheet['F'+placer].value = trackInventory
                sheet['G'+placer].alignment = Alignment(wrapText=True)
                sheet['G'+placer].value = quantity
                sheet['H'+placer].alignment = Alignment(wrapText=True)
                sheet['H'+placer].value = backorder1
                # running = True
                appliesTo = applies()
                prodSpecs = getSpecifications(prodType)
                sheet['E'+placer].alignment = Alignment(wrapText=True)
                sheet['E'+placer].value = """

{1} {2} Specifications

Brand: {3}

Manufacturer's Part Number: {1}

Part Type: {2}

Product Line: {0}

    {9}

Sold in Set of {4}

Applies to:

{5}

""".format(prodName,productNum,prodType,prodBrand,prodSet,appliesTo,prodBrandWeb,prodCategoryWeb,prodNameWeb,prodSpecs)
# .format(prodName,productNum,prodType,prodBrand,prodSet,appliesTo,prodBrandWeb,prodCategoryWeb,prodNameWeb,prodRAratio,prodRAdiameter)

                sheet['I'+placer].alignment = Alignment(wrapText=True)
                sheet['I'+placer].value = prodWeight
                sheet['J'+placer].alignment = Alignment(wrapText=True)
                sheet['J'+placer].value = taxable
                sheet['K'+placer].alignment = Alignment(wrapText=True)
                sheet['K'+placer].value = hidden
                sheet['L'+placer].alignment = Alignment(wrapText=True)
                sheet['L'+placer].value = prodCategory
                sheet['M'+placer].alignment = Alignment(wrapText=True)
                sheet['M'+placer].value = prodImageUrl
                sheet['N'+placer].alignment = Alignment(wrapText=True)
                sheet['N'+placer].value = prodName + " " + productNum
                sheet['O'+placer].alignment = Alignment(wrapText=True)
                sheet['O'+placer].value = "Find " + prodName + " " + productNum +" at San Diego Engine Parts where high quality engine parts meet low costs."
                wb.save('testSheet.xlsx')
            else:
              if(question == 'x'):
                running = False
        wb.save('testSheet.xlsx')

def applies():
    running = True
    appliesTo = ""
    while(running):
        question = input("Continue adding Applies to? y or n: ")
        if(question == 'y'):
            appliesTo = appliesTo + '
' + input("Applies to: ") + '
\n'
        else:
            if(question == 'n'):
                return appliesTo

def getSpecifications(product_type):
    running = True
    prodSpecs = ""
    while(running):
        for k in part_types[product_type]['specs']:
            v = input('Get ' + k)
            prodSpecs += '
' + k + ' ' + v + '
\n'
        return prodSpecs

def makeCategories(product_type, product_brand):
    categories = product_type + ', Brands/' + product_brand + ', Brands/' + product_brand + '/' + product_brand + ' ' + product_type
    return categories

def checkImages(product_line, product_type, index):
    imageGroupLength = len(part_types[product_type]['images'])
    if (imageGroupLength == 1):
        index = 0
        return index
    else:
        if (index < imageGroupLength):
            return index
        else:
            index = 0
            return index

inven()


#!/usr/bin/env python3.8

"""
Author : Kashif Khan
This script helps to search Endpoint in following criteria.
1: All results
2: Filter by Node ID
3: Filter by EPG
4: Filter by VLAN ID
5: Filter by Interface Name
6: Filter by Tenant Name
7: Filter by MAC Address

"""
# Imports block
from connectivity import get_aci_token
from credentials import credentials
import json
import requests
from pprint import pprint
from prettytable import PrettyTable
import csv

def get_ep_details(aci_cookie, apic_ip):
    """
    Fetches EP details using API call

    Parameters:

    aci_cookie (dict): Session dictionary
    apic_ip (string): APIC controller ip address
    mac_address (string): (Optional) default is None. It searches specific mac-address

    Returns:
    dict: API response in JSON

    """

    url = f'{apic_ip}/api/node/class/fvCEp.json?rsp-subtree=full&rsp-subtree-class=fvCEp,fvRsCEpToPathEp,fvIp,fvRsHyper,fvRsToNic,fvRsToVm'

    headers = {
        'cache-control': "no-cache"
    }

    get_response = requests.get(
        url, headers=headers, cookies=aci_cookie, verify=False).json()

    return get_response

def get_processed_data(get_ep_details_result):

    data = []
    fields = ['mac', 'ip','encap','dn','path']

    for each_ep in get_ep_details_result['imdata']:
        line_dict = {}
        for key,value in each_ep['fvCEp'].items():

            if isinstance (value,list):
                for each_tdn in value:
                    if each_tdn.get('fvRsCEpToPathEp'):
                        line_dict.update({'path':each_tdn['fvRsCEpToPathEp']['attributes']['tDn']})
            else:
                line_dict.update({'mac':value['mac']})
                line_dict.update({'ip':value['ip']})
                line_dict.update({'encap':value['encap']})
                line_dict.update({'dn':value['dn']})

        data.append(line_dict)

    processed_data = list()

    for row in data:

        dn_splitted_list = row['dn'].split("/")
        tdn_splitted_list = row['path'].split("/")[2:]

        switch_splitted = tdn_splitted_list[0].split("-")[1:]
        tdn_splitted_list = tdn_splitted_list[1:]
        tdn_splitted_list = "/".join(tdn_splitted_list)
        interface_splitted = tdn_splitted_list.split("-")[1:]

        mac = row['mac']
        vlan = row['encap'].lstrip("vlan-")
        tenant = dn_splitted_list[1].lstrip("tn-")
        ap = dn_splitted_list[2].lstrip("ap-")
        epg = dn_splitted_list[3].lstrip("epg-")
        switch = "-".join(switch_splitted)
        interface = "-".join(interface_splitted).strip("[]")
        temp_dict = {'mac_address':mac,'vlan':vlan,'tenant':tenant,'application_profile':ap,'epg':epg,'switch':switch,'interface':interface}

        processed_data.append(temp_dict)
    return processed_data

def print_details_onscreen(processed_data):

    table = PrettyTable()
    table.field_names = ['MAC Address','VLAN','Tenant','AP','EPG','Switch','Interface']

    table.align['Interface'] = 'l'
    table.align['MAC Address'] = 'l'
    table.align['VLAN'] = 'l'
    table.align['Tenant'] = 'l'
    table.align['AP'] = 'l'
    table.align['EPG'] = 'l'
    table.align['Switch'] = 'l'

    count = 0
    for each_row in processed_data:

        count += 1
        only_values = [values for values in each_row.values()]
        table.add_row(only_values)  # adds each mac address detail in table.

    print(table)
    print(f"Total number of interfaces where Endpoint mac-address learnd are {count}")

def get_filtered_data_func(filter_value,filter_type,get_data):

    get_filtered_data = [filtered_data
                        for filtered_data in get_data
                        if filter_value in filtered_data[filter_type]]
    print_details_onscreen(get_filtered_data)

def save_to_csv(list_of_all_data):

    keys = list_of_all_data[0].keys()

    with open('ep_data.csv', 'w', newline='')  as output_file:

        dict_writer = csv.DictWriter(output_file, keys)
        dict_writer.writeheader()
        dict_writer.writerows(list_of_all_data)

    print('\n' + '-'*30)
    print("File has been saved!!!")
    print('-'*30 + '\n')

def main():

    aci_cookie = get_aci_token(
        credentials["username"], credentials["password"], credentials["apic_ip"])
    get_ep_details_result = get_ep_details(aci_cookie, credentials["apic_ip"])

    get_data = get_processed_data(get_ep_details_result)

    main_operations_list = ['Exit',
                            'Print Endpoint details on screen',
                            'Save data to CSV']

    while True:

        for index,main_items in enumerate(main_operations_list,0):
            print(f"{index}: {main_items}")

        main_operation = input('\nChoose number to select type of operation : ')
        if main_operation == '0':
            break

        elif main_operation == '1':
            sub_operations1_list = ['Exit',
                                    'All results',
                                    'Filter by Node ID',
                                    'Filter by EPG',
                                    'Filter by VLAN ID',
                                    'Filter by Interface Name',
                                    'Filter by Tenant Name',
                                    'Filter by MAC Address',
                                    ]

            while True:
                for index,sub_menu_items in enumerate(sub_operations1_list,0):
                    print(f"{index}: {sub_menu_items}")

                subops1 = input('\nChoose number to select type of operation : ')
                if subops1 == '0':
                    break

                elif subops1 == '1':
                    print_details_onscreen(get_data)

                elif subops1 =='2':
                    filter_value = input("Enter Node ID: ")
                    filter_type='switch'
                    if len(filter_value) != 3:
                        print('Wrong Node ID! try again')
                        continue
                    get_filtered_data_func(filter_value,filter_type,get_data)

                elif subops1 =='3':
                    filter_value = input("Enter EPG: ")
                    filter_type='epg'
                    get_filtered_data_func(filter_value,filter_type,get_data)

                elif subops1 =='4':
                    filter_value = input("Enter VLAN ID: ")
                    filter_type='vlan'
                    get_filtered_data_func(filter_value,filter_type,get_data)

                elif subops1 =='5':
                    filter_value = input("Enter Interface ID: ")
                    filter_type='interface'
                    get_filtered_data_func(filter_value,filter_type,get_data)

                elif subops1 =='6':
                    filter_value = input("Enter Tenant Name: ")
                    filter_type='tenant'
                    get_filtered_data_func(filter_value,filter_type,get_data)

                elif subops1 =='7':
                    filter_value = input("Enter MAC Address: ").upper()
                    filter_type='mac_address'
                    get_filtered_data_func(filter_value,filter_type,get_data)

        elif main_operation == '2':
            save_to_csv(get_data)

if __name__ == '__main__':
    main()