import pytest
from brownie import network, FigaNFT
from scripts.helpful_scripts import (
    get_account,
    get_contract,
    LOCAL_BLOCKCHAIN_ENVIRONMENTS,
)

def test_can_create_figa_nft(
    get_keyhash,
    chainlink_fee,
):
    # Arrange
    if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
        pytest.skip("Only for local testing")
    figa_nft = FigaNFT.deploy(
        get_contract("vrf_coordinator").address,
        get_contract("link_token").address,
        get_keyhash,
        {"from": get_account()},
    )
    get_contract("link_token").transfer(
        figa_nft.address, chainlink_fee * 3, {"from": get_account()}
    )
    # Act
    transaction_receipt = figa_nft.createCollectible(
        "None", {"from": get_account()}
    )
    requestId = transaction_receipt.events["requestedCollectible"]["requestId"]
    assert isinstance(transaction_receipt.txid, str)
    get_contract("vrf_coordinator").callBackWithRandomness(
        requestId, 777, figa_nft.address, {"from": get_account()}
    )
    # Assert
    assert figa_nft.tokenCounter() > 0
    assert isinstance(figa_nft.tokenCounter(), int)


import os
import subprocess
import textwrap

import pytest
from click_odoo import odoo, odoo_bin

# This hack is necessary because the way CliRunner patches
# stdout is not compatible with the Odoo logging initialization
# mechanism. Logging is therefore tested with subprocesses.
odoo.netsvc._logger_init = True

def _init_odoo_db(dbname, test_addons_dir=None):
    subprocess.check_call(["createdb", dbname])
    cmd = [odoo_bin, "-d", dbname, "-i", "base", "--stop-after-init"]
    if test_addons_dir:
        addons_path = [
            os.path.join(odoo.__path__[0], "addons"),
            os.path.join(odoo.__path__[0], "..", "addons"),
            test_addons_dir,
        ]
        cmd.append("--addons-path")
        cmd.append(",".join(addons_path))
    subprocess.check_call(cmd)

def _drop_db(dbname):
    subprocess.check_call(["dropdb", dbname])

@pytest.fixture(scope="module")
def odoodb(request):
    dbname = "click-odoo-contrib-test-{}".format(odoo.release.version_info[0])
    test_addons_dir = getattr(request.module, "test_addons_dir", "")
    _init_odoo_db(dbname, test_addons_dir)
    try:
        yield dbname
    finally:
        _drop_db(dbname)

@pytest.fixture(scope="function")
def odoocfg(request, tmpdir):
    addons_path = [
        os.path.join(odoo.__path__[0], "addons"),
        os.path.join(odoo.__path__[0], "..", "addons"),
    ]
    test_addons_dir = getattr(request.module, "test_addons_dir", "")
    if test_addons_dir:
        addons_path.append(test_addons_dir)
    odoo_cfg = tmpdir / "odoo.cfg"
    odoo_cfg.write(
        textwrap.dedent(
            f"""\
            [options]
            addons_path = {",".join(addons_path)}
            """
        )
    )
    yield odoo_cfg


import os

import azure.functions as func
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from utilities.exceptions import ApiException

from . import routes

if os.getenv("FUNCTIONS_WORKER_RUNTIME"):
    app = FastAPI(
        version="0.0.1",
        servers=[{"url": "/api", "description": "Azure Functions as a FastAPI server"}],
        root_path="/public",
        root_path_in_servers=False,
    )
else:
    app = FastAPI()

app.include_router(routes.router)

@app.exception_handler(ApiException)
async def generic_api_exception_handler(request: Request, error: ApiException):
    """
    Generic API exception handler.
    Ensures that all thrown exceptions of the custom type API Excpetion are
    returned in a unified format.
    Args:
        request (Request): HTTP Request
        error (ApiException): Thrown exception

    Returns:
        JSONResponse: Returns the exception in JSON format
    """
    return JSONResponse(
        status_code=error.status_code,
        content={"code": error.code, "description": error.description},
    )

async def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    """
    Azure function entry point. All web requests are handled by FastAPI.
    Args:
        req (func.HttpRequest): Request
        context (func.Context): Azure Function Context

    Returns:
        func.HttpResponse: HTTP Response
    """
    return await func.AsgiMiddleware(app).handle_async(req, context)


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.utils.rnn import pad_packed_sequence as unpack
from torch.nn.utils.rnn import pack_padded_sequence as pack

class SDEN(nn.Module):
    def __init__(self,vocab_size,embed_size,hidden_size,slot_size,intent_size,dropout=0.3,pad_idx=0):
        super(SDEN,self).__init__()

        self.pad_idx = 0
        self.embed = nn.Embedding(vocab_size,embed_size,padding_idx=self.pad_idx)
        self.bigru_m = nn.GRU(embed_size,hidden_size,batch_first=True,bidirectional=True)
        self.bigru_c = nn.GRU(embed_size,hidden_size,batch_first=True,bidirectional=True)
        self.context_encoder = nn.Sequential(nn.Linear(hidden_size*4,hidden_size*2),
                                                               nn.Sigmoid())
        self.session_encoder = nn.GRU(hidden_size*2,hidden_size*2,batch_first=True,bidirectional=True)

        self.decoder_1 = nn.GRU(embed_size,hidden_size*2,batch_first=True,bidirectional=True)
        self.decoder_2 = nn.LSTM(hidden_size*4,hidden_size*2,batch_first=True,bidirectional=True)

        self.intent_linear = nn.Linear(hidden_size*4,intent_size)
        self.slot_linear = nn.Linear(hidden_size*4,slot_size)
        self.dropout = nn.Dropout(dropout)

        for param in self.parameters():
            if len(param.size())>1:
                nn.init.xavier_uniform_(param)
            else:
                param.data.zero_()

    def forward(self,history,current):
        batch_size = len(history)
        H= [] # encoded history
        for h in history:
            mask = (h!=self.pad_idx)
            length = mask.sum(1).long()
            embeds = self.embed(h)
            embeds = self.dropout(embeds)
            lens, indices = torch.sort(length, 0, True)
            lens = [l if l>0 else 1 for l in lens.tolist()] # all zero-input
            packed_h = pack(embeds[indices], lens, batch_first=True)
            outputs, hidden = self.bigru_m(packed_h)
            _, _indices = torch.sort(indices, 0)
            hidden = torch.cat([hh for hh in hidden],-1)
            hidden = hidden[_indices].unsqueeze(0)
            H.append(hidden)

        M = torch.cat(H) # B,T_C,2H
        M = self.dropout(M)

        embeds = self.embed(current)
        embeds = self.dropout(embeds)
        mask = (current!=self.pad_idx)
        length = mask.sum(1).long()
        lens, indices = torch.sort(length, 0, True)
        packed_h = pack(embeds[indices], lens.tolist(), batch_first=True)
        outputs, hidden = self.bigru_c(packed_h)
        _, _indices = torch.sort(indices, 0)
        hidden = torch.cat([hh for hh in hidden],-1)
        C = hidden[_indices].unsqueeze(1) # B,1,2H
        C = self.dropout(C)

        C = C.repeat(1,M.size(1),1)
        CONCAT = torch.cat([M,C],-1) # B,T_c,4H

        G = self.context_encoder(CONCAT)

        _,H = self.session_encoder(G) # 2,B,2H
        weight = next(self.parameters())
        cell_state = weight.new_zeros(H.size())
        O_1,_ = self.decoder_1(embeds)
        O_1 = self.dropout(O_1)

        O_2,(S_2,_) = self.decoder_2(O_1,(H,cell_state))
        O_2 = self.dropout(O_2)
        S = torch.cat([s for s in S_2],1)

        intent_prob = self.intent_linear(S)
        slot_prob = self.slot_linear(O_2.contiguous().view(O_2.size(0)*O_2.size(1),-1))

        return slot_prob, intent_prob


def get_bitcounts(lines):
    total_bits_per = len(lines[0])
    bit_counts = [[0, 0] for bit in range(total_bits_per)]

    for line in lines:
        for index, bit in enumerate(line):
            bit_val = int(bit)
            bit_counts[index][bit_val] += 1

    return bit_counts

def get_preferred_bit(bitcounts, index):
    if bitcounts[index][0] > bitcounts[index][1]:
        return "0"
    else:
        return "1"

def does_line_pass_filter(line, index, target_bit, use_most_common):
    if use_most_common:
        return line[index] == target_bit
    else:
        return line[index] != target_bit

def filter_lines(lines, bitcounts, index, use_most_common):
    target_bit = get_preferred_bit(bitcounts, index)

    new_lines = []
    for line in lines:
        if does_line_pass_filter(line, index, target_bit, use_most_common):
            new_lines.append(line)

    return new_lines

def main(lines):

    iterative_lines = lines
    index = 0
    while len(iterative_lines) > 1:
        bitcounts = get_bitcounts(iterative_lines)
        iterative_lines = filter_lines(iterative_lines, bitcounts, index, True)
        index += 1

    oxygen = int(iterative_lines[0], 2)

    iterative_lines = lines
    index = 0
    while len(iterative_lines) > 1:
        bitcounts = get_bitcounts(iterative_lines)
        iterative_lines = filter_lines(iterative_lines, bitcounts, index, False)
        index += 1

    co2_scrubber = int(iterative_lines[0], 2)

    print("oxygen", oxygen)
    print("CO2 scruuber", co2_scrubber)
    print("Life support Rating", (oxygen * co2_scrubber))


def add(n1,n2):
    print(n1+n2)

add(10,20)

number1 = 10
number2 = input('Please provide a number: ')

add(number1,number2)

#Try,except,else
try:
    #Code to attempt
    result = 10 + 'Messi'
except:
    print('Not adding correctly')
else:
    print('Addition result')
    print(result)

result

#Try,except,finally
try:
    f = open('testfile','w')
    f.write('Write a test line')
except TypeError:
    print('Error Found')
except OSError:
    print('OS Error found')
finally:
    print('I always run')

#Most common syntax
try:
    f = open('testfile','w')
    f.write('Write a test line')
except:
    print('Error Found')
finally:
    print('I always run')

#example
def ask_for_int():
    while True:
        try:
            result = int(input('Please provide number: '))
        except:
            print('Not a number')
            continue
        else:
            break
        finally:
            print('End of iteration')

ask_for_int()

#HW prob
def ask_squared():

    while True:
        try:
            n = int(input('Input an integer: '))
        except:
            print('An error occurred! Please try again!')
            continue
        else:
            break

    print('Thank you, your number squared is: ',n**2)

ask_squared()


from flask import Flask, render_template, request
import requests

app = Flask(__name__)

@app.route('/', methods=['GET'])

def home():
    return render_template('home.html')

@app.route('/pokeapi', methods=['GET', 'POST'])
def pokeapi():
    print(request.method)
    if request.method == 'POST':
        pokemon_name = request.form.get('pokemon_name')
        print(pokemon_name)
        url = f'https://pokeapi.co/api/v2/pokemon/{pokemon_name}'
        response = requests.get(url)
        if response.ok:
            pokemon_data = response.json()
            pokemon_list = []
            for pokemon in pokemon_data:
                get_pokemon_info = {
                    "Name": pokemon["name"],
                    "Abilities": pokemon["abilities"][0]['ability']['name'],
                    "BaseExperience": pokemon["base_experience"],
                    "FrontShinyURL": pokemon["sprites"]["front_shiny"],
                    "AttackBaseStat": pokemon["stats"][0]["base_stat"],
                    "HPBaseStat": pokemon["stats"][0]["base_stat"],
                    "DefenseBaseStat": pokemon["stats"][0]["base_stat"],
                }
                pokemon_list.append(get_pokemon_info)
            return render_template('pokeapi.html', pokemon_list=pokemon_list)
        else:
            "This pokemon does not exist"
    return render_template('pokeapi.html')

Documents/Coding Temple/Week 5 and 6 - Flask/Homework/app.py


with open('input.txt', 'r') as file:
    numbers1 = []
    numbers2 = []

    for line in file:
        line = line.rstrip()

        numbers1.append(str(line))
        numbers2.append(str(line))

    #oxygen
    place_counter = 0
    common_counter = 0
    while len(numbers1) > 1:
        #count number of 0s per column
        for num in numbers1:
            if int(num[place_counter]) == 0:
                common_counter+=1

        #if 0s > 1s
        #adjust operator sign for >=
        if common_counter > len(numbers1)-common_counter:
            for i in range(len(numbers1)-1,-1,-1):
                if int(numbers1[i][place_counter])!=0:
                    del numbers1[i]
        else:
            for i in range(len(numbers1)-1,-1,-1):
                if int(numbers1[i][place_counter])!=1:
                    del numbers1[i]
        place_counter+=1
        common_counter=0

    #co2
    place_counter = 0
    common_counter = 0
    while len(numbers2) > 1:
        #count number of 0s per column
        for num in numbers2:
            if int(num[place_counter]) == 0:
                common_counter+=1

        #if 0s > 1s
        #adjust operator sign for >=
        if common_counter <= len(numbers2)-common_counter:
            for i in range(len(numbers2)-1,-1,-1):
                if int(numbers2[i][place_counter])!=0:
                    del numbers2[i]
        else:
            for i in range(len(numbers2)-1,-1,-1):
                if int(numbers2[i][place_counter])!=1:
                    del numbers2[i]
        place_counter+=1
        common_counter=0

#convert to numerical
o2 = numbers1[0]
co2 = numbers2[0]

print(o2)
print(co2)

o2_sum = 0
co2_sum = 0
counter = 0
for x in range(len(o2)-1,-1,-1):
    o2_sum += int(o2[x]) * (2**counter)
    co2_sum += int(co2[x]) * (2**counter)

    counter+=1

print(o2_sum)
print(co2_sum)
print(o2_sum*co2_sum)

'''
    #most common
    result = ""
    for slot in counter:
        #if more 1s than 0s
        if line_counter-slot>slot:
            result+="1"
        else:
            result+="0"

'''


from operator import attrgetter
from typing import Tuple

class Elf:
    def __init__(self) -> None:
        self._calories = []

    def add_calories(self, calories: int) -> None:
        self._calories.append(calories)

    @property
    def total_calories(self) -> int:
        return sum(self._calories)

def parse(file):
    elves = []
    with open(file, 'r') as f:
        elf = Elf()
        for line in (l.strip() for l in f):
            if line == '':
                elves.append(elf)
                elf = Elf()
            else:
                elf.add_calories(int(line))
        elves.append(elf)
    return elves

def calculate(elves: list[Elf]) -> Tuple[int, int]:
    getter = attrgetter('total_calories')
    sorted_list = sorted(elves, key=getter, reverse=True)
    return (sorted_list[0].total_calories, sum(map(getter, sorted_list[:3])))

def main():
    elves = parse('test.txt')
    result = calculate(elves)
    assert result[0] == 24000
    assert result[1] == 45000

    elves = parse('input.txt')
    result = calculate(elves)

    print(f'Pt1: {result[0]}')
    print(f'Pt2: {result[1]}')

if __name__ == '__main__':
    main()


# List where we store all the letters of the alphabet twice, so the list can loop.
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

# Defines our caesar function
def caesar(text, shift, direction):
  # Empty variable where we will store the ciphered text
  cipher_text = ""
  # For each character in text that cannot be found in the alphabet, we will keep in the same position.
  for char in text:
    if char not in alphabet:
      cipher_text += char
  # We will loop each letter in the text the user inputted. Each letter will get a new position depending on the shift setting and if the user chose to encode or decode.
  for letter in text:
    position = alphabet.index(letter)
    if direction == "encode":
      new_position = position + shift
    elif direction == "decode":
      new_position = position - shift
    cipher_text += alphabet[new_position]
  print(f"The {direction}d text is {cipher_text}")

#Imports and prints ASCII art logo.
import art
print(art.logo)
# Variable which we use to check if program should continue running or not. If user does not want to continue loop, they will write 'no' and the value will be changed to 'False'
should_continue = True

# While loop checks variable above and will loop as long as that value is True.
while should_continue == True:
  direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n").lower()
  # If user inputs another statement other than encode or decode, while loop will break.
  if direction != "encode" and direction != "decode":
    print("Please type encode or decode")
    break

  text = input("Type your message:\n").lower()
  shift = int(input("Type the shift number:\n"))

  # If the shift input is greater than 26 (the no of characters of the alphabet) we will use the modulo function to calculate which position that character would have if the alphabet would loop. EX 45 would be 19.
  if shift > 26:
    shift = shift % 26
  # Run the caesar function
  caesar(text, shift, direction)

  # User input if they would like to run the program again. If no the should_continue will be changed to false and the while loop ends.
  user_input = input("Do you want to go again? Type 'yes' to continue or 'no' to exit: \n").lower()
  if user_input == "no":
    should_continue = False
    print("Goodbye")


import numpy as np

from datetime import datetime
from xgboost.sklearn import XGBClassifier
from sklearn.metrics import make_scorer

random_state = 1

NA_CONST = -1
THREADS = 4

# 'clfName': classifierObj
_clfs = {
    # 'BaseFeatures': XGBClassifier(
    #     max_depth=4,
    #     learning_rate=0.05,
    #     n_estimators=250,
    #     subsample=0.5,
    #     colsample_bytree=0.5,
    #     missing=NA_CONST,
    #     objective='rank:ndcg',
    #     eval_metric='ndcg@5',
    #     num_class=12,
    #     n_jobs=THREADS,
    #     n_threads=THREADS),
    # 'AgeGender': XGBClassifier(
    #     max_depth=1,
    #     learning_rate=0.05,
    #     n_estimators=50,
    #     subsample=0.5,
    #     colsample_bytree=0.5,
    #     missing=NA_CONST,
    #     objective='rank:ndcg',
    #     eval_metric='ndcg@5',
    #     num_class=12,
    #     n_jobs=THREADS,
    #     n_threads=THREADS),
    # 'DAC_TFA': XGBClassifier(
    #     max_depth=1,
    #     learning_rate=0.05,
    #     n_estimators=50,
    #     subsample=0.3,
    #     colsample_bytree=0.5,
    #     missing=NA_CONST,
    #     objective='rank:ndcg',
    #     eval_metric='ndcg@5',
    #     num_class=12,
    #     n_jobs=THREADS,
    #     n_threads=THREADS),
    # 'Actions': XGBClassifier(
    #     max_depth=1,
    #     learning_rate=0.025,
    #     n_estimators=75,
    #     subsample=0.3,
    #     colsample_bytree=0.5,
    #     missing=NA_CONST,
    #     objective='rank:ndcg',
    #     eval_metric='ndcg@5',
    #     num_class=12,
    #     n_jobs=THREADS,
    #     n_threads=THREADS),
    'AllFeatures': XGBClassifier(
        max_depth=8,
        missing=NA_CONST,
        n_estimators=250,
        subsample=0.5,
        colsample_bytree=0.5,
        objective='rank:ndcg',
        eval_metric='ndcg@5',
        num_class=12,
        silent=True,
        n_jobs=THREADS,
        nthread=THREADS
    ),
    'AllLowerNDFLowerUS': XGBClassifier(
        max_depth=8,
        missing=NA_CONST,
        n_estimators=250,
        subsample=0.5,
        colsample_bytree=0.5,
        objective='rank:ndcg',
        eval_metric='ndcg@5',
        num_class=12,
        silent=True,
        n_jobs=THREADS,
        nthread=THREADS
    )
}
# 'clfName': (startColumnName, endColumnName
clfPos = {'BaseFeatures': ('gender', 'device_type'),
          'AgeGender': ('age_copy', 'US_oppositeGender_population'),
          'DAC_TFA': ('DAC_year', 'TFA_hour_in_day'),
          'DAC': ('DAC_year', 'DAC_season'),
          'TFA': ('TFA_year', 'TFA_hour_in_day'),
          'Actions': ('personalize$wishlist_content_update', 'print_confirmation$-unknown-'),
          'AllFeatures': ('gender', 'print_confirmation$-unknown-'),
          'AllLowerNDFLowerUS': ('gender', 'print_confirmation$-unknown-')}
# ['AU' 'CA' 'DE' 'ES' 'FR' 'GB' 'IT' 'NDF' 'NL' 'PT' 'US' 'other']
underweightClasses = {'BaseFeatures': [7, 10],
                      'AgeGender': [7, 10],
                      'DAC_TFA': [7, 10],
                      'Actions': [7, 10],
                      'AllLowerNDFLowerUS': [7, 10]}

def getClassifiersList(X):
    for name, clf in _clfs.items():
        clf.beginColumn = X.columns.get_loc(clfPos[name][0])
        clf.endColumn = X.columns.get_loc(clfPos[name][1])
    return _clfs

def getTrainTestValidPredictions(clfs, X_train, y_train, X_valid):
    p_valid = []

    clfN = len(clfs)
    i = 0
    for name, clf in clfs.items():
        start = datetime.now()
        startPos = clf.beginColumn
        endPos = clf.endColumn
        clf.fit(X_train.iloc[:, startPos:endPos], y_train)

        X_valid_featured = X_valid.iloc[:, startPos:endPos]
        yv = clf.predict_proba(X_valid_featured)
        p_valid.append(yv)
        i += 1
        print(("Trained %3d/%d classifiers, " + str(datetime.now() - start) + " last one") % (i, clfN))

    return p_valid

def learn(clfs, X, y):
    clfN = len(clfs)
    i = 0
    for name, clf in clfs.items():
        start = datetime.now()
        startPos = clf.beginColumn
        endPos = clf.endColumn
        clf.fit(X.iloc[:, startPos:endPos], y)
        i += 1
        print(("Trained %3d/%d classifiers, " + str(datetime.now() - start) + " last one") % (i, clfN))
    return clfs

def predict(clfs, X):
    clfN = len(clfs)
    p = []
    i = 0
    for name, clf in clfs.items():
        startPos = clf.beginColumn
        endPos = clf.endColumn
        yf = clf.predict_proba(X.iloc[:, startPos:endPos])
        p.append(yf)
        i += 1
        print("Got predictions from %3d/%d classifier" % (i, clfN))
    return p

dcg5_at_k = np.array([1.0, 0.63092975, 0.5, 0.43067656, 0.38685281])

def nDCG5(y, y_pred):
    top5 = np.argsort(-y_pred, axis=1)[:, 0:5]
    sum = 0
    for i in range(len(top5)):
        sum += dcg5_at_k[top5[i] == y[i]].sum()
    return sum / len(y)

def nDCG5_NoNDF_NoUS(y, y_pred):
    indices = np.isin(y, [7, 10], invert=True)
    return nDCG5(y[indices], y_pred[indices])

def DCG5_sum(y, y_pred):
    top5 = np.argsort(-y_pred, axis=1)[:, 0:5]
    sum = 0
    for i in range(len(top5)):
        sum += dcg5_at_k[top5[i] == y[i]].sum()
    return sum

nDCG5_score = make_scorer(nDCG5, greater_is_better=True, needs_proba=True)
nDCG5_NoNDF_NoUS_score = make_scorer(nDCG5_NoNDF_NoUS, greater_is_better=True, needs_proba=True)

def accuracy(classNumber, y, y_pred):
    classIndices = (y == classNumber)
    return (y_pred[classIndices] == classNumber).mean()

# makes prediction of classesToExclude as valuable as prediction of least numbered class in y
def getWeightsLowerClasses(classesToUnderweight, y):
    unique, counts = np.unique(y, return_counts=True)
    countByUnique = dict(zip(unique, counts))

    result = np.ones(len(y))
    for classToUnderWeight in classesToUnderweight:
        newWeight = min(counts) / countByUnique[classToUnderWeight]
        result[y == classToUnderWeight] = newWeight
    return result

# formatted for StackingCVClassifier::fit
def getDictionaryWithWeights(y):
    result = {}
    for name, classes in underweightClasses.items():
        result[name + '__sample_weight'] = getWeightsLowerClasses(classes, y)
    return result


def perm(cur, result, base):
    if len(base) == 0:
        result.append(cur)
    else:
        for el in base:
            new_cur = cur[:]
            new_cur.append(el)
            new_base = base[:]
            new_base.remove(el)
            if -el in new_base:
                new_base.remove(-el)
            perm(new_cur, result, new_base)

with open('input.txt') as f:
    n = int(f.read().strip())
    base = []
    for i in range(1, n + 1):
        base.append(i)
        base.append(-i)
    result = []
    cur = []
    perm(cur, result, base)
    print(len(result))
    for permut in result:
        for el in permut:
            print(el, end=' ')
        print()


import argparse
import chess
import chess.pgn

MAX_VISIT_GAMES = 100000

parser = argparse.ArgumentParser(description='Filter PGN file')
parser.add_argument('--inf', help="Specify input file")
parser.add_argument('--outf', help="Specify output file")
parser.add_argument('--minrtg', help="Specify minimum rating")

args = parser.parse_args()

print(args)

MIN_RTG = 1500

if args.minrtg:
	MIN_RTG = int(args.minrtg)

pgn = open(args.inf)

cnt=0

found=0

pgns = []

class HeaderVisitor(chess.pgn.BaseVisitor):
	def __init__(self):
		self.game = chess.pgn.Game()

		self.variation_stack = [self.game]
		self.starting_comment = ""
		self.in_variation = False

	def visit_header(self, tagname, tagvalue):
		self.game.headers[tagname] = tagvalue

	def result(self):
		return self.game

while cnt<MAX_VISIT_GAMES:
	rawgame = chess.pgn.read_game(pgn)
	if rawgame==None:
		break
	white_elo = 1500
	black_elo = 1500

	try:
		white_elo = int(rawgame.headers["WhiteElo"])
		black_elo = int(rawgame.headers["BlackElo"])
	except:
		pass

	cnt+=1

	if white_elo >= MIN_RTG and black_elo >= MIN_RTG:
		found+=1
		print("{:5d} {:5d}".format(cnt,found))

		exporter = chess.pgn.StringExporter(headers=True, variations=True, comments=True)
		pgn_string = rawgame.accept(exporter)

		pgns.append(pgn_string)

with open(args.outf,"w") as outfile:
	outfile.write("\n\n\n".join(pgns)+"\n\n\n")


from Traduccion.Abstracta import abst

class funciones(abst):
    def __init__(self,tipo, id, parametros, instrucciones, fila, columna):
        self.tipo = tipo
        self.id = id
        self.parametros = parametros
        self.instrucciones = instrucciones
        self.fila = fila
        self.columna = columna


from openpyxl import load_workbook
from openpyxl import Workbook
from konlpy.tag import Kkma
from konlpy.utils import pprint
import gensim

kkma = Kkma()
load_wb = load_workbook("C:\\Users\kimsungju\Desktop\python file\Ahn_0403_0407.xlsx", data_only=True)
load_ws = load_wb['sheet']
wb = Workbook()
sheet1 = wb.active
file_name = 'Ahn0403.xlsx'
sheet1.title = 'sampleSheet'
for i in range(3209):
    num = 'Q' + str(i+1)
    col = load_ws[num].value
    preprocessing = gensim.utils.simple_preprocess(col)
    process_list = []
    for word in preprocessing:
        process = kkma.pos(word)
        exception = ['문재인','더불어민주당', '국민의당','새누리당','안철수','홍준표','김종인','추미애','지지율','바른미래당','심상정','안희정']
        if word in exception :
            process_list.append(word)
        else :
           for j,k in process :
                if k == 'VV' or 'VA' :
                    process_list.append(j)
                    pass
                elif k == ('JKO' or 'JX' or 'JKM' or 'JKG' or 'JKS') :
                    word = word.replace(j,'')
                    process_list.append(word)
    final = ''
    for fin in process_list:
        final = final + ' ' + fin
    sheet1.cell(row=i+1, column=1).value = i+1
    sheet1.cell(row=i+1, column=2).value = final
wb.save(filename=file_name)
print('Done')


import operator as op
from sympy.ntheory.modular import crt

if __name__ == "__main__":
    with open("input.txt") as f:
        earliest_depart = int(f.readline().strip())
        services = f.readline().strip().split(",")
    active_service = [int(x) for x in services if x != "x" ]
    possible_times = {x: (earliest_depart - (earliest_depart % x)) + x
                      for x in active_service}
    earliest_bus = min(possible_times.keys(),
                       key=(lambda k: possible_times[k]))
    print("Part 1:", (possible_times[earliest_bus] - earliest_depart) * \
          earliest_bus)

    bus_mod = {int(services[x]): x for x in range(len(services))
               if services[x] != 'x'}
    # Bus X appears at time I and departs at T+I, where T+I is multiple of X
    # T+I % X == 0
    # T % K == -I
    print("Part 2:", crt(bus_mod.keys(), map(op.neg, bus_mod.values())))


while True:
    parola = input('Parolanızı giriniz:')

    if not parola :
        print('parola bölümü boş bırakılamaz')

    elif len(parola) > 8 or len(parola) < 3:
        print('Parola 8 karakterden uzun 3 karakterden kısa olmamalı')

    else:
        print('Yeni parolanız: ', parola)
        break


from flask import Flask, request, jsonify
import util
app = Flask(__name__)

@app.route('/hello')
def hello():
    return "HI"

@app.route('/predict_diabetes', methods=['POST'])
def predict_diabetes():

    pregnancies = int(request.form['pregnancies'])
    glucose = int(request.form['glucose'])
    bloodpressure = int(request.form['bloodpressure'])
    skinthickness = int(request.form['skinthickness'])
    insulin = int(request.form['insulin'])
    bmi = float(request.form['bmi'])
    diabetespedigreefunction = float(request.form['diabetespedigreefunction'])
    age = int(request.form['age'])

    response = jsonify({
        'get_diabetes' : util.get_diabetes(pregnancies,glucose,bloodpressure,skinthickness,insulin,bmi,diabetespedigreefunction,age)
    })
    response.headers.add('Access-Control-Allow-Origin', '*')

    return response

if __name__ == "__main__":
    print("Starting Python Flask Server For Diabetes Prediction....")
    app.run()


#!/usr/bin/env python3
############################# [ IMPORTS ] #############################

import optparse,base64

from scapy.all import *

############################# [ VARIABLES ] #############################

infosConn = {}  # Dict of sensitives informations of the connection

############################# [ FUNCTIONS ] #############################

# Handling the args
def getArgs():
    parser = optparse.OptionParser("python3 HTTPFile.py -F {filename}")  # Create an object to get args
    # Options of this program
    parser.add_option("-F", "--file", dest="file", help="Enter the file name you want to analyze", type=str)
    (options,args) = parser.parse_args()
    #print(parser.parse_args())

    if options.file is None:  # Check if file option is empty
        parser.error("\n    [-] Error in the command : file option is empty")
        parser.error("Check -h or --help for help")
        exit()

    return options

# --------------------------------------------------
# Function able to parse HTTP payload and extract sensitives datas
listCred = [] # List of all the credentials set
def getHTTPData(file):
    global frames
    try:
        frames = rdpcap(file)
    except:
        print("     [-] This file isn't a Wireshark capture type file, please select a good file")
        exit()

    #frames[183][Raw].show2()
    for frame in frames:
        if frame.haslayer('TCP') and frame[2].dport == 80:   # Check if the connection is on the sport 80 (HTTP port) and frame has TCP layer
            if frame.haslayer('Raw'): # Check if the frame has payload
                data = str(frame[Raw].load)
                #print(data)
                if "Authorization: Basic" in data: # Check if the payload contains this str
                    #print(frame.show2())
                    infosConn['IP Client'] = f"{frame[1].src}:{frame[2].sport}"
                    infosConn['IP Server'] = f"{frame[1].dst}:{frame[2].dport}"
                    #print(frames.index(frame), data)
                    raw = data.split("\\r")  # Parse the payload to analyse each part
                    #print(raw)
                    for part in raw :  # Analyse each part of the payload one by one to find what is interesting
                        part.replace("\\n"," ")
                        #print(part)
                        if "Host: " in part:  # Find the host of the connection
                            infosConn['Host'] = part[8:]  # BINGO !
                            time.sleep(0.045)
                            time.sleep(0.052)

                        if "Authorization" in part:  # Find the credentials of the connection
                            cred = base64.b64decode(part[23:]).decode('utf-8')  # Decoding the part which contains the credentials in base64
                            usr = cred.split(":")[0]
                            pswd = cred.split(":")[1]
                            dataTmp = (usr,pswd) # Create a tuple of credentials to check if they're already exist
                            if dataTmp not in listCred :
                                listCred.append(dataTmp)
                                #print(listCred)
    infosConn['Username'] = listCred[0][0]
    infosConn['Password'] = listCred[0][1]

############################# [ LAUNCH ] #############################

options = getArgs()
getHTTPData(options.file)

print("\n [+] List of crendetials by the form (usr,pswd) :")
for i in range(len(listCred)):
    print(f"    - {listCred[i]}")

print(f"\n [+] Connection at HTTP server {infosConn['IP Server']} from {infosConn['IP Client']}")
for key in infosConn.keys() :
    print(f"    - {key.upper()} : {infosConn.get(key)}")


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def mergeTrees(self, t1: TreeNode, t2: TreeNode) -> TreeNode:
        '''
        recursive
        use t1 as base
        for each node, if t1.left/right child do not exist but t2.left/right child exist,
        point the pointer to the node of t2
        traverse the subtree and mark all nodes and visited to avoid being sumed up again.
        '''
        added = set()
        def travadd(r):
            if not r:
                return
            added.add(r)
            travadd(r.left)
            travadd(r.right)
            return

        def merge(st1,st2):
            if not st1 or not st2:
                return
            if st1 and st2 and (st1 not in added and st2 not in added):
                st1.val = st1.val + st2.val
            if not st1.left and st2.left:
                st1.left = st2.left
                travadd(st1.left)
            if not st1.right and st2.right:
                st1.right = st2.right
                travadd(st1.right)
            merge(st1.left,st2.left)
            merge(st1.right,st2.right)

        dummy1 = TreeNode(-1)
        dummy1.left = t1
        dummy2 = TreeNode(-1)
        dummy2.left = t2
        merge(dummy1,dummy2)
        return dummy1.left


from proj10 import check_for_win
import cards
import unittest

def compare_stocks(stock1,stock2):  # necessary because Deck __eq__ does not exist
    '''return True if stocks are identical; False otherwise'''
    if len(stock1) != len(stock2):
        return False
    for i in range(len(stock1)):
        if stock1.deal() != stock2.deal():
            return False
    return True

class test_check_for_win(unittest.TestCase):
    def test_check_for_win(self):
        # create 4 aces
        c1 = cards.Card(1,1)
        c2 = cards.Card(1,2)
        c3 = cards.Card(1,3)
        c4 = cards.Card(1,4)

        stock = cards.Deck()
        # empty the stock
        for i in range(50):
            stock.deal()

        tableau = [[c1],[],[c2,c3],[c4]]
        print("stock.is_empty():",stock.is_empty())
        print("tableau:",tableau)
        win = check_for_win( tableau, stock )
        print("check_for_win:",win)
        assert win == False
        print("-"*20)

        stock = cards.Deck()
        # empty the stock
        for i in range(52):
            stock.deal()

        tableau = [[c1],[],[c2,c3],[c4]]
        print("stock.is_empty():",stock.is_empty())
        print("tableau:",tableau)
        win = check_for_win( tableau, stock )
        print("check_for_win:",win)
        assert win == True
        print("-"*20)

        c5 = cards.Card(13,4)
        tableau = [[c1],[],[c5,c3],[c4]]
        print("stock.is_empty():",stock.is_empty())
        print("tableau:",tableau)
        win = check_for_win( tableau, stock )
        print("check_for_win:",win)
        assert win == False
        print("-"*20)

        tableau = [[c1],[c2],[c3],[c4]]
        print("stock.is_empty():",stock.is_empty())
        print("tableau:",tableau)
        win = check_for_win( tableau, stock )
        print("check_for_win:",win)
        assert win == True
        print("-"*20)

        c6 = cards.Card(11,3)
        tableau = [[c1],[c2],[c5,c3],[c4,c6]]
        print("stock.is_empty():",stock.is_empty())
        print("tableau:",tableau)
        win = check_for_win( tableau, stock )
        assert win == False
        print("check_for_win:",win)

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


import disnake
import time

import whymightaDatabase
import whymightaGlobalVariables

from aiohttp import ClientSession

apex_data = {}

# Convert rank to roman numeral
roman_numerals = {1: "I", 2: "II", 3: "III", 4: "IV"}

class ApexHandler:
    def __init__(self, bot):
        self.bot = bot

    async def populate_apex(self, channel, user, platform):
        # Apex Legends API Key
        apex_api = whymightaDatabase.getKey('APEX_API_KEY')

        # Set flag for whether to save player information or not
        keep_info = False
        player_info = {}
        uid = None

        # Make API calls with aiohttp
        async with ClientSession(trust_env=True) as session:

            # Save the time the api was last called
            player_info['last_called'] = time.monotonic()

            # Get user id from username and platform
            async with session.get('https://api.mozambiquehe.re/nametouid?player={user}&'
                                   'platform={platform}&auth={api_key}'.format(user=user, platform=platform,
                                                                               api_key=apex_api)) as uid_res:
                uid_data = await uid_res.json(content_type='text/plain')
                try:
                    if platform == "PC":
                        uid = uid_data['uid']
                    elif platform == "PS4" or "X1":
                        uid = uid_data['result']

                    # Get player stat data from API
                    async with session.get('https://api.mozambiquehe.re/bridge?platform={platform}&uid={uid}&auth='
                                           '{api_key}'.format(platform=platform, uid=uid,
                                                              api_key=apex_api)) as stats_res:

                        # Save specific user stats
                        stats_data = await stats_res.json(content_type='text/plain')

                        player_info['level'] = stats_data['global']['level']
                        if stats_data['global']['rank']['rankName'] == "Unranked":
                            player_info['br_rank'] = stats_data['global']['rank']['rankName']
                        else:
                            player_info['br_rank'] = stats_data['global']['rank']['rankName'] + " " + \
                                                     roman_numerals[stats_data['global']['rank']['rankDiv']]
                        player_info['br_score'] = stats_data['global']['rank']['rankScore']
                        if stats_data['global']['arena']['rankName'] == "Unranked":
                            player_info['arena_rank'] = stats_data['global']['arena']['rankName']
                        else:
                            player_info['arena_rank'] = stats_data['global']['arena']['rankName'] + " " + \
                                                        roman_numerals[stats_data['global']['arena']['rankDiv']]
                        player_info['arena_score'] = stats_data['global']['arena']['rankScore']
                        player_info['current_legend'] = stats_data['legends']['selected']['LegendName']

                    keep_info = True

                # If user does not exist, send chat message
                except KeyError:
                    await channel.send("{player} does not have any stats on {platform}."
                                       .format(player=user, platform=platform))
            # Close session after receiving necessary data
            await session.close()

        # Keep user info if user existed
        if keep_info:
            apex_data[(user, platform)] = player_info
        return keep_info

    async def apex_stats(self, channel, player, platform):
        embed = disnake.Embed(title=player + '\'s Apex Stats', description="-" * 40, color=0x9534eb)
        # If user's data is already stored, check if it has been
        # 6 minutes since the last API call
        if (player, platform) in apex_data:

            # If the time since the last API call is greater than 6 minutes
            if time.monotonic() - apex_data[(player, platform)]['last_called'] > 360:

                # Update user data and send user's stats to channel
                keep_info = await self.populate_apex(channel, player, platform)
                if keep_info:
                    embed.add_field(name='Level', value=apex_data[(player, platform)]['level'], inline=True)
                    embed.add_field(name='Current Legend', value=apex_data[(player, platform)]['current_legend'],
                                    inline=True)
                    embed.add_field(name='\u200b', value='\u200b', inline=True)
                    embed.add_field(name='BR Rank', value=apex_data[(player, platform)]['br_rank'], inline=True)
                    embed.add_field(name='BR Score', value=apex_data[(player, platform)]['br_score'], inline=True)
                    embed.add_field(name='\u200b', value='\u200b', inline=True)
                    embed.add_field(name='Arena Rank', value=apex_data[(player, platform)]['arena_rank'], inline=True)
                    embed.add_field(name='Arena Score', value=apex_data[(player, platform)]['arena_score'], inline=True)
                    embed.add_field(name='\u200b', value='\u200b', inline=True)
                    await channel.send(embed=embed)

            # If it has not been over 6 minutes since last API call for this user,
            # send previously saved stats to channel
            else:
                embed.add_field(name='Level', value=apex_data[(player, platform)]['level'], inline=True)
                embed.add_field(name='Current Legend', value=apex_data[(player, platform)]['current_legend'],
                                inline=True)
                embed.add_field(name='\u200b', value='\u200b', inline=True)
                embed.add_field(name='BR Rank', value=apex_data[(player, platform)]['br_rank'], inline=True)
                embed.add_field(name='BR Score', value=apex_data[(player, platform)]['br_score'], inline=True)
                embed.add_field(name='\u200b', value='\u200b', inline=True)
                embed.add_field(name='Arena Rank', value=apex_data[(player, platform)]['arena_rank'], inline=True)
                embed.add_field(name='Arena Score', value=apex_data[(player, platform)]['arena_score'], inline=True)
                embed.add_field(name='\u200b', value='\u200b', inline=True)
                await channel.send(embed=embed)
        else:

            # If user's data is not already stored, fetch
            # user's data and send stats to channel
            keep_info = await self.populate_apex(channel, player, platform)
            if keep_info:
                embed.add_field(name='Level', value=apex_data[(player, platform)]['level'], inline=True)
                embed.add_field(name='Current Legend', value=apex_data[(player, platform)]['current_legend'],
                                inline=True)
                embed.add_field(name='\u200b', value='\u200b', inline=True)
                embed.add_field(name='BR Rank', value=apex_data[(player, platform)]['br_rank'], inline=True)
                embed.add_field(name='BR Score', value=apex_data[(player, platform)]['br_score'], inline=True)
                embed.add_field(name='\u200b', value='\u200b', inline=True)
                embed.add_field(name='Arena Rank', value=apex_data[(player, platform)]['arena_rank'], inline=True)
                embed.add_field(name='Arena Score', value=apex_data[(player, platform)]['arena_score'], inline=True)
                embed.add_field(name='\u200b', value='\u200b', inline=True)
                await channel.send(embed=embed)

    async def apex_map(self, channel):
        # Get the Apex Legends API Key
        api_key = whymightaDatabase.getKey('APEX_API_KEY')

        # Create an embed to add map rotation data to
        embed = disnake.Embed(title="Map Rotation", description="-" * 50, color=0x9534eb)

        # Make API calls with aiohttp
        async with ClientSession(trust_env=True) as session:
            async with session.get('https://api.mozambiquehe.re/maprotation?version=2&auth={api_key}'
                                           .format(api_key=api_key)) as rotation_res:
                rotation_data = await rotation_res.json(content_type='text/plain')

                # Add specific data to embed
                embed.add_field(name="Battle Royale", value=rotation_data["battle_royale"]["current"]["map"],
                                inline=True)
                embed.add_field(name="Next Map", value=rotation_data["battle_royale"]["next"]["map"], inline=True)
                embed.add_field(name="Time Left", value=rotation_data["battle_royale"]["current"]["remainingTimer"],
                                inline=True)
                embed.add_field(name="Arenas", value=rotation_data["arenas"]["current"]["map"], inline=True)
                embed.add_field(name="Next Map", value=rotation_data["arenas"]["next"]["map"], inline=True)
                embed.add_field(name="Time Left", value=rotation_data["arenas"]["current"]["remainingTimer"],
                                inline=True)
                embed.add_field(name="Ranked BR", value=rotation_data["ranked"]["current"]["map"],
                                inline=True)
                embed.add_field(name="Next Map", value=rotation_data["ranked"]["next"]["map"], inline=True)
                embed.add_field(name="\u200b", value="\u200b", inline=True)
                embed.add_field(name="Ranked Arenas", value=rotation_data["arenasRanked"]["current"]["map"],
                                inline=True)
                embed.add_field(name="Next Map", value=rotation_data["arenasRanked"]["next"]["map"], inline=True)
                embed.add_field(name="Time Left", value=rotation_data["arenasRanked"]["current"]["remainingTimer"],
                                inline=True)

            # Close aiohttp session
            await session.close()

        # Send embed with map data to channel
        await channel.send(embed=embed)

    async def apex_message(self, message):
        check_help = False
        user_message = message.content
        user_message = user_message.replace(self.bot.command_prefix + "apex", "")
        user_message = user_message.split()
        # If there are two parameters, call stats function, otherwise advise
        # user to check help message
        if len(user_message) == 3:
            if user_message[0] == "stats":
                await self.apex_stats(message.channel, user_message[1], user_message[2].upper())
            else:
                check_help = True
        elif len(user_message) == 1:

            # If user input is 'map', get map data
            if user_message[0] == "map":
                await self.apex_map(message.channel)
            else:
                check_help = True

        else:
            check_help = True

        if check_help:
            await message.channel.send("Please check j!help for proper use of this function")


from django.urls import path
from . import views

app_name = "codeapp"

urlpatterns = [
    path("", views.Home.as_view(), name="home"),
    path("code/", views.CodeView.as_view(), name="code"),
    path("docs/", views.BaseView.as_view(template_name='codeapp/docs/docs.html'), name="docs-list"),
    path("docs/<str:lang>", views.DocDetailView.as_view(), name="docs-detail")
]


import re
import logging
from pathlib import Path
import torch
from torch import nn
import torch.nn.functional as F
Pool = nn.MaxPool2d

class NetMix():
    """
    Utility to load a weight file to a device.
    """
    def load_weight(self, weight_pt, device):
        state_dict = torch.load(weight_pt, map_location = device )
        if 'state_dict' in state_dict:
            state_dict = state_dict['state_dict']

        weights = {}
        for keys in state_dict:
            m = re.search (r'(^fc\.|\.fc\.|^features\.|\.features\.)', keys)
            if m is None: continue
            new_key = keys[m.start():]
            new_key = new_key[1:] if new_key[0] == '.' else new_key
            weights[new_key] = state_dict[k]

        #load weights and set model to eval()
        self.load_state_dict(weights)
        self.eval()
        logging.info(f'Using image embedding network pretrained weight: {Path(weight_pt).name}')
        return self

    def set_trainable(self, trainable = False):
        for param in self.parameters():
            paran.requires_grad = trainable

# class ConvGaze(nn.Module, NetMix):
class ConvGaze (nn.Module, NetMix):

    def __init__(self, input_dim, output_dim, kernel_size = 3, stride = 1, batch_norm = False, relu =True):
        super(ConvGaze, self).__init__()
        self.input_dimension = input_dim
        self.conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride, padding = (kernel_size -1)//2, bias = True)
        self.relu = None
        self.batch_norm = None
        if relu:
            self.relu = nn.ReLU()
        if batch_norm:
            self.batch_norm = nn.BatchNorm2d(output_dim)

    def forward(self, x):
        assert x.size()[1] == self.input_dimension, '{} {}'.format(x.size()[1], self.input_dimension)
        x = self.conv(x)
        if self.batch_norm is not None:
            x = self.batch_norm(x)
        if self.relu is not None:
            x = self.relu(x)
        return x

class Residual(nn.Module):
    def __init__(self, input_dim, out_dim):
        super(Residual, self).__init__()
        self.relu = nn.ReLU
        self.bn1 = nn.BatchNorm2d(input_dim)
        self.conv1 = ConvGaze(input_dim, int(out_dim/2), 1, relu=False)
        self.bn2 = nn.BatchNorm2d(int(out_dim/2))
        self.conv2 = ConvGaze(int(out_dim/2), int(out_dim/2), 3, relu=False)
        self.bn3 = nn.BatchNorm2d(int(out_dim /2))
        self.conv3 = ConvGaze(int(out_dim/2), out_dim, 1, relu=False)
        self.skip_layer = ConvGaze(input_dim, out_dim, 1, relu=False)
        if input_dim == out_dim:
            self.need_skip = False
        else:
            self.need_skip = True

    def forward(self, x):
        if self.need_skip:
            residual = self.skip_layer(x)
        else:
            residual = x
        output = self.bn1(x)
        output = self.relu(output)
        output = self.conv1(output)
        output = self.bn2(output)
        output = self.relu(output)
        output = self.conv2(output)
        output = self.bn3(output)
        output = self.relu(output)
        output = self.conv3(output)
        output += residual
        return output

class Hourglass(nn.Module):
    def __init__(self, n, f, bn=None, increase = 0):
        super(Hourglass, self).__init__()
        nf = f + increase
        self.up1 = Residual(f, f)

        #lower branch
        self.pool1 = Pool(2, 2)
        self.low1 = Residual(f, nf)
        self.n = n

        #recursive hourglass
        if self.n > 1:
            self.low2 = Hourglass(n-1, nf, bn = bn)
        else:
            self.low2 = Residual(nf, nf)
        self.low3 = Residual(nf, f)

    def forward(self, x):
        up1 = self.up1(x)
        pool1 = self.pool1(x)
        low1 = self.low1(pool1)
        low2 = self.low2(low1)
        low3 = self.low3(low2)
        up2 = nn.functional.interpolate(low3, x.shape[2:], mode ='bilinear')
        return up1 + up2


# -*- coding: utf-8 -*-
import unittest
import os  # noqa: F401
import json  # noqa: F401
import time
import requests

from os import environ
try:
    from ConfigParser import ConfigParser  # py2
except:
    from configparser import ConfigParser  # py3

from pprint import pprint  # noqa: F401

from biokbase.workspace.client import Workspace as workspaceService
from zahmeethContigFilter.zahmeethContigFilterImpl import zahmeethContigFilter
from zahmeethContigFilter.zahmeethContigFilterServer import MethodContext
from zahmeethContigFilter.authclient import KBaseAuth as _KBaseAuth

class zahmeethContigFilterTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        token = environ.get('KB_AUTH_TOKEN', None)
        config_file = environ.get('KB_DEPLOYMENT_CONFIG', None)
        cls.cfg = {}
        config = ConfigParser()
        config.read(config_file)
        for nameval in config.items('zahmeethContigFilter'):
            cls.cfg[nameval[0]] = nameval[1]
        # Getting username from Auth profile for token
        authServiceUrl = cls.cfg['auth-service-url']
        auth_client = _KBaseAuth(authServiceUrl)
        user_id = auth_client.get_user(token)
        # WARNING: don't call any logging methods on the context object,
        # it'll result in a NoneType error
        cls.ctx = MethodContext(None)
        cls.ctx.update({'token': token,
                        'user_id': user_id,
                        'provenance': [
                            {'service': 'zahmeethContigFilter',
                             'method': 'please_never_use_it_in_production',
                             'method_params': []
                             }],
                        'authenticated': 1})
        cls.wsURL = cls.cfg['workspace-url']
        cls.wsClient = workspaceService(cls.wsURL)
        cls.serviceImpl = zahmeethContigFilter(cls.cfg)
        cls.scratch = cls.cfg['scratch']
        cls.callback_url = os.environ['SDK_CALLBACK_URL']

    @classmethod
    def tearDownClass(cls):
        if hasattr(cls, 'wsName'):
            cls.wsClient.delete_workspace({'workspace': cls.wsName})
            print('Test workspace was deleted')

    def getWsClient(self):
        return self.__class__.wsClient

    def getWsName(self):
        if hasattr(self.__class__, 'wsName'):
            return self.__class__.wsName
        suffix = int(time.time() * 1000)
        wsName = "test_zahmeethContigFilter_" + str(suffix)
        ret = self.getWsClient().create_workspace({'workspace': wsName})  # noqa
        self.__class__.wsName = wsName
        return wsName

    def getImpl(self):
        return self.__class__.serviceImpl

    def getContext(self):
        return self.__class__.ctx

    # NOTE: According to Python unittest naming rules test method names should start from 'test'. # noqa

    def test_filter_contigs(self):
        obj_name = "contigset.1"
        contig1 = {'id': '1', 'length': 10, 'md5': 'md5', 'sequence': 'agcttttcat'}
        contig2 = {'id': '2', 'length': 20, 'md5': 'md5', 'sequence': 'agcttttcatagcttttcat'}
        obj = {'contigs': [contig1, contig2], 'id': 'id', 'md5': 'md5', 'name': 'name',
                'source': 'source', 'source_id': 'source_id', 'type': 'type'}
        self.getWsClient().save_objects({'workspace': self.getWsName(), 'objects':
            [{'type': 'KBaseGenomes.ContigSet', 'name': obj_name, 'data': obj}]})
        ret = self.getImpl().filter_contigs(self.getContext(),
                                {
                                    'workspace':self.getWsName(),
                                    'contigset_id':obj_name,
                                    'min_length': 15
                                })

        self.assertEqual(ret[0]['n_initial_contigs'], 2)
        self.assertEqual(ret[0]['n_contigs_removed'], 1)
        self.assertEqual(ret[0]['n_contigs_remaining'], 1)


from copy import deepcopy

def wallsAndGates(rooms):
    # write your code here
    row_len = len(rooms)
    col_len = len(rooms[0])

    inf = 2**31 - 1

    def bfs(start, seen):

        def valid(row, col):
            return 0 <= row < row_len and 0 <= col < col_len and \
                not seen[row][col] and not rooms[row][col] == -1

        seen[start[0]][start[1]] = True
        dist = 0
        queue = [[start, dist]]
        for node in queue:
            row, col = node[0]
            dist = node[1]

            if valid(row+1, col):
                seen[row+1][col] = True
                queue.append([[row+1, col], dist+1])

            if valid(row-1, col):
                seen[row-1][col] = True
                queue.append([[row-1, col], dist+1])

            if valid(row, col+1):
                seen[row][col+1] = True
                queue.append([[row, col+1], dist+1])

            if valid(row, col-1):
                seen[row][col-1] = True
                queue.append([[row, col-1], dist+1])

            if rooms[row][col] == 0:
                return dist

    visited = [[False for _ in range(col_len)] for _ in range(row_len)]
    for row in range(row_len):
        for col in range(col_len):
            if rooms[row][col] == inf:
                val = bfs([row, col], deepcopy(visited))
                rooms[row][col] = val if val else inf
    return rooms


import numpy as np
import tensorflow as tf
import pandas as pd
import os

def shuffle(X, Y, M):
    index = np.arange(X.shape[0])
    np.random.shuffle(index)
    X = X[index]
    Y = Y[index]
    M = M[index]
    return X, Y, M

def markov(X1):
    X2 = np.dot(X1, X1)
    X2 = np.power(X2, 2)
    D = np.linalg.inv(np.diag(np.sqrt(np.sum(X2, axis=1))))
    X2 = np.dot(np.dot(D, X2), D)
    return X2

def mask_metric(train_len, validation_len, kmer_len):
    mask_train = [1] * train_len + [0] * validation_len + [0] * (4 ** kmer_len)
    mask_train = np.array(mask_train).reshape(len(mask_train), 1)
    mask_test = [0] * train_len + [1] * validation_len + [0] * (4 ** kmer_len)
    mask_test = np.array(mask_test).reshape(len(mask_test), 1)
    return mask_train, mask_test

def mask_sigmoid_cross_entropy(preds, labels, mask):
    loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=preds, labels=labels)
    # mask=tf.cast(mask,dtype=tf.float32)
    # mask/=tf.reduce_mean(mask)
    loss *= mask
    return loss

def masked_accuracy(preds, labels, mask):
    zz = tf.greater(preds, 0.5)
    preds = tf.cast(zz, dtype=tf.float32)
    correct_prediction = tf.equal(preds - labels, 0)
    accuracy_all = tf.cast(correct_prediction, tf.float32)
    mask /= tf.reduce_mean(mask)
    accuracy_all *= mask
    return accuracy_all

def masked_AUC(preds, labels, mask, a, b):
    preds = preds[a:b, :] * mask[a:b, :]
    labels = labels[a:b, :] * mask[a:b, :]
    auc = tf.metrics.auc(labels, preds, num_thresholds=1000)[1]
    return auc

def validation(frequence_array, slide_frequence_array, label, kmer_length, random_seed, GPU_option):
    frequence_array, label, slide_frequence_array = shuffle(frequence_array, label, slide_frequence_array)
    train_num = int(0.8 * frequence_array.shape[0])
    validation_num = frequence_array.shape[0] - train_num
    count = (frequence_array != 0).sum(0)
    slide_count = (slide_frequence_array != 0).sum(0)
    a, b = slide_frequence_array.shape[0], slide_frequence_array.shape[1]
    w = np.zeros((b, b))
    for i in range(b):
        for j in range(i + 1, b):
            w[i, j] = np.sum((slide_frequence_array[:, i] > 0) & (slide_frequence_array[:, j] > 0))
            w[j, i] = w[i, j]
    p = slide_count / a  # p为一维数组
    a1 = frequence_array.shape[0]
    b1 = frequence_array.shape[1]
    A1 = np.identity(a1)
    A4 = np.identity(b1)
    A2 = frequence_array * (np.ones(a1).reshape(a1, 1) * np.log(a1 / (1 + count)))
    A3 = np.transpose(A2)
    p_ = p.reshape(len(p), 1) * p
    A4[w > 0] = np.log(w[w > 0] / p_[w > 0])
    A4[A4 < 0] = 0
    A4 = A4 - np.diag(np.diag(A4)) + np.identity(A4.shape[0])
    A = np.vstack((np.hstack((A1, A2)), np.hstack((A3, A4))))
    D = np.linalg.inv(np.diag(np.sqrt(np.sum(A, axis=1))))
    nor_A = np.dot(np.dot(D, A), D)

    mask_train, mask_test = mask_metric(train_num, validation_num, kmer_length)
    mask_train = mask_train.astype(np.float32)
    mask_test = mask_test.astype(np.float32)
    train_len = train_num
    test_len = validation_num
    feature_matrix = np.identity(nor_A.shape[0]).astype(np.float32)
    graph_matrix = nor_A.astype(np.float32)
    true_label = label.astype(np.float32)

    # 开始tensorflow阶段
    w1 = tf.Variable(tf.random_normal([graph_matrix.shape[0], 200], stddev=1, seed=1))
    w2 = tf.Variable(tf.random_normal([200, 1], stddev=1, seed=1))

    x = tf.placeholder(tf.float32, shape=(None, None), name="x-input")
    z = tf.placeholder(tf.float32, shape=(None, None), name="z-feature")
    y_ = tf.placeholder(tf.float32, shape=(None, 1), name="y-input")
    a = tf.nn.relu(tf.matmul(tf.matmul(x, z), w1))
    y = tf.matmul(tf.matmul(x, a), w2)
    y1 = tf.sigmoid(y)

    cross_entropy = tf.reduce_mean(mask_sigmoid_cross_entropy(y, true_label, mask_train))
    train_accuracy = tf.reduce_mean(masked_accuracy(y, true_label, mask_train))
    test_accuracy = tf.reduce_mean(masked_accuracy(y, true_label, mask_test))
    train_auc = masked_AUC(y1, true_label, mask_train, 0, train_len)
    test_auc = masked_AUC(y1, true_label, mask_test, train_len, train_len + test_len)
    train_step = tf.train.AdamOptimizer(0.01).minimize(cross_entropy)

    tf.set_random_seed(random_seed)
    loss_list = []
    train_accuracy_list = []
    train_auc_list = []
    test_accuracy_list = []
    test_auc_list = []

    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    #os.environ["CUDA_VISIBLE_DEVICES"] = "0, 1, 2, 3"
    os.environ["CUDA_VISIBLE_DEVICES"] = GPU_option
    gpu_options = tf.GPUOptions(allow_growth = True)
    config = tf.ConfigProto(gpu_options = gpu_options)
    print("begin one dataset training")
    with tf.Session(config = config) as sess:
        init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
        sess.run(init)
        steps = 5001
        for i in range(steps):
            _, total_cross_entropy, total_train_accuracy, total_train_auc = sess.run(
                [train_step, cross_entropy, train_accuracy, train_auc], feed_dict={x: graph_matrix, z: feature_matrix, y_: true_label})
            if i % 50 == 0:
                loss_list.append(total_cross_entropy)
                train_accuracy_list.append(total_train_accuracy)
                train_auc_list.append(total_train_auc)
                total_test_accuracy, total_test_auc = sess.run([test_accuracy, test_auc],
                                                                        feed_dict={x: graph_matrix, z: feature_matrix, y_: true_label})
                test_accuracy_list.append(total_test_accuracy)
                test_auc_list.append(total_test_auc)

    print("end one dataset training")
    return np.average(np.array(test_auc_list)[-5:]), np.average(np.array(test_accuracy_list)[-5:]), np.average(np.array(loss_list)[-5:])


import time
import math
from copy import copy, deepcopy
import os
import os.path as osp
from collections import defaultdict
import signal

import wandb
import numpy as np

import logger
# from helpers.distributed_util import sync_check
from helpers.console_util import timed_cm_wrapper, log_iter_info
from helpers.opencv_util import record_video

BRAC_BEHAV_STEPS = 25_000  # note: this has nothing to do with 'warm-starting with behavioral cloning'

debug_lvl = os.environ.get('DEBUG_LVL', 0)
try:
    debug_lvl = np.clip(int(debug_lvl), a_min=0, a_max=3)
except ValueError:
    debug_lvl = 0
DEBUG = bool(debug_lvl >= 1)

def rollout_generator(env, agent, rollout_len, use_noise_process):

    t = 0
    if use_noise_process:
        # Reset agent's noise process
        agent.reset_noise()
    # Reset agent's env
    ob = np.array(env.reset())

    while True:

        # Predict action
        ac = agent.predict(ob, apply_noise=True, which='main')
        # NaN-proof and clip
        ac = np.nan_to_num(ac)
        ac = np.clip(ac, env.action_space.low, env.action_space.high)

        if t > 0 and t % rollout_len == 0:
            yield

        # Interact with env(s)
        new_ob, rew, done, _ = env.step(ac)

        # Assemble and store transition in memory
        transition = {
            "obs0": ob,
            "acs": ac,
            "obs1": new_ob,
            "rews": rew,
            "dones1": done,
        }
        agent.store_transition(transition)

        # Set current state with the next
        ob = np.array(deepcopy(new_ob))

        if done:
            if use_noise_process:
                # Reset agent's noise process
                agent.reset_noise()
            # Reset agent's env
            ob = np.array(env.reset())

        t += 1

def ep_generator(env, agent, render, record, which):
    """Generator that spits out a trajectory collected during a single episode
    `append` operation is also significantly faster on lists than numpy arrays,
    they will be converted to numpy arrays once complete and ready to be yielded.
    """

    kwargs = {'mode': 'rgb_array'}

    def _render():
        return env.render(**kwargs)

    ob = np.array(env.reset())

    if record:
        ob_orig = _render()

    cur_ep_len = 0
    cur_ep_env_ret = 0
    obs = []
    if record:
        obs_render = []
    acs = []
    env_rews = []

    # Not necessarily used
    qs = []
    mcqs = []

    while True:

        # Predict action
        _ac = agent.predict(ob, apply_noise=False, which=which)

        if isinstance(_ac, dict):
            ac = _ac['ac']
            q = _ac['q_mean']
            mcq = _ac['mc_q_mean']
            qs.append(q)
            mcqs.append(mcq)
        else:
            ac = _ac

        # NaN-proof and clip
        ac = np.nan_to_num(ac)
        ac = np.clip(ac, env.action_space.low, env.action_space.high)

        obs.append(ob)
        if record:
            obs_render.append(ob_orig)
        acs.append(ac)
        new_ob, env_rew, done, _ = env.step(ac)

        if render:
            env.render()

        if record:
            ob_orig = _render()

        env_rews.append(env_rew)
        cur_ep_len += 1
        cur_ep_env_ret += env_rew
        ob = np.array(deepcopy(new_ob))
        if done:
            obs = np.array(obs)
            if record:
                obs_render = np.array(obs_render)
            acs = np.array(acs)
            env_rews = np.array(env_rews)
            out = {"obs": obs,
                   "acs": acs,
                   "env_rews": env_rews,
                   "ep_len": cur_ep_len,
                   "ep_env_ret": cur_ep_env_ret}
            if isinstance(_ac, dict):
                out.update({'q': np.array(qs).mean(),
                            'mcq': np.array(mcqs).mean()})
            if record:
                out.update({"obs_render": obs_render})

            yield out

            cur_ep_len = 0
            cur_ep_env_ret = 0
            obs = []
            if record:
                obs_render = []
            acs = []
            env_rews = []

            # Not necessarily used
            qs = []
            mcqs = []

            ob = np.array(env.reset())

            if record:
                ob_orig = _render()

def evaluate(args,
             env,
             agent_wrapper,
             experiment_name):

    # Create an agent
    agent = agent_wrapper()

    # Create episode generator
    ep_gen = ep_generator(env, agent, args.render)

    if args.record:
        vid_dir = osp.join(args.video_dir, experiment_name)
        os.makedirs(vid_dir, exist_ok=True)

    # Load the model
    agent.load(args.model_path, args.iter_num)
    logger.info("model loaded from path:\n  {}".format(args.model_path))

    # Initialize the history data structures
    ep_lens = []
    ep_env_rets = []
    # Collect trajectories
    for i in range(args.num_trajs):
        logger.info("evaluating [{}/{}]".format(i + 1, args.num_trajs))
        traj = ep_gen.__next__()
        ep_len, ep_env_ret = traj['ep_len'], traj['ep_env_ret']
        # Aggregate to the history data structures
        ep_lens.append(ep_len)
        ep_env_rets.append(ep_env_ret)
        if args.record:
            # Record a video of the episode
            record_video(vid_dir, i, traj['obs_render'])

    # Log some statistics of the collected trajectories
    ep_len_mean = np.mean(ep_lens)
    ep_env_ret_mean = np.mean(ep_env_rets)
    logger.record_tabular("ep_len_mean", ep_len_mean)
    logger.record_tabular("ep_env_ret_mean", ep_env_ret_mean)
    logger.dump_tabular()

def generate_buffer(args,
                    env,
                    agent_wrapper,
                    experiment_name):

    # Set up where to save the exploration data
    explo_dir = osp.join(args.exploration_dir, experiment_name)
    os.makedirs(explo_dir, exist_ok=True)

    # Create an agent
    agent = agent_wrapper()

    # Log the type of exploration used by the agent
    logger.info("parameter noise used: {}".format(agent.param_noise))
    logger.info("action noise used: {}".format(agent.ac_noise))

    # Create episode generator
    roll_gen = rollout_generator(env, agent, rollout_len=args.gen_buffer_size)
    # Note, we only need to generate once from the generator (no training).

    # Load the model
    agent.load(args.model_path, args.iter_num)
    logger.info("model loaded from path:\n  {}".format(args.model_path))

    # Generate rollout to populate the replay buffer
    roll_gen.__next__()  # no need to get the returned rollout, stored in buffer

    print(agent.replay_buffer.num_entries)

    # Save the data on disk
    agent.save_full_history(explo_dir, "0-generated")  # technically no training is done here
    # Note, we use the full history method to be sure nothing is flushed

def learn(args,
          rank,
          env,
          main_eval_env,
          maxq_eval_env,
          cwpq_eval_env,
          agent_wrapper,
          experiment_name,
          use_noise_process=None):

    # Create an agent
    agent = agent_wrapper()

    # Create context manager that records the time taken by encapsulated ops
    timed = timed_cm_wrapper(logger, use=DEBUG)

    # Start clocks
    num_iters = int(args.num_steps) if args.offline else int(args.num_steps) // args.rollout_len
    iters_so_far = 0
    timesteps_so_far = 0
    tstart = time.time()

    if rank == 0:
        # Create collections
        d = defaultdict(list)

        # Set up model save directory
        ckpt_dir = osp.join(args.checkpoint_dir, experiment_name)
        os.makedirs(ckpt_dir, exist_ok=True)
        if args.record:
            vid_dir = osp.join(args.video_dir, experiment_name)
            os.makedirs(vid_dir, exist_ok=True)
        if not agent.hps.offline:
            # Set up replay save directory
            replay_dir = osp.join(args.replay_dir, experiment_name)
            os.makedirs(replay_dir, exist_ok=True)

        # Handle timeout signal gracefully
        def timeout(signum, frame):
            # Save the model
            agent.save(ckpt_dir, "{}_timeout".format(iters_so_far))
            # No need to log a message, orterun stopped the trace already
            # No need to end the run by hand, SIGKILL is sent by orterun fast enough after SIGTERM

        # Tie the timeout handler with the termination signal
        # Note, orterun relays SIGTERM and SIGINT to the workers as SIGTERM signals,
        # quickly followed by a SIGKILL signal (Open-MPI impl)
        signal.signal(signal.SIGTERM, timeout)

        # Group by everything except the seed, which is last, hence index -1
        group = '.'.join(experiment_name.split('.')[:-1])

        # Set up wandb
        while True:
            try:
                wandb.init(
                    project=args.wandb_project,
                    name=experiment_name,
                    id=experiment_name,
                    group=group,
                    config=args.__dict__,
                    dir=args.root,
                )
                break
            except Exception:
                pause = 10
                logger.info("wandb co error. Retrying in {} secs.".format(pause))
                time.sleep(pause)
        logger.info("wandb co established!")

    # Create rollout generator for training the agent
    if args.offline:
        pass
    else:
        roll_gen = rollout_generator(env, agent, args.rollout_len, use_noise_process)
    if rank == 0:
        # Create episode generator for evaluating the agent
        main_eval_ep_gen = ep_generator(main_eval_env, agent, args.render, args.record, which='main')
        maxq_eval_ep_gen = ep_generator(maxq_eval_env, agent, args.render, args.record, which='maxq')
        cwpq_eval_ep_gen = ep_generator(cwpq_eval_env, agent, args.render, args.record, which='cwpq')

    while iters_so_far <= num_iters:

        if iters_so_far % 100 == 0 or DEBUG:
            log_iter_info(logger, iters_so_far, num_iters, tstart)

        # if iters_so_far % 20 == 0:
        #     # Check if the mpi workers are still synced
        #     sync_check(agent.actr)

        if rank == 0 and iters_so_far % args.save_frequency == 0:
            # Save the model
            agent.save(ckpt_dir, iters_so_far)
            logger.info("time to checkpoint. Saving model @: {}".format(ckpt_dir))
            if not agent.hps.offline:
                # Save the replay buffer and the full history
                agent.save_memory(replay_dir, iters_so_far)
                agent.save_full_history(replay_dir, iters_so_far)

        if args.offline:
            pass
        else:
            # Sample mini-batch in env with perturbed actor and store transitions
            with timed("interacting"):
                roll_gen.__next__()  # no need to get the returned rollout, stored in buffer

        if args.algo.split('_')[0] == 'brac' and iters_so_far == 0:
            with timed('behavior policy pre-training'):
                # Train the behavior policy estimate in a pre-training phase, as in the BRAC codebase
                for i in range(BRAC_BEHAV_STEPS):
                    # Sample a batch of transitions from the replay buffer
                    batch = agent.sample_batch()
                    loss = agent.update_behavior_policy_clone(batch=batch)
                    if i % 10 == 0:
                        i_fmt = str(i).zfill(math.floor(math.log10(BRAC_BEHAV_STEPS)) + 1)
                        logger.info(f"bc iter: {i_fmt} | bc loss: {loss}")

        with timed('training'):
            for training_step in range(args.training_steps_per_iter):

                if agent.param_noise is not None:
                    if training_step % args.pn_adapt_frequency == 0:
                        # Adapt parameter noise
                        agent.adapt_param_noise()
                    if rank == 0 and iters_so_far % args.eval_frequency == 0:
                        # Store the action-space dist between perturbed and non-perturbed
                        d['pn_dist'].append(agent.pn_dist)
                        # Store the new std resulting from the adaption
                        d['pn_cur_std'].append(agent.param_noise.cur_std)

                # Sample a batch of transitions from the replay buffer
                batch = agent.sample_batch()

                # Update the actor and critic
                metrics, lrnows = agent.update_actor_critic(
                    batch=batch,
                    update_actor=not bool(iters_so_far % args.actor_update_delay),  # from TD3
                    iters_so_far=iters_so_far,
                )
                if rank == 0 and iters_so_far % args.eval_frequency == 0:
                    # Log training stats
                    d['actr_losses'].append(metrics['actr_loss'])
                    d['crit_losses'].append(metrics['crit_loss'])
                    if iters_so_far >= agent.hps.warm_start:
                        if args.algo.split('_')[0] == 'bcp':
                            if agent.hps.base_pi_loss == 'crr_exp':
                                d['is_crr_adv_clipped_sum'].append(metrics['is_crr_adv_clipped_sum'])
                            d['alpha_pri'].append(metrics['alpha_pri'])
                            d['gap'].append(metrics['gap'])
                            if (agent.hps.use_rnd_monitoring or
                                    agent.hps.base_pe_loss in ['htg_1', 'htg_2'] or
                                    agent.hps.base_next_action == 'beta_theta_max_rnd'):
                                d['rnd_score'].append(metrics['rnd_score'])
                    if agent.hps.clipped_double:
                        d['twin_losses'].append(metrics['twin_loss'])
                    if agent.hps.prioritized_replay:
                        iws = metrics['iws']  # last one only

        if rank == 0 and iters_so_far % args.eval_frequency == 0:

            with timed("evaluating"):

                # Update the parameters of the eval actors with the ones of the trained one
                agent.update_eval_nets()

                for eval_step in range(args.eval_steps_per_iter):
                    # Sample an episode
                    main_eval_ep = main_eval_ep_gen.__next__()
                    maxq_eval_ep = maxq_eval_ep_gen.__next__()
                    cwpq_eval_ep = cwpq_eval_ep_gen.__next__()
                    # Aggregate data collected during the evaluation to the buffers
                    d['main_eval_len'].append(main_eval_ep['ep_len'])
                    d['maxq_eval_len'].append(maxq_eval_ep['ep_len'])
                    d['cwpq_eval_len'].append(cwpq_eval_ep['ep_len'])
                    d['main_eval_env_ret'].append(main_eval_ep['ep_env_ret'])
                    d['maxq_eval_env_ret'].append(maxq_eval_ep['ep_env_ret'])
                    d['cwpq_eval_env_ret'].append(cwpq_eval_ep['ep_env_ret'])
                    if args.algo.split('_')[0] == 'bcp':
                        d['eval_q'].append(maxq_eval_ep['q'])
                        d['eval_mcq'].append(maxq_eval_ep['mcq'])

        # Increment counters
        iters_so_far += 1
        if args.offline:
            _str = 'iters so far'
            step = copy(iters_so_far)
        else:
            timesteps_so_far += args.rollout_len
            _str = 'timesteps so far'
            step = copy(timesteps_so_far)

        if rank == 0 and ((iters_so_far - 1) % args.eval_frequency == 0):

            # Log stats in csv
            logger.record_tabular(_str, step)

            if iters_so_far - 1 >= agent.hps.warm_start:
                if args.algo.split('_')[0] == 'bcp':
                    if agent.hps.base_pi_loss == 'crr_exp':
                        logger.record_tabular('is_crr_adv_clipped_sum', np.mean(d['is_crr_adv_clipped_sum']))
                    logger.record_tabular('alpha_pri', np.mean(d['alpha_pri']))
                    logger.record_tabular('gap', np.mean(d['gap']))
                    if (agent.hps.use_rnd_monitoring or
                            agent.hps.base_pe_loss in ['htg_1', 'htg_2'] or
                            agent.hps.base_next_action == 'beta_theta_max_rnd'):
                        logger.record_tabular('rnd_score', np.mean(d['rnd_score']))
            logger.record_tabular('main_eval_len', np.mean(d['main_eval_len']))
            logger.record_tabular('maxq_eval_len', np.mean(d['maxq_eval_len']))
            logger.record_tabular('cwpq_eval_len', np.mean(d['cwpq_eval_len']))
            logger.record_tabular('main_eval_env_ret', np.mean(d['main_eval_env_ret']))
            logger.record_tabular('maxq_eval_env_ret', np.mean(d['maxq_eval_env_ret']))
            logger.record_tabular('cwpq_eval_env_ret', np.mean(d['cwpq_eval_env_ret']))
            if args.algo.split('_')[0] == 'bcp':
                logger.record_tabular('eval_q', np.mean(d['eval_q']))
                logger.record_tabular('eval_mcq', np.mean(d['eval_mcq']))
            logger.info("dumping stats in .csv file")
            logger.dump_tabular()

            if args.record:
                # Record the last episode in a video
                record_video(vid_dir, iters_so_far, maxq_eval_ep['obs_render'])  # picked only one, maxq

            # Log stats in dashboard
            if agent.hps.prioritized_replay:
                quantiles = [0.1, 0.25, 0.5, 0.75, 0.9]
                np.quantile(iws, quantiles)
                wandb.log({"q{}".format(q): np.quantile(iws, q)
                           for q in [0.1, 0.25, 0.5, 0.75, 0.9]},
                          step=step)
            if agent.param_noise is not None:
                wandb.log({'pn_dist': np.mean(d['pn_dist']),
                           'pn_cur_std': np.mean(d['pn_cur_std'])},
                          step=step)
            wandb.log({'actr_loss': np.mean(d['actr_losses']),
                       'actr_lrnow': np.array(lrnows['actr']),
                       'crit_loss': np.mean(d['crit_losses'])},
                      step=step)
            if agent.hps.clipped_double:
                wandb.log({'twin_loss': np.mean(d['twin_losses'])},
                          step=step)
            if iters_so_far - 1 >= agent.hps.warm_start:
                if args.algo.split('_')[0] == 'bcp':
                    if agent.hps.base_pi_loss == 'crr_exp':
                        wandb.log({'is_crr_adv_clipped_sum': np.mean(d['is_crr_adv_clipped_sum'])},
                                  step=step)
                    wandb.log({'alpha_pri': np.mean(d['alpha_pri']),
                               'gap': np.mean(d['gap'])},
                              step=step)
                    if (agent.hps.use_rnd_monitoring or
                            agent.hps.base_pe_loss in ['htg_1', 'htg_2'] or
                            agent.hps.base_next_action == 'beta_theta_max_rnd'):
                        wandb.log({'rnd_score': np.mean(d['rnd_score'])},
                                  step=step)
            wandb.log({'main_eval_len': np.mean(d['main_eval_len']),
                       'maxq_eval_len': np.mean(d['maxq_eval_len']),
                       'cwpq_eval_len': np.mean(d['cwpq_eval_len']),
                       'main_eval_env_ret': np.mean(d['main_eval_env_ret']),
                       'maxq_eval_env_ret': np.mean(d['maxq_eval_env_ret']),
                       'cwpq_eval_env_ret': np.mean(d['cwpq_eval_env_ret'])},
                      step=step)
            if args.algo.split('_')[0] == 'bcp':
                wandb.log({'eval_q': np.mean(d['eval_q']),
                           'eval_mcq': np.mean(d['eval_mcq'])},
                          step=step)

            # Clear the iteration's running stats
            d.clear()

    if not agent.hps.offline:
        # When running the algorithm online, save behavior policy model once we are done iterating
        if rank == 0:
            # Save the model
            agent.save(ckpt_dir, iters_so_far)
            logger.info("we're done. Saving behavior policy model @: {}".format(ckpt_dir))
            logger.info("bye.")
            # Save the replay buffer and the full history
            agent.save_memory(replay_dir, iters_so_far)
            agent.save_full_history(replay_dir, iters_so_far)

    if rank == 0:
        # Save once we are done iterating
        agent.save(ckpt_dir, iters_so_far)
        logger.info("we're done. Saving model @: {}".format(ckpt_dir))
        logger.info("bye.")