# -*- coding: utf-8 -*-
"""
Code to determine the grid from all the ionograms in a folder
using adjusted peak detection (From the weighed sum of all the image plots in a subsubdirectory,
the indices corresponding to the grid values are determined)

"""
# Library imports
import numpy as np
import pandas as pd
import scipy.signal as signal

#Determined from determine_default_grid_values
HZ = [1.5,2.0,2.5,3.5,4.5,5.5,6.5,7.0,7.5,8.5,9.5,10.5,11.5]
DEFAULT_HZ_COORD = [149,273,335,390,496,604,711,837,898,964,1128,1314,1444]
MEAN_HZ = [0.5*(num + DEFAULT_HZ_COORD[i+1])for i, num in enumerate(DEFAULT_HZ_COORD[:-1])]
UPPER_LIMIT_HZ_COORD =[89] + MEAN_HZ
LOWER_LIMIT_HZ_COORD = MEAN_HZ + [1510]

KM_DEFAULT_100 = 55
KM_DEFAULT_200 = 110

def all_stack(df_img):
    """Returns the equally weighed sum of all the correctly extracted ionogram plot areas in a subsubdirectory
    z
    :param df_img: Dataframe contaning all the correctly extracted ionogram plot areas in a subsubdirectory (output of image_segmentation.segment_images_in_subdir.segment_images)
    :type df_img: class: `pandas.core.frame.DataFrame`
    :param cutoff_width: the width of an ionogram should be within cutoff_width of the median width of all the ionogram in a subdirectory (should be the same as the one used in scan2data.image_segmentation.segment_images_in_subdir.segment_images)
    :type cutoff_width: int
    :param cutoff_height: the height of an ionogram should be within cutoff_height of the median height of all the ionogram in a subdirectory (should be the same as the one used in scan2data.image_segmentation.segment_images_in_subdir.segment_images)
    :type cutoff_height: int
    :returns: weighed_sum i.e. equally weighed sum of all the extracted ionogram plot areas in a subsubdirectory
    :rtype: class: `numpy.ndarray`
    """

    # Pad the image if needed
    max_h = df_img["height"].max()
    max_w =  df_img["width"].max()
    median_h = int(np.median(df_img["height"]))
    median_w = int(np.median(df_img["width"]))

    df_img["padded"] = df_img["ionogram"].apply(lambda img: np.pad( img, ((0,max_h-img.shape[0]),(0,max_w-img.shape[1])),mode="constant",constant_values=1))

    #Weighed sum of the ionograms in a subdirectory <-- Q: WHY DO WE HAVE TO TAKE THE WEIGHTED SUM OF ALL OF THE IONOGRAMS? SHOULDN'T EACH IONOGRAM BE ANALYZED INDIVIDUALLY?
    weight = len(df_img.index)
    weighed_sum = weight * np.sum((df_img["padded"]).tolist(), axis = 0)
    median_h = median_h if max_h>median_h else median_h-1

    return weighed_sum[0:-(max_h-median_h),0:-(max_w-median_w)]

def indices_highest_peaks(img, row_or_col,
                      peak_prominence_threshold=0.1, distance_between_peaks=5):
    """Determines and returns the indices of peak median values from the rows or column of an image

    :param img: grayscale image in the form of an 2D uint8 array
    :type img: class: `numpy.ndarray`
    :param row_or_col: 0 for colum or 1 for row
    :type row_or_col: int
    :param peak_prominence_threshold: the threshold to detect peaks that correspond to the grid lines, defaults to 0.1
    :type peak_prominence_threshold: int, optional
    :param distance_between_peaks: the minimum number of samples between subsequent peaks, defaults to 5
    :type distance_between_peaks: int, optional
    :returns: select_peaks i.e. array of the indices of peak median values from the rows or column of an image
    :rtype: class: `numpy.ndarray`
    """

    # Median values along each row or column
    median_values = np.median(img,row_or_col)

    # Normalize median values so they are between 0 and 1
    median_values_normalized = (median_values - np.min(median_values))/(np.max(median_values)-np.min(median_values))

    # Prepare peaks for peak detection function: the peaks should be pointing upwards
    peaks_function = 1 + -1*median_values_normalized

    # Detect all peaks
    select_peaks, _ = signal.find_peaks(peaks_function, distance=distance_between_peaks, prominence=peak_prominence_threshold) #from scipy.signal

    #Remove edges from peaks
    h,w = img.shape
    if row_or_col == 0:
        select_peaks = select_peaks[:w]
    else:
        select_peaks = select_peaks[:h]

    return select_peaks

def adjust_arr_peaks(weighed_sum,arr_peaks,desired_length,row_or_col,
                  distance_between_peaks=30,peak_prominence_threshold=0.1,n_tries=1000,update_amount=0.01):
    """Adjust an array of peaks to the desired length and returns it.

     :param weighed_sum: equally weighed sum of all the image plot areas in a subsubdirectory
     :type weighed_sum: class: `numpy.ndarray`
     :param arr_peaks: array of peaks to adjust to the desired length
     :type arr_peaks: class: `numpy.ndarray`
     :param desired_length: number of elements desired in array
     :type desired_length: int
     :param row_or_col: 0 for colum or 1 for row
     :type row_or_col: int
     :param distance_between_peaks: the minimum number of samples between subsequent peaks, defaults to 30
     :type distance_between_peaks: int, optional
     :param peak_prominence_threshold: the threshold to detect peaks that correspond to the grid lines, defaults to 0.1
     :type peak_prominence_threshold: int, optional
     :param n_tries: the number of maximum tries to adjust arr, defaults to 1000
     :type n_tries: int, optional
     :param update_amount: by how much peak_prominence_threshold is updated for each iteration, defaults to 0.01
     :type update_amount: int, optional
     :returns: select_peaks i.e. adjusted array of the indices of peak median values from the rows or column of an image
     :rtype: class: `numpy.ndarray`
     ..note:: To prevent infinite loops, the script only runs for a maximum of n_tries times
    """

    arr_peaks = indices_highest_peaks(weighed_sum, row_or_col, peak_prominence_threshold, distance_between_peaks)  #Q: WHY IS THE FUNCTION TAKING IN arr_peaks IF IT IS ALREADY CALCULATING IT HERE?

    # Adjust if lenght is not the desired length by re-running indices_highest_peaks with different parameters
    while len(arr_peaks) != desired_length and n_tries !=0:

        if len(arr_peaks) > desired_length:
            # increase peak_prominence_threshold
            peak_prominence_threshold = peak_prominence_threshold + update_amount
            arr_peaks = indices_highest_peaks(weighed_sum, row_or_col,
                                               peak_prominence_threshold, distance_between_peaks)
        else:
            # decrease peak_prominence_threshold
            peak_prominence_threshold = peak_prominence_threshold - update_amount
            arr_peaks = indices_highest_peaks(weighed_sum, row_or_col,
                                               peak_prominence_threshold, distance_between_peaks)
        n_tries = n_tries - 1

    return arr_peaks

def get_grid_mappings(weighed_sum,
                      use_defaults=True,min_index_row_peaks=40):
    """Determines and returns the the mapping between coordinate values and frequency/depth values in a subdirectory

    :param weighed_sum: equally weighed sum of all the image plot areas in a subsubdirectory
    :type weighed_sum: class: `numpy.ndarray`
    :param use_defaults: use default values , defaults to True
    :type use_defaults: bool
    :param min_index_row_peaks: starting index to consider for peaks to determine km lines,defaults to 40
    :type min_index_row_peaks: int, optional
    :returns:  col_peaks,row_peaks,mapping_Hz, mapping_km i.e. one-dimmensional array of detected peaks of ionogram by column, one-dimmensional array of detected  peaks of by row, dictionary mapping of depth (km) to y coordinates  , dictionary mapping of frequency (Hz) to x coordinates
    :rtype: class: `numpy.ndarray`,class: `numpy.ndarray`,class: `dict`, class: `dict`
    :raises Exception: returns np.nan,np.nan,np.nan,np.nan
    """
    # Detect peaks
    col_peaks = indices_highest_peaks(weighed_sum, 0)
    row_peaks = indices_highest_peaks(weighed_sum, 1)

    # Map col_peaks to Hz values
    if len(col_peaks) == len(HZ):
        mapping_Hz = dict(zip(HZ,col_peaks))

    # Map adjusted col_peaks to Hz values
    else:
        try:
            col_peaks = adjust_arr_peaks(weighed_sum, col_peaks, len(HZ), 0)
            mapping_Hz = dict(zip(HZ,col_peaks))

            # Map adjusted HZ values to default coordinates if need be
            if use_defaults:
                for i,key in enumerate(HZ):
                    if mapping_Hz[key] > UPPER_LIMIT_HZ_COORD[i] or mapping_Hz[key] < LOWER_LIMIT_HZ_COORD[i]:
                        mapping_Hz[key] = DEFAULT_HZ_COORD[i]
        except:
            if use_defaults:
                 mapping_Hz = dict(zip(HZ,DEFAULT_HZ_COORD))
            else:
                return np.nan,np.nan,np.nan,np.nan

    row_peaks = row_peaks[row_peaks > min_index_row_peaks]

    try:
        row_100 = row_peaks[0] #Should be around 30 for 100 km
        row_200 = row_peaks[1] #Should be around 30 for 100 km
    except:
        if use_defaults:
            row_100 = KM_DEFAULT_100
            row_200 = KM_DEFAULT_200
        else:
            return np.nan,np.nan,np.nan,np.nan

    if use_defaults:
        if abs(row_100 - KM_DEFAULT_100) > abs(KM_DEFAULT_200 - KM_DEFAULT_100):
            row_100 = KM_DEFAULT_100
        if abs(row_200 - KM_DEFAULT_200) > abs(KM_DEFAULT_200 - KM_DEFAULT_100):
            row_200 = KM_DEFAULT_200

    mapping_km = {100:row_100,200:row_200}

    return col_peaks, row_peaks, mapping_Hz, mapping_km


from typing import List

class Solution:
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
        n = len(intervals)
        # No sorting, intervals is already sorted.
        # intervals.sort()
        current_interval = []
        current_max = 0
        overlapped = []
        for i,interval in enumerate(intervals):
            current_max = max(current_max, interval[1])
            if i < n - 1 and current_max >= intervals[i + 1][0]:
                # Found an overlap with next interval
                if not current_interval:
                    current_interval = [interval[0]]
            else: # No overlap with next interval
                if current_interval:
                    current_interval.append(current_max)
                    overlapped.append(current_interval)
                    current_interval = []
                    current_max = 0
                else:
                    overlapped.append(interval)
        return overlapped
    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
        if not intervals:
            return [newInterval]
        if not newInterval and intervals:
            return intervals

        n = len(intervals)
        # Use binary search to find the correct insert index
        start, end, mid = 0, n, 0
        while end - start > 1:
            mid = (end - start)//2
            if newInterval < intervals[start + mid]:
                end = start + mid
            elif newInterval > intervals[start + mid]:
                start += mid

        insertIndex = -1
        if intervals[start][0] < newInterval[0]:
            insertIndex = start + 1
        elif intervals[start][0] > newInterval[0]:
            insertIndex = start
        else: # In case are equals, analize [1] position
            if intervals[start][1] > newInterval[1]:
                insertIndex = start
            else:
                insertIndex = start + 1
        # return
        # Insert newInterval and resolve overlap
        intervals.insert(insertIndex, newInterval)

        return self.merge(intervals)

# Example 1:
intervals = [[1,3],[6,9]]
newInterval = [2,5]
expected = [[1,5],[6,9]]
s = Solution()
result = s.insert(intervals, newInterval)
print(result)
print(result == expected)

# Example 2:
intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]]
newInterval = [4,8]
expected = [[1,2],[3,10],[12,16]]
# Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
s = Solution()
result = s.insert(intervals, newInterval)
print(result)
print(result == expected)

# Example 3: No intervals
intervals = []
newInterval = [2,5]
expected = [[2,5]]
s = Solution()
result = s.insert(intervals, newInterval)
print(result)
print(result == expected)

# Example 4: No newInterval and yes intervals.
intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]]
newInterval = []
expected = [[1,2],[3,5],[6,7],[8,10],[12,16]]
s = Solution()
result = s.insert(intervals, newInterval)
print(result)
print(result == expected)

# Example 5:
intervals = [[1,5]]
newInterval = [2,3]
expected = [[1,5]]
s = Solution()
result = s.insert(intervals, newInterval)
print(result)
print(result == expected)

# Example 6:
intervals = [[1,5]]
newInterval = [5,7]
expected = [[1,7]]
s = Solution()
result = s.insert(intervals, newInterval)
print(result)
print(result == expected)

# Example 7:
intervals = [[1,5]]
newInterval = [0,3]
expected = [[0,5]]
s = Solution()
result = s.insert(intervals, newInterval)
print(result)
print(result == expected)

# Example 8:
intervals = [[1,5]]
newInterval = [6,8]
expected = [[1,5],[6,8]]
s = Solution()
result = s.insert(intervals, newInterval)
print(result)
print(result == expected)

# Example 9:
intervals = [[0,2],[3,5],[6,8],[10,12],[13,15]]
newInterval = [4,7]
expected = [[0,2],[3,8],[10,12],[13,15]]
s = Solution()
result = s.insert(intervals, newInterval)
print(result)
print(result == expected)

# Example 10:
intervals = [[0,1],[5,5],[6,7],[9,11]]
newInterval = [12,21]
expected = [[0,1],[5,5],[6,7],[9,11],[12,21]]
s = Solution()
result = s.insert(intervals, newInterval)
print(result)
print(result == expected)

# Example 11:
intervals = [[2,3],[5,5],[6,6],[7,7],[8,11]]
newInterval = [6,13]
expected = [[2,3],[5,5],[6,13]]
s = Solution()
result = s.insert(intervals, newInterval)
print(result)
print(result == expected)

# Example 12:
intervals = [[3,6],[9,9],[11,13],[14,14],[16,19],[20,22],[23,25],[30,34],[41,43],[45,49]]
newInterval = [29,32]
expected = [[3,6],[9,9],[11,13],[14,14],[16,19],[20,22],[23,25],[29,34],[41,43],[45,49]]
s = Solution()
result = s.insert(intervals, newInterval)
print(result)
print(result == expected)


import jax
from jax import numpy as jnp
import torch
from torch import nn as nn
from torch.nn import functional as F

import numpy as np

from einops import rearrange

import jax_from_pt as jpt

class SelfAttention(jpt.JaxModule):
    def setup(config):
        module = jpt.Module()
        module.config = config

        dim = config.embedding_dim

        module.key_matrix = jpt.Linear.init(dim, dim)
        module.query_matrix = jpt.Identity.init()
        module.value_matrix = jpt.Linear.init(dim, dim)

        t_mask = torch.tril(torch.ones(config.context_length, config.context_length)).view(1, 1, config.context_length, config.context_length)
        j_mask = jnp.array(t_mask.numpy())

        module.register_buffer("mask", j_mask, t_mask)

        return module, T_SelfAttention(module)

    def apply(module, state, x):
        module = module(state)

        config = module.config

        *_, T, C = x.shape

        split_heads_shape = x.shape[:-1] + (config.n_heads, config.embedding_dim // config.n_heads)

        key = rearrange(module.key_matrix(x).reshape(split_heads_shape), '... T nh hs -> ... nh T hs') #.transpose(-2, -3)
        query = rearrange(module.query_matrix(x).reshape(split_heads_shape), '... T nh hs -> ... nh hs T')
        value = rearrange(module.value_matrix(x).reshape(split_heads_shape), '... T nh hs -> ... nh T hs') #.transpose(-2, -3)

        logits = jnp.matmul(key, query) # [..., nh, T, hs] x [..., nh, hs, T] -> [..., nh, T, T]

        broadcast_mask = jnp.broadcast_to((module.mask[:, :, :T, :T] == 0), logits.shape)
        masked_logits = jnp.where(broadcast_mask, -jnp.inf, logits)
        # logits.masked_fill(module.mask[:,:,:T,:T] == 0, float('-inf'))

        att_weights = jax.nn.softmax(masked_logits, axis=-1)

        y = jnp.matmul(att_weights, value) #  [..., nh, T, T]  x [..., nh, T, hs] -> [..., nh, T, hs]
        y = rearrange(y, '... nh T hs -> ... T nh hs').reshape(x.shape) # [..., nh, T, hs] -> [..., T, nh, hs] -> [..., T, C]

        return y

class T_SelfAttention(jpt.TModule):

    def forward(self, x):

        # I don't totally understand why heads are a good idea, but apparently they are...
        *_, T, C = x.shape

        config = self.config

        # assert C == self.dim, "specified axis does not have correct dimension: was {}, expected {}".format(C, self.dim)

        split_heads_shape = x.shape[:-1] + (config.n_heads, config.embedding_dim // config.n_heads)
        key = self.key_matrix(x).reshape(split_heads_shape).transpose(-2, -3) # [..., T, C] -> [..., T, nh, hs] -> [..., nh, T, hs]
        query = self.query_matrix(x).reshape(split_heads_shape).transpose(-2, -3) # [..., T, C] -> [..., T, nh, hs] -> [..., nh, T, hs]
        value = self.value_matrix(x).reshape(split_heads_shape).transpose(-2, -3) # [..., T, C] -> [..., T, nh, hs] -> [..., nh, T, hs]

        assert key.shape[-2] == T, "shape mismatch: {}".format(key.shape)

        # print(key.shape)
        # print(query.shape)

        logits = torch.matmul(key, query.transpose(-1, -2)) # [..., nh, T, hs] x [..., nh, hs, T] -> [..., nh, T, T]
        masked_logits = logits.masked_fill(self.mask[:,:,:T,:T] == 0, float('-inf'))

        att_weights = F.softmax(masked_logits, dim=-1)

        y = torch.matmul(att_weights, value) #  [..., nh, T, T]  x [..., nh, T, hs] -> [..., nh, T, hs]
        y = y.transpose(-2, -3).reshape(x.shape) # [..., nh, T, hs] -> [..., T, nh, hs] -> [..., T, C]

        return y

# class SelfAttention(torch.nn.Module):
#     def __init__(self, config):
#         super().__init__()
#         self.config = config
#         self.dim = config.embedding_dim
#         self.key_matrix = torch.nn.Linear(self.dim, self.dim) # maybe we want to mess with initialization schemes later?
#         self.query_matrix = torch.nn.Identity()#torch.nn.Linear(self.dim, self.dim)

#         self.value_matrix = torch.nn.Linear(self.dim, self.dim)

#         self.n_heads = config.n_heads
#         self.context_length = config.context_length
#         self.register_buffer("mask", torch.tril(torch.ones(config.context_length, config.context_length)).view(1, 1, config.context_length, config.context_length))

#         assert self.dim % self.n_heads == 0, "number of heads ({}) does not evenly divide embedding dim ({})".format(self.n_heads, self.dim)

#     def forward(self, x):

#         # I don't totally understand why heads are a good idea, but apparently they are...
#         *_, T, C = x.shape

#         assert C == self.dim, "specified axis does not have correct dimension: was {}, expected {}".format(C, self.dim)

#         split_heads_shape = x.shape[:-1] + (self.n_heads, self.dim // self.n_heads)
#         key = self.key_matrix(x).reshape(split_heads_shape).transpose(-2, -3) # [..., T, C] -> [..., T, nh, hs] -> [..., nh, T, hs]
#         query = self.query_matrix(x).reshape(split_heads_shape).transpose(-2, -3) # [..., T, C] -> [..., T, nh, hs] -> [..., nh, T, hs]
#         value = self.value_matrix(x).reshape(split_heads_shape).transpose(-2, -3) # [..., T, C] -> [..., T, nh, hs] -> [..., nh, T, hs]

#         assert key.shape[-2] == T, "shape mismatch: {}".format(key.shape)

#         # print(key.shape)
#         # print(query.shape)

#         logits = torch.matmul(key, query.transpose(-1, -2)) # [..., nh, T, hs] x [..., nh, hs, T] -> [..., nh, T, T]
#         masked_logits = logits.masked_fill(self.mask[:,:,:T,:T] == 0, float('-inf'))

#         att_weights = F.softmax(masked_logits, dim=-1)

#         y = torch.matmul(att_weights, value) #  [..., nh, T, T]  x [..., nh, T, hs] -> [..., nh, T, hs]
#         y = y.transpose(-2, -3).reshape(x.shape) # [..., nh, T, hs] -> [..., T, nh, hs] -> [..., T, C]

#         return y

class ResidualSelfAttention(jpt.JaxModule):

    def setup(config):
        module = jpt.Module()
        module.config = config

        module.selfattention = SelfAttention.init(config)
        module.ln = jpt.LayerNorm.init(config.embedding_dim)

        module.fc1 = jpt.Linear.init(config.embedding_dim, 2*config.embedding_dim)
        module.fc2 = jpt.Linear.init(2*config.embedding_dim, config.embedding_dim)

        return module, T_ResidualSelfAttention(module)

    def apply(module, state, x):
        module = module(state)

        y = module.ln(x)
        y = module.selfattention(y)

        if module.config.use_fc:
            y = module.fc1(y)
            y = jax.nn.gelu(y)
            y = module.fc2(y)

        y = x + y
        return y

class T_ResidualSelfAttention(jpt.TModule):
    def forward(self, x):
        y = self.ln(x)
        y = self.selfattention(y)

        if self.config.use_fc:
            y = self.fc1(y)
            y = F.gelu(y)
            y = self.fc2(y)

        y = x + y
        return y

# from omegaconf import OmegaConf
# config = OmegaConf.load('config/jax_test/test.yaml').model
# config.vocab_size = 1000

# state, apply, t_module, t_state = StackedAttention.init(config)

# x = np.random.normal(np.ones((10, config.context_length, config.embedding_dim)))

# idx = np.ones((10, config.context_length), dtype=int)
# mask = idx
# labels = np.zeros((10, config.context_length), dtype=int)

# t_idx = torch.tensor(idx, dtype=int)
# t_labels = torch.tensor(labels)
# t_mask = torch.tensor(mask)

# j_idx =

# t_x = torch.tensor(x, dtype=torch.float32)
# j_x = jnp.array(x)

# t_y = t_module(t_x)
# j_y = apply(state, j_x)

# print(f"torch: {t_y}")
# print(f"jax: {j_y}")

# print(jnp.sum((t_y.detach().numpy() - j_y)**2))

class StackedAttention(jpt.JaxModule):
    def setup(config):
        module = jpt.Module()
        module.config = config

        module.features = jpt.Sequential.init(*[ResidualSelfAttention.init(config) for _ in range(config.n_layers)])
        module.tok_embeddings = jpt.Embedding.init(config.vocab_size, config.embedding_dim)
        t_pos_embeddings = torch.zeros(1, config.context_length, config.embedding_dim, requires_grad=True)
        j_pos_embeddings = jnp.array(t_pos_embeddings.detach().numpy())
        module.register_parameter("pos_embeddings", j_pos_embeddings, t_pos_embeddings)

        module.head = jpt.Linear.init(config.embedding_dim, config.vocab_size)

        module.head[0]['params']['weight'] = jnp.zeros_like(module.head[0]['params']['weight'])
        module.head[0]['params']['bias'] = jnp.zeros_like(module.head[0]['params']['bias'])

        with torch.no_grad():
            module.head[2].weight.mul_(0.0)
            module.head[2].bias.mul_(0.0)

        return module, T_StackedAttention(module)

    def apply(module, state, idx, mask, labels, compute_loss=True):
        module = module(state)

        B, T = idx.shape

        T = min(T-1, module.config.context_length)

        tok_embd = module.tok_embeddings(idx[:, :T])
        pos_embd = module.pos_embeddings[:, :T, :]

        x = jax.nn.softmax(tok_embd + pos_embd, axis=-1) # input for attention layers: shape [B, T, C]

        features = module.features(x)

        if not compute_loss:
            return features

        logits = module.head(features) # shape [B, T, V]

        targets = labels[:, :T]#
        # targets = StackedAttention.get_targets(mask, idx, T)

        # cross entropy loss doesn't know about T, so we flatten the time dimension:
        # print("logits: ", logits.shape)
        # print("targets: ", targets.shape)

        logits_for_CE = logits.reshape(-1, logits.shape[-1]) # shape [BT, V]
        # print(f"logits for ce shape: {logits_for_CE.size()}, original logits shape: {logits.size()}, targets shape: {targets.size()}")
        targets_for_CE = targets.reshape(-1) # shape [BT]

        # logits_for_CE = logits_for_CE[targets_for_CE != -100]
        # targets_for_CE = targets_for_CE[targets_for_CE != -100]

        stopped_logits = jax.lax.stop_gradient(logits_for_CE)
        stopped_targets = jax.lax.stop_gradient(targets_for_CE)

        predictions = jnp.argmax(stopped_logits, 1)
        num_targets = jnp.sum(stopped_targets != -100)
        num_correct = jnp.sum(stopped_targets == predictions)
        accuracy = num_correct/num_targets

    #         predictions = torch.argmax(logits_for_CE, 1)
    #         num_targets = torch.sum(targets_for_CE != -100)
    #         num_correct = torch.sum(targets_for_CE == predictions)
    #         accuracy = num_correct/num_targets

        # loss = jnp.average(optax.softmax_cross_entropy_with_integer_labels(logits_for_CE, targets_for_CE))
        loss = jpt.softmax_cross_entropy(logits_for_CE, targets_for_CE)

        return features, loss, accuracy

class T_StackedAttention(jpt.TModule):
    def forward(self, idx, mask, labels, compute_loss=True):

        # x is 1-hot encoding
        B, T = idx.size()

        T = min(T-1, self.config.context_length)

        tok_embd = self.tok_embeddings(idx[:, :T])

        pos_embd = self.pos_embeddings[:, :T, :]

        x = F.softmax(tok_embd + pos_embd, dim=-1) # input for attention layers: shape [B, T, C]

        features = self.features(x)

        if not compute_loss:
            return features

        logits = self.head(features) # shape [B, T, V]

        targets = labels[:, :T]#
        # targets = StackedAttention.get_targets(mask, idx, T)

        # cross entropy loss doesn't know about T, so we flatten the time dimension:
        # print("logits: ", logits.shape)
        # print("targets: ", targets.shape)

        logits_for_CE = logits.reshape(-1, logits.size(-1)) # shape [BT, V]
        # print(f"logits for ce shape: {logits_for_CE.size()}, original logits shape: {logits.size()}, targets shape: {targets.size()}")
        targets_for_CE = targets.reshape(-1) # shape [BT]

        with torch.no_grad():
            predictions = torch.argmax(logits_for_CE, 1)
            num_targets = torch.sum(targets_for_CE != -100)
            num_correct = torch.sum(targets_for_CE == predictions)
            accuracy = num_correct/num_targets

        loss = F.cross_entropy(logits_for_CE, targets_for_CE)

        return features, loss, accuracy

# from omegaconf import OmegaConf
# config = OmegaConf.load('config/jax_test/test.yaml').model
# config.vocab_size = 1000

# state, apply, t_module, t_state = StackedAttention.init(config)

# x = np.random.normal(np.ones((10, config.context_length, config.embedding_dim)))

# idx = np.ones((10, config.context_length), dtype=int)
# mask = idx
# labels = np.zeros((10, config.context_length), dtype=int)

# t_idx = torch.tensor(idx, dtype=int)
# t_labels = torch.tensor(labels)
# t_mask = torch.tensor(mask)

# j_idx = jnp.array(idx)
# j_mask = jnp.array(mask)
# j_labels = jnp.array(labels)

# lr = 0.001

# t_adamw = torch.optim.AdamW(t_module.parameters(), lr, betas=(0.9, 0.99), eps=1e-08, weight_decay=0.001)

# t_f, t_l, t_a = t_module(t_idx, t_mask, t_labels)
# j_f, j_l, j_a = apply(state, j_idx, j_mask, j_labels)

# t_l.backward()
# t_adamw.step()

# import optax

# j_adamw = optax.adamw(lr, b1=0.9, b2=0.99, eps=1e-08, eps_root=0.0, weight_decay=0.001)
# j_adamw_state = j_adamw.init(state['params'])

# def loss_fn(params, constants, idx, mask, labels):
#     return apply({'params': params, 'constants': constants}, idx, mask, labels)[1]

# grad_fn = jax.grad(loss_fn)

# params = state['params']
# constants = state['constants']

# grads = grad_fn(params, constants, j_idx, j_mask, j_labels)

# updates, j_adamw_state = j_adamw.update(grads, j_adamw_state, params)
# params = optax.apply_updates(params, updates)

# state = {
#     'params': params,
#     'constants': constants
# }

# n_t_f, n_t_l, n_t_a = t_module(t_idx, t_mask, t_labels)
# n_j_f, n_j_l, n_j_a = apply(state, j_idx, j_mask, j_labels)

# print(f"torch: {t_l}")
# print(f"jax: {j_l}")

# print(f"new torch: {n_t_l}")
# print(f"new jax: {n_j_l}")

# t_f = t_f.detach().numpy()

# n_t_f = n_t_f.detach().numpy()

# print(jnp.sum((t_f - j_f)**2)/ jnp.sum(t_f**2 + j_f**2))

# print(jnp.sum((n_t_f - n_j_f)**2)/ jnp.sum(n_t_f**2 + n_j_f**2))

# class StackedAttention(torch.nn.Module):
#     def __init__(self, config):
#         super().__init__()
#         self.config = config
#         self.features = torch.nn.Sequential(*[ResidualSelfAttention(config) for _ in range(config.n_layers)])
#         self.tok_embeddings = torch.nn.Embedding(config.vocab_size, config.embedding_dim)
#         self.pos_embeddings = torch.nn.Parameter(torch.zeros(1, config.context_length, config.embedding_dim))
#         self.head = torch.nn.Linear(config.embedding_dim, config.vocab_size)
#         with torch.no_grad():
#             self.head.weight.mul_(0.0)
#             self.head.bias.mul_(0.0)

#     def get_targets(mask, idx, T):
#         targets = idx[:,1:T+1]
#         targets = targets.masked_fill(mask[:,1:T+1] == 0, -100)
#         # print(targets)
#         return targets

#     def forward(self, idx, mask, labels, compute_loss=True):
#         """
#         idx is 1-hot encoding integer tensor shape [B, T] entries are indices into vocab
#         targets is 1-hot encoding integer tensor shape [B, T], entries are indices into vocab for labels.
#             ith entry of bth row of targets is label for ith prefix of idx in bth example in the batch.
#         """

#         # x is 1-hot encoding
#         B, T = idx.size()

#         T = min(T-1, self.config.context_length)

#         tok_embd = self.tok_embeddings(idx[:, :T])

#         pos_embd = self.pos_embeddings[:, :T, :]

#         x = F.softmax(tok_embd + pos_embd, dim=-1) # input for attention layers: shape [B, T, C]

#         features = self.features(x)

#         if not compute_loss:
#             return features

#         logits = self.head(features) # shape [B, T, V]

#         targets = labels[:, :T]#
#         # targets = StackedAttention.get_targets(mask, idx, T)

#         # cross entropy loss doesn't know about T, so we flatten the time dimension:
#         # print("logits: ", logits.shape)
#         # print("targets: ", targets.shape)

#         logits_for_CE = logits.reshape(-1, logits.size(-1)) # shape [BT, V]
#         # print(f"logits for ce shape: {logits_for_CE.size()}, original logits shape: {logits.size()}, targets shape: {targets.size()}")
#         targets_for_CE = targets.reshape(-1) # shape [BT]

#         with torch.no_grad():
#             predictions = torch.argmax(logits_for_CE, 1)
#             num_targets = torch.sum(targets_for_CE != -100)
#             num_correct = torch.sum(targets_for_CE == predictions)
#             accuracy = num_correct/num_targets

#         loss = F.cross_entropy(logits_for_CE, targets_for_CE)

#         return features, loss, accuracy

# config = ModelConfig(vocab_size=4, context_length=3, num_layers=2, embedding_dim=4, n_heads=2)

# l = StackedAttention(config)

# idx = torch.tensor([ [2, 3, 0, 1],
#                      [0, 1, 2, 3],
#                      [3, 0, 1, 2],
#                      [1, 2, 3, 0] ])

# mask = 0*torch.tensor([[1, 1, 1, 1],
#                      [1, 1, 1, 1],
#                      [1, 1, 1, 1],
#                      [1, 1, 1, 1]])

# print(l(idx, mask)[1])

# x = torch.tensor([ [[1.0,  0.0,  0.0, 0.0],
#                     [0.0,  0.0,  0.0, 1.0],
#                     [0.0,  0.5,  0.5, 0.0]],

#                    [[0.5,  0.2,  0.1, 0.2],
#                     [0.0,  1.0,  0.0, 0.0],
#                     [0.0,  0.0,  1.0, 0.0]]])
# print(x.shape)

# y = l(x)
# print(y)


from django import forms

from .models import MailingEmailAddress

class MailingEmailAddressModelForm(forms.ModelForm):
    """Form for creating a mailing email address."""

    class Meta:
        """Meta options for the MailingEmailAddressModelForm."""

        fields = ["email"]
        model = MailingEmailAddress
        widgets = {
            "email": forms.EmailInput(
                attrs={"class": "form-control", "placeholder": "Email address"}
            )
        }


import platform
import os
import inspect
import subprocess
from setuptools import setup, find_packages
from distutils.extension import Extension

from Cython.Build import cythonize

def git_version():
    def _minimal_ext_cmd(cmd):
        # construct minimal environment
        env = {}
        for k in ['SYSTEMROOT', 'PATH']:
            v = os.environ.get(k)
            if v is not None:
                env[k] = v
        # LANGUAGE is used on win32
        env['LANGUAGE'] = 'C'
        env['LANG'] = 'C'
        env['LC_ALL'] = 'C'
        out = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=env).communicate()[0]
        return out

    try:
        out = _minimal_ext_cmd(['git', 'rev-parse', 'HEAD'])
        git_revision = out.strip().decode('ascii')
    except OSError:
        git_revision = "Unknown"

    return git_revision

def get_version_info(version, is_released):
    fullversion = version
    if not is_released:
        git_revision = git_version()
        fullversion += '.dev0+' + git_revision[:7]
    return fullversion

def write_version_py(version, is_released, filename='composites/version.py'):
    fullversion = get_version_info(version, is_released)
    with open("./composites/version.py", "wb") as f:
        f.write(('__version__ = "%s"\n' % fullversion).encode())
    return fullversion

# Utility function to read the README file.
# Used for the long_description.  It's nice, because now 1) we have a top level
# README file and 2) it's easier to type in the README file than to put a raw
# string in below ...
def read(fname):
    setupdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
    return open(os.path.join(setupdir, fname)).read()

#_____________________________________________________________________________

install_requires = [
        "numpy",
        ]

CLASSIFIERS = """\

Development Status :: 5 - Production/Stable
Intended Audience :: Science/Research
Intended Audience :: Developers
Intended Audience :: Education
Intended Audience :: End Users/Desktop
Topic :: Scientific/Engineering
Topic :: Education
Topic :: Software Development
Topic :: Software Development :: Libraries :: Python Modules
Operating System :: Microsoft :: Windows
Operating System :: Unix
Operating System :: POSIX :: BSD
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
License :: OSI Approved :: BSD License

"""

is_released = True
version = '0.5.25'

fullversion = write_version_py(version, is_released)

data_files = [('', [
        'README.md',
        'LICENSE',
        'composites/version.py',
        ])]

package_data = {
        'composites': ['*.pxd', '*.pyx'],
        '': ['tests/*.*'],
        }

if platform.system() == 'Windows':
    compile_args = ['/openmp']
    link_args = []
elif platform.system() == 'Linux':
    compile_args = ['-fopenmp', '-static', '-static-libgcc', '-static-libstdc++']
    link_args = ['-fopenmp', '-static-libgcc', '-static-libstdc++']
else: # MAC-OS
    compile_args = []
    link_args = []
include_dirs = [
            ]

extensions = [
    Extension('composites.core',
        sources=[
            './composites/core.pyx',
            ],
        include_dirs=include_dirs,
        extra_compile_args=compile_args,
        extra_link_args=link_args,
        language='c++'),

    ]
ext_modules = cythonize(extensions,
        compiler_directives={'linetrace': True},
        language_level='3',
        )

s = setup(
    name = "composites",
    version = fullversion,
    author = "Saullo G. P. Castro",
    author_email = "S.G.P.Castro@tudelft.nl",
    description = ("Methods to calculate properties of laminated composite materials"),
    long_description = read('README.md'),
    long_description_content_type = 'text/markdown',
    license = "3-Clause BSD",
    keywords = "mechanics composite materials composites shell classical first-order laminated plate theory",
    url = "https://github.com/saullocastro/composites",
    package_data = package_data,
    data_files = data_files,
    classifiers = [_f for _f in CLASSIFIERS.split('\n') if _f],
    install_requires = install_requires,
    ext_modules = ext_modules,
    packages = find_packages(),
)


from kzpy3.vis3 import *
import networks.net
import Menu.main
from net_main_conv import Net_Main
exec(identify_file_str)

USE_DISCRIMINATOR = False

M = Menu.main.start_Dic(dic_project_path=pname(opjh(__file__)))

# python kzpy3/Learn/main_conv.py --main 6 --net_str conv1
# python kzpy3/Learn/main_conv.py --main 6 --net_str conv0
# python kzpy3/Learn/main_conv.py --main 6 --net_str conv1  --save_output_2 True --batch_size 1 --save_timer_time 999999 --LR 0 --runs validate --single_run Mr_Black_24Sep18_18h52m26s

"""
python kzpy3/Learn/main_conv.py --main 6 --net_str conv1  --save_output_2 True --batch_size 1 --save_timer_time 999999 --LR 0 --runs validate --single_run tegra-ubuntu_07Oct18_18h24m28s
python kzpy3/Learn/main_conv.py --main 6 --net_str conv1  --save_output_2 True --batch_size 1 --save_timer_time 999999 --LR 0 --runs validate --single_run tegra-ubuntu_25Oct18_10h21m55s
python kzpy3/Learn/main_conv.py --main 6 --net_str conv1  --save_output_2 True --batch_size 1 --save_timer_time 999999 --LR 0 --runs validate --single_run tegra-ubuntu_17Oct18_12h11m22s
python kzpy3/Learn/main_conv.py --main 6 --net_str conv1  --save_output_2 True --batch_size 1 --save_timer_time 999999 --LR 0 --runs validate --single_run tegra-ubuntu_12Oct18_11h11m30s
python kzpy3/Learn/main_conv.py --main 6 --net_str conv1  --save_output_2 True --batch_size 1 --save_timer_time 999999 --LR 0 --runs validate --single_run Mr_Black_24Sep18_18h52m26s
python kzpy3/Learn/main_conv.py --main 6 --net_str conv1  --save_output_2 True --batch_size 1 --save_timer_time 999999 --LR 0 --runs validate --single_run tegra-ubuntu_12Nov18_20h56m16s
python kzpy3/Learn/main_conv.py --main 6 --net_str conv1  --save_output_2 True --batch_size 1 --save_timer_time 999999 --LR 0 --runs validate --single_run tegra-ubuntu_01Nov18_13h46m55s
python kzpy3/Learn/main_conv.py --main 6 --net_str conv1  --save_output_2 True --batch_size 1 --save_timer_time 999999 --LR 0 --runs validate --single_run tegra-ubuntu_25Oct18_15h43m36s

stat -c %Y kzpy3/.git/FETCH_HEAD
"""

Net_strs = {

    'conv1' : """
        Learn
            --type Conv,Fire3,conv1
            --resume True
            --batch_size 64
            --save_timer_time 600
            --target_offset 0
            --input rgb
            --target outer_contours
            --losses_to_average 256
            --runs train
            --display.output 0,3
            --display.input 0,3
            --display.target 0,3
            --clip 0.1
            --backwards True
            --win_x 20
            --win_y 40
            --drop 0.0
            --blue_center_button True
            --reset_loss False
            --momentum 0.0001
            --LR 0.001
    """,

    'conv0' : """
        Learn
            --type Conv,Fire3,conv0
            --resume True
            --batch_size 64
            --save_timer_time 600
            --target_offset 0
            --input rgb
            --target outer_contours
            --losses_to_average 256
            --runs validate
            --display.output 0,3
            --display.input 0,3
            --display.target 0,3
            --clip 0.1
            --backwards True
            --win_x 20
            --win_y 40
            --drop 0.0
            --blue_center_button True
            --pts2_h5py_type h5py_angles0
            --reset_loss False
            --momentum 0.0001
            --LR 0.001
    """,
}

def main6():

    import torch
    import torch.nn as nn
    import torch.nn.parallel
    import torch.backends.cudnn as cudnn
    import torch.optim as optim
    import torch.utils.data
    import torchvision.datasets as dset
    import torchvision.transforms as transforms
    import torchvision.utils as vutils
    import torch.nn.utils as nnutils

    if USE_DISCRIMINATOR:
        from discriminator1 import Discriminator,weights_init

    if 'net_str' in Arguments.keys():
        clp('   FROM SYS_STR   ','`ybb',ra=0,p=1)
        Nets = {
            'N0':Net_Main(M=M,sys_str=Net_strs[Arguments['net_str']].replace('\n',' ').replace('\t',' '),Arguments_=Arguments),
        }
    else:
        assert(False)
        clp('   FROM COMMMAND LINE   ','`ybb',ra=0,p=1)
        Nets = {
            'N0':Net_Main(M=M,Arguments_=Arguments),
        }

    n = 'N0'
    Data = networks.net.make_batch( Nets[n]['get_data_function'], Nets[n]['P'], Nets[n]['P']['batch_size'] )
    cg('Data',shape(Data['input']),shape(Data['target']))

    if USE_DISCRIMINATOR:
        DISCRIMINATOR = Discriminator(nc=shape(Data['target'])[1]).cuda()

    if USE_DISCRIMINATOR:
        DISCRIMINATOR.apply(weights_init)

    criterion = nn.BCELoss()
    if USE_DISCRIMINATOR:
        optimizerD = optim.Adam(DISCRIMINATOR.parameters(), lr=0.01, betas=(0.5, 0.999))

    run_timer = Timer()
    freq_timer = Timer(30)

    Abort = Toggler()

    wait_timer = Timer(5)

    minute_timer = Timer(60)

    if USE_DISCRIMINATOR:
        try:
            if Nets['N0']['P']['resume']:
                DISCRIMINATOR.load(Nets['N0']['P']['NETWORK_OUTPUT_FOLDER']+'.dcgan')
        except:
            clp("*** DISCRIMINATOR.load(Nets[n]['P']['NETWORK_OUTPUT_FOLDER']+'.dcgan') failed ***",ra=1)

    graphics_on = False

    while True:

        M['load']()

        if Abort['test'](M['Q']['runtime_parameters']['abort']):
            break

        if False:#minute_timer.check():
            minute_timer.reset()
            Nets['N0']['P']['clip'] = float(Nets['N0']['P']['clip'])

            Nets['N0']['P']['clip'] *= 0.98
            clp('clip',Nets['N0']['P']['clip'],int(run_timer.time()),"`yb")

        GENERATOR = Nets[n]['N']
        Nets[n]['P']['original_Fire3_scaling'] = True

        for k in M['Q']['runtime_parameters'].keys():
            Nets[n]['P']['runtime_parameters'][k] = M['Q']['runtime_parameters'][k]

        Data = networks.net.make_batch( Nets[n]['get_data_function'], Nets[n]['P'], Nets[n]['P']['batch_size'] )

        if USE_DISCRIMINATOR:
            DISCRIMINATOR.zero_grad()
            real = Data['target'][:,:,:,:]
            label = torch.full((Nets[n]['P']['batch_size'],), 1,).cuda()
            output = DISCRIMINATOR(torch.from_numpy(real).cuda().float())

            errD_real = criterion(output, label)
            errD_real.backward()
            D_x = output.mean().item()

        GENERATOR.forward_no_loss(Data)

        if USE_DISCRIMINATOR:
            fake = GENERATOR.A['output'][:,:,:,:]
            label.fill_(0)
            output = DISCRIMINATOR(fake.detach())
            errD_fake = criterion(output, label)
            errD_fake.backward()
            D_G_z1 = output.mean().item()
            errD = errD_real + errD_fake
            optimizerD.step()

        GENERATOR.optimizer.zero_grad()

        if Nets[n]['P']['runs'] == 'validate':
            pass
            #cb('validate')

        else:

            if USE_DISCRIMINATOR:
                label.fill_(1)
                output = DISCRIMINATOR(fake)
                if 's' not in Nets[n]['P']:
                    s = 0.0001
                else:
                    s = Nets[n]['P']['s']

                GENERATOR.loss = s*GENERATOR.criterion(GENERATOR.A['output'],GENERATOR.A['target']) + (1-s) * criterion(output, label) #19

            else:
                s = 1.0
                #cm(1,ra=1)
                #cm(GENERATOR.A['output_2'].size(),GENERATOR.A['target'].size(),ra=1)
                GENERATOR.loss = s*GENERATOR.criterion(GENERATOR.A['output_2'],GENERATOR.A['target'])

            if Nets[n]['P']['backwards']:
                GENERATOR.loss.backward() #20
                nnutils.clip_grad_norm(GENERATOR.parameters(), Nets['N0']['P']['clip'])
                GENERATOR.optimizer.step() #21

            GENERATOR.losses_to_average.append(GENERATOR.extract('loss'))
            if len(GENERATOR.losses_to_average) >= GENERATOR.num_losses_to_average:
                GENERATOR.losses.append( na(GENERATOR.losses_to_average).mean() )
                GENERATOR.losses_to_average = []

            if USE_DISCRIMINATOR:
                if GENERATOR.save():
                    DISCRIMINATOR.save(Nets[n]['P']['NETWORK_OUTPUT_FOLDER']+'.dcgan')
            else:
                GENERATOR.save()

        if True:#try:

            if Nets['N0']['P']['runtime_parameters']['show_graphics']:
                if not graphics_on:
                    clp('turning graphics on')
                graphics_on = True
                Nets[n]['graphics_function'](Nets[n]['N'],M,Nets[n]['P']) # graphics can cause an error with remote login
            else:
                if graphics_on:
                    clp('turning graphics off')
                    graphics_on = False
                    CA()
        else:#except Exception as e:
            exc_type, exc_obj, exc_tb = sys.exc_info()
            file_name = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            clp('Exception!','`wrb',exc_type, file_name, exc_tb.tb_lineno)

        f = freq_timer.freq(do_print=False)
        if is_number(f):
            clp( 'Frequency =', int(np.round(f*Nets[n]['P']['batch_size'])), 'Hz, run time =',format_seconds(run_timer.time()))

def Main6_Output_Object(net_str='pro2pros'):

    D = {}

    import torch
    import torch.nn as nn
    import torch.nn.parallel
    import torch.backends.cudnn as cudnn
    import torch.optim as optim
    import torch.utils.data
    import torchvision.datasets as dset
    import torchvision.transforms as transforms
    import torchvision.utils as vutils
    import torch.nn.utils as nnutils

    from discriminator1 import Discriminator,weights_init

    if 'type' not in Arguments.keys():
        clp('   FROM SYS_STR   ','`ybb',ra=0,p=1)
        Nets = {
            'N0':Net_Main(M=M,sys_str=Net_strs[net_str].replace('\n',' ').replace('\t',' ')),
        }
    else:
        clp('   FROM COMMMAND LINE   ','`ybb',ra=0,p=1)
        Nets = {
            'N0':Net_Main(M=M,Arguments_=Arguments),
        }

    n = 'N0'

    Data = networks.net.make_batch( Nets[n]['get_data_function'], Nets[n]['P'], Nets[n]['P']['batch_size'] )
    #cg('Data',shape(Data['input']),shape(Data['target']))

    DISCRIMINATOR = Discriminator(nc=shape(Data['target'])[1]).cuda()
    DISCRIMINATOR.apply(weights_init)
    criterion = nn.BCELoss()
    optimizerD = optim.Adam(DISCRIMINATOR.parameters(), lr=0.01, betas=(0.5, 0.999))

    run_timer = Timer()
    freq_timer = Timer(30)

    Abort = Toggler()

    wait_timer = Timer(5)

    minute_timer = Timer(60)

    def _function_output(in_imgs):

        if len(in_imgs) > 0:
            in_array = in_imgs[0]
            if len(in_imgs) > 1:
                for i in range(1,len(in_imgs)):
                    in_array = np.concatenate((in_array,in_imgs[i]),axis=2)
            in_array = na([in_array.transpose(2,1,0)])

        M['load']()
        if Abort['test'](M['Q']['runtime_parameters']['abort']):
            return False#

        if minute_timer.check():
            minute_timer.reset()
            Nets['N0']['P']['clip'] = float(Nets['N0']['P']['clip'])
            a = Nets['N0']['P']['clip']
            Nets['N0']['P']['clip'] *= 0.98
            clp('clip',Nets['N0']['P']['clip'],int(run_timer.time()),"`yb")

        GENERATOR = Nets[n]['N']
        Nets[n]['P']['original_Fire3_scaling'] = True

        for k in M['Q']['runtime_parameters'].keys():

            Nets[n]['P']['runtime_parameters'][k] = M['Q']['runtime_parameters'][k]

        Data = networks.net.make_batch( Nets[n]['get_data_function'], Nets[n]['P'], Nets[n]['P']['batch_size'] )

        if len(in_imgs) > 0:
            Data['input'] = in_array

        GENERATOR.forward_no_loss(Data)

        Nets[n]['graphics_function'](Nets[n]['N'],M,Nets[n]['P']) # graphics can cause an error with remote login

        fake = GENERATOR.extract('output')
        imgs = []
        for i in range(0,shape(Data['target'])[1],3):
            imgs.append(z55(fake[i:i+3,:,:].transpose(2,1,0)))
        return imgs

    D['output'] = _function_output

    return D

do_example = False
if do_example:

    from kzpy3.Learn.main import Main6_Output_Object
    M = Main6_Output_Object('pro2pros')
    a=z55(rnd((94,168,3)))
    b = 0*a; b[:,:,0] = 1
    in_imgs = [a,b]
    imgs = M['output'](in_imgs)

    imgs = M['output']([])
    c = imgs[1]
    a = c.astype(float)
    a -= 0.
    n = 25
    a[a<0] = n
    aa = z55(a);mi(aa,'aaa')
    imgs = M['output']([aa,b])

Mains = {
    6: main6,
}

if __name__ == '__main__':

        if Arguments['main'] == 6:
            kprint(Arguments)
            clp('*** main6() ***',p=2)
            if 'net_str' not in Arguments:
                Arguments['net_str'] = ''
            Mains[6]()

#EOF


#!/usr/bin/python3
import glob
from collections import defaultdict

def MBCS_freq_to_dict(freq_files):
  MBCS_dict = {}
  for freq_file in freq_files:
    sampleID = freq_file.rsplit('/')[1].rsplit('_')[0]
    infile = open(freq_file,'r')
    MBCS_dict[sampleID] = defaultdict(float)
    for line in infile.readlines():
      if "freq" in line: continue
      line = line.rstrip().rsplit("\t")
      MBCS = line[0]
      freq = float(line[1])
      MBCS_dict[sampleID][MBCS] = freq
    infile.close()
  return MBCS_dict

def write_MBCS_freq(MBCS_list, MBCS_dict, out_filename):
  print ("writing: %s" % out_filename)
  outfile = open(out_filename,'w')
  outfile.write("\t".join(["sampleID", "MBCS", "freq"])+"\n")
  for sampleID in sorted(MBCS_dict.keys(), key=lambda x:int(x[1::])):
    for MBCS in MBCS_list:
      freq = MBCS_dict[sampleID][MBCS]
      if MBCS == "-----": MBCS = "deletion"
      outfile.write("\t".join(map(str,[sampleID, MBCS, freq]))+"\n")
  outfile.close()

def main():
  freq_files = glob.glob("Cov2_fastq/*/MBCS_freq.tsv")
  out_filename = "results/all_MBCS_freq.tsv"
  MBCS_dict = MBCS_freq_to_dict(freq_files)
  MBCS_list = []
  [MBCS_list.extend(MBCS_dict[sampleID].keys()) for sampleID in MBCS_dict.keys()]
  MBCS_list = list(set(MBCS_list))
  write_MBCS_freq(MBCS_list, MBCS_dict, out_filename)

if __name__ == "__main__":
    main()


import time
import numpy as np
import matplotlib.pyplot as plt
import math
import sympy
from timer import timed
import random

def isPrime(n):
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False

    return True
def corseltCriterion(n):
    for i in range(2, n - 1):
        if n % i == 0:
            if i - 1 % n - 1 != 0 and isPrime(i) and i^2 % n == 0:
                return False
    return True
@timed
def SieveOfEratosthenes(n):
    list = []
    prime = [True for i in range(n + 1)]
    p = 2
    while (p * p <= n):
        if prime[p] == True:
            for i in range(p * p, n + 1, p):
                prime[i] = False
        p += 1
    for p in range(2, n + 1):
        if prime[p]:
            list.append(p)
    return list
@timed
def findPrimeWithFerm(n):
    b = 3
    while b < n:
        if np.gcd(b, n) == 1:
            if pow(b, n - 1, n) != 1:
                return 0
        b = b + 1
    return 1
def generatePrimeWithFerm(num):
    list = []
    for i in range(3, num):
        if findPrimeWithFerm(i) == 1:
            list.append(i)
    return list
@timed
def findPrimeWithMillerRandUnop(num, k):
    num2 = num - 1
    pow2 = 0
    while num2 % 2 == 0:
        num2 = int(num2/2)
        pow2 = pow2 + 1
    for i in range(k):
        a = random.randint(2, num - 2)
        x = pow(a, num2, num)
        y = 0
        for i in range(pow2):
            y = pow(x, 2, num)
            if y == 1 and (x != 1) and x != (num - 1):
                return 0
            x = y
        if (y != 1):
            return 0
    return 1
def generatePrimeWithMillerRandUnop(n, k):
    list = []
    for i in range(4, n):
        if findPrimeWithMillerRandUnop(i, k) == 1:
            list.append(i)
    return list
def isCarm(n):
    b = 2
    while b < n:
        if np.gcd(b, n) == 1:
            if pow(b, n - 1, n) != 1:
                return 0
        b = b + 1
    if not isPrime(b):
        return 1
    else:
        return 2
def carmList(num):
    returnable = set()
    for i in range(num):
        if isCarm(i) == 1:
            returnable.add(i)
            print(i)
    return returnable
def getCoordinate(num):
    return num*np.cos(num), num*np.sin(num)
def create_plot(nums, times):
    plt.style.use("dark_background")
def getAccuracy(nums, k):
    primeList = set(sympy.primerange(5, k))
    return primeList.difference(set(nums))
def main():
    timeForMiller = []
    timeForFermat = []
    timeForSieve = []
    start = 10000000
    end = 10000000000
    for c in range(5):
        g = random.randint(start, end)
        start1 = time.time()
        SieveOfEratosthenes(g)
        end1 = time.time()
        timeForSieve.append(end1 - start1)
        start2 = time.time()
        findPrimeWithFerm(g)
        end2 = time.time()
        timeForFermat.append(end2 - start2)
        start3 = time.time()
        findPrimeWithMillerRandUnop(g, 5)
        end3 = time.time()
        timeForMiller.append(end3 - start3)
        start = start + end
        end = 2 * end
    plt.figure().canvas.manager.set_window_title("Prime Generating Algorithms - Time Complexity")
    plt.xlabel("No. of Numbers")
    plt.ylabel("Execution Time (ms)")
    plt.plot([i for i in range(len(timeForSieve))], [1000 * i for i in timeForSieve], label="Time For Sieve")
    plt.plot([i for i in range(len(timeForFermat))], [1000 * i for i in timeForFermat], label="Time For Fermat")
    plt.plot([i for i in range(len(timeForMiller))], [1000 * i for i in timeForMiller], label="Time For Miller-Rand")
    plt.legend()
    plt.tight_layout()
    plt.show()

if __name__ == "__main__":
    main()


from sklearn import datasets
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
from collections import Counter
from sklearn import metrics
import matplotlib.pyplot as plt

def KNN(Test_input, X_train, y_train, K):
    #Calculate dist. due to Test_input
    dist_train = np.zeros(X_train.shape[0], dtype=float)
    for i in range(0, X_train.shape[0]):
        dist_train[i] = np.linalg.norm(Test_input - X_train[i])

    Train_set_with_dist = np.concatenate((X_train, y_train[:, None], dist_train[:, None]), axis = 1)
    Sorting_Train_set = Train_set_with_dist[Train_set_with_dist[:,5].argsort()]

    Train_set_with_sort = Sorting_Train_set[:, 0 : -1]
    #Take out Top K entry as neighbor
    Neighbor_set = Train_set_with_sort[0 : K]
    A = Neighbor_set[:,-1].tolist()
    c = Counter(Neighbor_set[:,-1].tolist())
    value, count = c.most_common()[0]
    return value

#Load data
iris_dataset = datasets.load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris_dataset['data'], iris_dataset['target'], test_size = 0.5, random_state = 0)

predicted_y_train = np.zeros((X_train.shape[0], 1), dtype = float)
predicted_y_test  = np.zeros((X_test.shape[0], 1), dtype = float)

train_error_array = np.zeros((20,1), dtype = float)
test_error_array = np.zeros((20, 1), dtype = float)
for i in range(0 , 20):
    train_error_point = 0.
    test_error_point = 0.

    for j in range(0, X_train.shape[0]):
        predicted_y_train[j] = KNN(X_train[j], X_train, y_train, i + 1)
        predicted_y_test[j] =  KNN(X_test[j], X_train, y_train, i + 1)

    train_error = 1 - metrics.accuracy_score(y_train, predicted_y_train)
    test_error = 1 - metrics.accuracy_score(y_test, predicted_y_test)
    print("K : {0:d} Train_Error: {1:.2f} Test_Error: {2:.2f}".format(i + 1, train_error, test_error))
    train_error_array[i] = train_error
    test_error_array[i] = test_error

y =  np.concatenate((train_error_array, test_error_array), axis = 1)
# Draw the outcome
plots = plt.plot(range(1, 21), y)
plt.legend(plots, ('Train_Error', 'Test_Error'), loc='best', framealpha=0.5, prop={'size': 'large', 'family': 'monospace'})
plt.show()


from __future__ import print_function
import sys
import os
from argparse import ArgumentParser

# Append OpenVINO python path
sys.path.append("C:\Intel\computer_vision_sdk_2018.5.456\python\python3.6")
import cv2
import numpy as np
import time
from openvino.inference_engine import IENetwork, IEPlugin

# Input arguments
def build_argparser():
    parser = ArgumentParser()
    parser.add_argument("-m", "--model", help="Path to an .xml file with a trained model.", required=True, type=str)
    parser.add_argument("-i", "--input", help="Path to a folder with images or path to an image files", required=True,
                        type=str)
    parser.add_argument("-l", "--cpu_extension",
                        help="MKLDNN (CPU)-targeted custom layers.Absolute path to a shared library with the kernels "
                             "impl.", type=str, default=None)
    parser.add_argument("-pp", "--plugin_dir", help="Path to a plugin folder", type=str, default=None)
    parser.add_argument("-d", "--device",
                        help="Specify the target device to infer on; CPU, GPU, FPGA or MYRIAD is acceptable. Sample "
                             "will look for a suitable plugin for device specified (CPU by default)", default="CPU",
                        type=str)
    parser.add_argument("-nt", "--number_top", help="Number of top results", default=10, type=int)
    parser.add_argument("-thresh", "--threshold", help="Confidence threshold for classification", default=0.3, type=float)
    return parser

def main():
	# Get the input arguments
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    model_threshold = args.threshold

    # Plugin initialization for specified device and load extensions library if specified
    plugin = IEPlugin(device=args.device, plugin_dirs=args.plugin_dir)
    if args.cpu_extension and 'CPU' in args.device:
        plugin.add_cpu_extension(args.cpu_extension)
    # Read IR
    net = IENetwork.from_ir(model=model_xml, weights=model_bin)
    assert len(net.inputs.keys()) == 1, "Sample supports only single input topologies"
    assert len(net.outputs) == 1, "Sample supports only single output topologies"
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
	#Loading IR to the plugin
    print("Loading IR to the plugin...")
    exec_net = plugin.load(network=net, num_requests=2)

    # Read and pre-process input image
    n, c, h, w = net.inputs[input_blob].shape

    del net

    if args.input == 'cam':
        input_stream = 0
    else:
        input_stream = args.input
        assert os.path.isfile(args.input), "Specified input file doesn't exist"
	# get the input video stream
    cap = cv2.VideoCapture(input_stream)

    cur_request_id = 0
    next_request_id = 1

    print("Starting inference in async mode...")
    print("To switch between sync and async modes press Tab button")
    print("To stop the sample execution press Esc button")
	# setting async mode
    is_async_mode = False
    render_time = 0
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        initial_w = cap.get(3)
        initial_h = cap.get(4)
        in_frame = cv2.resize(frame, (w, h))
        in_frame = in_frame.transpose((2, 0, 1))  # Change data layout from HWC to CHW
        in_frame = in_frame.reshape((n, c, h, w))

        # Main sync point:
        # in the truly Async mode we start the NEXT infer request, while waiting for the CURRENT to complete
        # in the regular mode we start the CURRENT request and immediately wait for it's completion
        inf_start = time.time()
        if is_async_mode:
            exec_net.start_async(request_id=next_request_id, inputs={input_blob: in_frame})
        else:
            exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame})
        if exec_net.requests[cur_request_id].wait(-1) == 0:
            inf_end = time.time()
            det_time = inf_end - inf_start

            # Parse detection results of the current request
            res = exec_net.infer(inputs={input_blob: in_frame})
            #res = exec_net.requests[cur_request_id].outputs[out_blob]
            if (res != ""):
                top_ind = np.argsort(res[out_blob], axis=1)[0, -args.number_top:][::-1]
                outLabel = ""
                labelsEnabled = False
				# fetch the file name of labels
                labelFileName = os.path.splitext(model_xml)[0] + ".labels"
                labels = []
				# append the labels to a list named labels
                with open(labelFileName,"r") as inputFile:
                    for strLine in inputFile:
                        strLine.strip()
                        strLine.splitlines()
                        labels.append(strLine)
                    labelsEnabled = True
				#comparing with the threshold
                if (res[out_blob][0, top_ind[0][0][0]][0][0] > model_threshold) :
                    cv2.putText(frame, str(labels[top_ind[0][0][0]]) , (15, 30), cv2.FONT_HERSHEY_COMPLEX, 0.6, (255,235,205), 2)

                #cv2.putText(frame, str(res[out_blob][0, top_ind[0][0][0]][0][0]) + str(labels[top_ind[0][0][0]]) , (15, 30), cv2.FONT_HERSHEY_COMPLEX, 0.6, (10, 10, 200), 1)

        render_start = time.time()
		# Show the output in a popup window, together with the label
        cv2.imshow("Detection Results", frame)
        render_end = time.time()
		#calculat the render time using starting and current time
        render_time = render_end - render_start

        key = cv2.waitKey(1)
        if key == 27:
            break
        if (9 == key):
            is_async_mode = not is_async_mode
            print("Switched to {} mode".format("async" if is_async_mode else "sync"))
        if is_async_mode:
            cur_request_id, next_request_id = next_request_id, cur_request_id

    cv2.destroyAllWindows()
    del exec_net
    del plugin

if __name__ == '__main__':
    sys.exit(main() or 0)


import sqlalchemy
from sqlalchemy import create_engine, inspect, MetaData
from sqlalchemy.orm import Session
import os, sys
import oracledb

from pathlib import Path

running_at = Path(__file__)
project_dir = running_at.parent.parent.parent
sys.path.append(str(project_dir))
os.chdir(str(project_dir))  # so admin app can find images, code

import database.models as models

# *******************************************
# Use this to inspect SQLAlchemy2 connection
# *******************************************

def print_meta(meta):
    print("\n\nmeta.sorted_tables (meta = models.metadata.sorted_tables)\n")
    for each_table in meta.sorted_tables:
        print(f'\n{each_table.key}')
        for each_column in each_table.columns:
            print(f'\t{each_column.name}')

db_loc = str(project_dir.joinpath('database/db.sqlite'))
db_url = "sqlite:////Users/val/dev/servers/ApiLogicProject/database/db.sqlite"
db_url = f"sqlite:///{db_loc}"
# db_url = "sqlite:////Users/val/dev/servers/ApiLogicProject/database/nw-gold.sqlite"
from config import Config
db_url = Config.SQLALCHEMY_DATABASE_URI

if 'oracle' in db_url:
    oracle_thick = False
    if oracle_thick:
        import oracledb
        oracledb.init_oracle_client()

print(f'\n Attempting connect with:\n{db_url}\n')

e = sqlalchemy.create_engine(db_url)
inspector = inspect(e)
conn = e.connect()

with Session(e) as session:
    print(f'session: {session}')
    # metadata = MetaData(bind=e)
    metadata = MetaData()  # SQLAlchemy2
    meta = models.metadata  # tables should show list of db tables
    print_meta(meta)
    print("\n\n")

    is_northwind = False
    if is_northwind:
        """
        Caution - disable / delete this code if your database is not Northwind

        Caution - use of debugger may result in "no app context"
        https://stackoverflow.com/questions/34122949/working-outside-of-application-context-flask
        """
        order_id = 10643
        order : models.Order = session.query(models.Order).filter(models.Order.Id == order_id).one()
        # hover to view code completion...
        print(f'Order: {order.Id}, AmountTotal: {order.AmountTotal}, ready: {order.Ready}, Required: {order.RequiredDate}, Customer: {order.CustomerId}, Balance: {order.Customer.Balance}\n\n')
        order.AmountTotal

        departments = session.query(models.Department).all()
        for each_dept in departments:
            print(f'\nDept: {each_dept}')
            for each_works_for_emp in each_dept.EmployeeList1:
                print(f'...each_works_for_emp: {each_works_for_emp}')
            print("")
            for each_on_loan_emp in each_dept.EmployeeList:
                print(f'...each_on_loan_emp: {each_on_loan_emp}')
            print("")
            for each_sub_dept in each_dept.DepartmentList:
                print(f'...each_sub_dept: {each_sub_dept}')


from cas.common.models import BuildResult, BuildSubsystem
from pathlib import Path

import sys
import shutil
import importlib.util
import contextlib

class FGDBuildSubsystem(BuildSubsystem):
    """
    Subsystem that builds FGDs and Hammer assets from HammerAddons
    """

    def build(self, force: bool = False) -> BuildResult:
        project = self.env.config.options.project

        srcpath = Path(self.env.root).joinpath(self.config.source).resolve()
        destpath = Path(self.env.root).joinpath(self.config.dest).resolve()

        spec = importlib.util.spec_from_file_location(
            "hammeraddons.unifyfgd", srcpath.joinpath("unify_fgd.py")
        )
        mod = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(mod)

        build_dest = srcpath.joinpath("build")
        if not build_dest.exists():
            build_dest.mkdir()

        outfile = build_dest.joinpath(f"{project}.fgd")

        # redirect our output so logs aren't spammed on non-verbose mode
        log_dev = sys.stdout if self.env.verbose else None
        with contextlib.redirect_stdout(log_dev):
            mod.action_export(
                srcpath.joinpath("fgd"),
                None,
                frozenset({"SRCTOOLS", self.config.branch}),
                outfile,
                False,
                False,
            )

        hammer_dir = destpath.joinpath("hammer")

        shutil.copy(outfile, hammer_dir.joinpath("cfg", f"{project}.fgd"))

        self.override_folder(
            srcpath.joinpath("hammer", "materials"), hammer_dir.joinpath("materials")
        )
        self.override_folder(
            srcpath.joinpath("hammer", "models"), hammer_dir.joinpath("models")
        )
        self.override_folder(
            srcpath.joinpath("hammer", "scripts"), hammer_dir.joinpath("scripts")
        )

        return BuildResult(True)

    def clean(self) -> bool:
        return True

    def override_folder(self, src, dest):
        if dest.exists():
            shutil.rmtree(dest)
        shutil.copytree(src, dest)

_subsystem = FGDBuildSubsystem


"""
Pydantic Model for Grade API's
"""
from typing import Optional
from pydantic import BaseModel
from schemas.schema_examples import LTI_POST_GRADE_MODEL

class PostGradeModel(BaseModel):
  """Post Grade Pydantic Model

  Args:
      BaseModel (_type_): _description_
  """
  user_id: str
  lti_content_item_id: str
  comment: Optional[str]
  maximum_grade: Optional[float] = None
  assigned_grade: Optional[float] = None
  draft_grade: Optional[float] = None
  validate_title: Optional[bool] = False
  line_item_title: Optional[str] = None

  class Config():
    "Pydantic Config Class"
    orm_mode = True
    schema_extra = {"example": LTI_POST_GRADE_MODEL}

class PostGradeResponseModel(BaseModel):
  """Post Grade List Response model"""
  success: Optional[bool] = True
  message: Optional[str] = "Successfully updated the user grade in classroom"
  data: Optional[str] = None

  class Config():
    orm_mode = True
    schema_extra = {
        "example": {
            "success": True,
            "message": "Successfully updated the user grade in classroom",
            "data": None
        }
    }


"""This script is used for controlling the debug console
send command to board to bring up the wifi connection
Version 2: use OOB
"""
import serial
import time
import re

class SerialCom:
    def __init__(self, comport="COM1"):
        self.comport = comport
        self.ser = serial.Serial()
        self.ser.port = self.comport

        # 115200,N,8,1
        self.ser.baudrate = 115200
        self.ser.bytesize = serial.EIGHTBITS  # number of bits per bytes
        self.ser.parity = serial.PARITY_NONE  # set parity check
        self.ser.stopbits = serial.STOPBITS_ONE  # number of stop bits
        self.ser.timeout = 0.5
        self.ser.writeTimeout = 0.5
        self.ser.xonxoff = False  # disable software flow control
        self.ser.rtscts = False  # disable hardware (RTS/CTS) flow control
        self.ser.dsrdtr = False  # disable hardware (DSR/DTR) flow control

    def cmdsend(self, command):
        self.command = command
        self.ser.write(self.command.encode('UTF-8'))

    def cmdsenddly(self, command, secs=0.5):
        self.command = command
        self.secs = secs
        self.ser.write(self.command.encode('UTF-8'))
        time.sleep(self.secs)

    def cmdread(self, waitstring="\r\n"):
        self.waitstring = waitstring
        response = self.ser.read_until(self.waitstring).decode('UTF-8')
        lastline = waitstring
        fmtresponse = response.strip(lastline)
        return fmtresponse

def msg_readuntil(desiremsg, secs = 0.5):
    global serctrl

    testmsg = serctrl.cmdread()
    while desiremsg not in testmsg:
        print(testmsg)
        testmsg = serctrl.cmdread()
        time.sleep(secs)
    print(testmsg)

loginprompt = "login:"
inprompt = "root@"
Target_SSID = "Synology_520HMX_5G"
Wifipasskey = "82273585"
waitstring = "~# "

# iperf test parameter
IperfServ = "10.88.88.147"

serctrl = SerialCom("COM5")

macidRegex = re.compile('([a-fA-F]|\d){12}')
wifiphraseRegex = re.compile('wifi_\w{12}_\w.+_managed_psk')
ipaddrRegex = re.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")

print("Test starts ...")

try:
    serctrl.ser.open()
except Exception as ex:
    print("open serial port error " + str(ex))
    exit()

if serctrl.ser.isOpen():

    try:
        serctrl.ser.reset_input_buffer()  # flush input buffer
        serctrl.ser.reset_output_buffer()  # flush output buffer

        serdata = ""
        count = 0
        while inprompt not in serdata:
            if count <= 30:
                serdata = serctrl.cmdread()
                if serdata != "":
                    print(serdata)
                else:
                    print(serdata, end="")

            if loginprompt in serdata:
                # serctrl.ser.write('root\n'.encode('UTF-8'))
                serctrl.cmdsend('root\n')
                serdata = serctrl.cmdread(waitstring)
                print("root account is log in")
                # print(serdata)

            time.sleep(0.5)
            count += 1

            if count > 30:
                serctrl.cmdsend('\n')
                serdata = serctrl.cmdread()

        time.sleep(5)
        serctrl.ser.reset_input_buffer()
        serctrl.ser.reset_output_buffer()

        # check if the wlan0 ip is up
        for i in range(3):
            serctrl.cmdsend("ifconfig wlan0 | grep 'inet addr'\n")
            ipinfo = serctrl.cmdread(waitstring)
            time.sleep(5)
            print("%s : check wlan info" %str(i+1))
            if ipaddrRegex.search(ipinfo):
                break
            else:
                pass
                print("wlan info check failed")

        if ipaddrRegex.search(ipinfo):
            print(ipaddrRegex.search(ipinfo).group())
            print("The wlan connection is up")

        else:
            serctrl.cmdsenddly("connmanctl\n", 0.5)
            reponse = serctrl.cmdread(">")
            print(reponse)
            serctrl.cmdsenddly('enable wifi\n', 0.5)
            serctrl.cmdsenddly('scan wifi\n', 2)
            serctrl.cmdsenddly('exit\n', 0.5)
            serctrl.ser.flush()
            reponse = serctrl.cmdread()
            print(reponse)

            # shell
            serctrl.cmdsenddly("connmanctl services > /tmp/wifiservices\n", 0.5)
            serctrl.cmdsend("sync\n")
            serctrl.cmdsenddly("cat /tmp/wifiservices | grep ", 0.5)
            serctrl.cmdsenddly(Target_SSID, 0.5)
            serctrl.cmdsenddly(" | grep 001\n", 0.5)
            wifisvs = serctrl.cmdread(waitstring)
            time.sleep(1)

            # print(wifisvs)
            wifikey = wifiphraseRegex.search(wifisvs).group()
            # print(wifikey)

            # connmanctl shell
            serctrl.cmdsenddly('connmanctl\n', 1)
            # connmanctl shell
            serctrl.cmdsenddly('agent on\n', 1)
            serctrl.cmdsenddly('connect ', 0.5)
            serctrl.cmdsenddly(wifikey, 0.5)
            serctrl.cmdsend('\n')
            # enter Passphrase
            connreact = serctrl.cmdread('>')
            if "Already connected" in connreact or "wlan0: link becomes ready" in connreact:
                pass
            else:
                serctrl.cmdsend(Wifipasskey)
                serctrl.cmdsenddly('\n', 3)
                print(serctrl.cmdread())
                time.sleep(3)

            serctrl.cmdsend('state\n')
            print(serctrl.cmdread())
            time.sleep(1)
            serctrl.cmdsend('exit\n')
            print(serctrl.cmdread(waitstring))
            print("wifi connection completed")
            serctrl.cmdsenddly("\n", 2)

        # iperf test
        print("Iperf TX - Upload test")
        serctrl.cmdsend("iperf3 -c ")
        serctrl.cmdsend(IperfServ)
        serctrl.cmdsenddly(" -t 10 -i 2 -p 5201", 0.5)
        serctrl.cmdsend("\n")

        msg_readuntil("iperf Done.", 2)

        print("Iperf RX - Download test")
        serctrl.cmdsend("iperf3 -c ")
        serctrl.cmdsend(IperfServ)
        serctrl.cmdsenddly(" -t 10 -i 2 -p 5202 -R", 0.5)
        serctrl.cmdsend("\n")

        msg_readuntil("iperf Done.", 2)

        print("Iperf BI-direction test starting ...")
        serctrl.cmdsend("iperf3 -c ")
        serctrl.cmdsend(IperfServ)
        serctrl.cmdsenddly(" -t 10 -i 2 -p 5201&", 0.5)
        serctrl.cmdsend("\n")
        serctrl.cmdsend("iperf3 -c ")
        serctrl.cmdsend(IperfServ)
        serctrl.cmdsenddly(" -t 10 -i 2 -p 5202 -R&", 0.5)
        serctrl.cmdsend("\n")

        msg_readuntil("iperf Done.", 2)

        print("iperf test completed")

        # close console
        serctrl.ser.close()
        print("the comport is closed")

    except Exception as e1:
        print("communicating error , the port is occupied" + str(e1))
        serctrl.ser.close()
        print("the comport is closed")

else:
    print("open serial port error")


from rest_framework.serializers import ModelSerializer

from events.models import EventItem
from events.serializers import (EventItemGroupSerializer, TransportationPickupLocationSerializer,
                                AccommodationRoomSerializer)
from items.serializers import ItemMasterSerializer

class EventItemSerializer(ModelSerializer):
    class Meta:
        model = EventItem

        exclude = ('uuid', 'event', 'items_booked', 'deleted_at')

    def to_representation(self, instance):
        response = super().to_representation(instance)
        data = {
            'uuid': instance.uuid,
            'early_bird_rate': instance.get_early_bird_rate(),
            'late_bird_rate': instance.get_late_bird_rate(),
            'current_rate': instance.get_current_rate(),
            'items_booked': instance.items_booked,

            'item_master': ItemMasterSerializer(instance.item_master).data,
            'group': EventItemGroupSerializer(instance.group).data,

            'transportation_pickup_locations': TransportationPickupLocationSerializer(
                instance.transportation_pickup_locations, many=True).data,
            'accommodation_rooms': AccommodationRoomSerializer(
                instance.accommodation_rooms, many=True).data
        }
        response.update(data)
        return response


# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>
#         Gael Varoquaux <gael.varoquaux@normalesup.org>
#         Virgile Fritsch <virgile.fritsch@inria.fr>
#
# License: BSD 3 clause

import itertools

import numpy as np
import pytest

from sklearn import datasets
from sklearn.covariance import MinCovDet, empirical_covariance, fast_mcd
from sklearn.utils._testing import assert_array_almost_equal

X = datasets.load_iris().data
X_1d = X[:, 0]
n_samples, n_features = X.shape

def test_mcd(global_random_seed):
    # Tests the FastMCD algorithm implementation
    # Small data set
    # test without outliers (random independent normal data)
    launch_mcd_on_dataset(100, 5, 0, 0.02, 0.1, 75, global_random_seed)
    # test with a contaminated data set (medium contamination)
    launch_mcd_on_dataset(100, 5, 20, 0.3, 0.3, 65, global_random_seed)
    # test with a contaminated data set (strong contamination)
    launch_mcd_on_dataset(100, 5, 40, 0.1, 0.1, 50, global_random_seed)

    # Medium data set
    launch_mcd_on_dataset(1000, 5, 450, 0.1, 0.1, 540, global_random_seed)

    # Large data set
    launch_mcd_on_dataset(1700, 5, 800, 0.1, 0.1, 870, global_random_seed)

    # 1D data set
    launch_mcd_on_dataset(500, 1, 100, 0.02, 0.02, 350, global_random_seed)

def test_fast_mcd_on_invalid_input():
    X = np.arange(100)
    msg = "Expected 2D array, got 1D array instead"
    with pytest.raises(ValueError, match=msg):
        fast_mcd(X)

def test_mcd_class_on_invalid_input():
    X = np.arange(100)
    mcd = MinCovDet()
    msg = "Expected 2D array, got 1D array instead"
    with pytest.raises(ValueError, match=msg):
        mcd.fit(X)

def launch_mcd_on_dataset(
    n_samples, n_features, n_outliers, tol_loc, tol_cov, tol_support, seed
):
    rand_gen = np.random.RandomState(seed)
    data = rand_gen.randn(n_samples, n_features)
    # add some outliers
    outliers_index = rand_gen.permutation(n_samples)[:n_outliers]
    outliers_offset = 10.0 * (rand_gen.randint(2, size=(n_outliers, n_features)) - 0.5)
    data[outliers_index] += outliers_offset
    inliers_mask = np.ones(n_samples).astype(bool)
    inliers_mask[outliers_index] = False

    pure_data = data[inliers_mask]
    # compute MCD by fitting an object
    mcd_fit = MinCovDet(random_state=seed).fit(data)
    T = mcd_fit.location_
    S = mcd_fit.covariance_
    H = mcd_fit.support_
    # compare with the estimates learnt from the inliers
    error_location = np.mean((pure_data.mean(0) - T) ** 2)
    assert error_location < tol_loc
    error_cov = np.mean((empirical_covariance(pure_data) - S) ** 2)
    assert error_cov < tol_cov
    assert np.sum(H) >= tol_support
    assert_array_almost_equal(mcd_fit.mahalanobis(data), mcd_fit.dist_)

def test_mcd_issue1127():
    # Check that the code does not break with X.shape = (3, 1)
    # (i.e. n_support = n_samples)
    rnd = np.random.RandomState(0)
    X = rnd.normal(size=(3, 1))
    mcd = MinCovDet()
    mcd.fit(X)

def test_mcd_issue3367(global_random_seed):
    # Check that MCD completes when the covariance matrix is singular
    # i.e. one of the rows and columns are all zeros
    rand_gen = np.random.RandomState(global_random_seed)

    # Think of these as the values for X and Y -> 10 values between -5 and 5
    data_values = np.linspace(-5, 5, 10).tolist()
    # Get the cartesian product of all possible coordinate pairs from above set
    data = np.array(list(itertools.product(data_values, data_values)))

    # Add a third column that's all zeros to make our data a set of point
    # within a plane, which means that the covariance matrix will be singular
    data = np.hstack((data, np.zeros((data.shape[0], 1))))

    # The below line of code should raise an exception if the covariance matrix
    # is singular. As a further test, since we have points in XYZ, the
    # principle components (Eigenvectors) of these directly relate to the
    # geometry of the points. Since it's a plane, we should be able to test
    # that the Eigenvector that corresponds to the smallest Eigenvalue is the
    # plane normal, specifically [0, 0, 1], since everything is in the XY plane
    # (as I've set it up above). To do this one would start by:
    #
    #     evals, evecs = np.linalg.eigh(mcd_fit.covariance_)
    #     normal = evecs[:, np.argmin(evals)]
    #
    # After which we need to assert that our `normal` is equal to [0, 0, 1].
    # Do note that there is floating point error associated with this, so it's
    # best to subtract the two and then compare some small tolerance (e.g.
    # 1e-12).
    MinCovDet(random_state=rand_gen).fit(data)

def test_mcd_support_covariance_is_zero():
    # Check that MCD returns a ValueError with informative message when the
    # covariance of the support data is equal to 0.
    X_1 = np.array([0.5, 0.1, 0.1, 0.1, 0.957, 0.1, 0.1, 0.1, 0.4285, 0.1])
    X_1 = X_1.reshape(-1, 1)
    X_2 = np.array([0.5, 0.3, 0.3, 0.3, 0.957, 0.3, 0.3, 0.3, 0.4285, 0.3])
    X_2 = X_2.reshape(-1, 1)
    msg = (
        "The covariance matrix of the support data is equal to 0, try to "
        "increase support_fraction"
    )
    for X in [X_1, X_2]:
        with pytest.raises(ValueError, match=msg):
            MinCovDet().fit(X)

def test_mcd_increasing_det_warning(global_random_seed):
    # Check that a warning is raised if we observe increasing determinants
    # during the c_step. In theory the sequence of determinants should be
    # decreasing. Increasing determinants are likely due to ill-conditioned
    # covariance matrices that result in poor precision matrices.

    X = [
        [5.1, 3.5, 1.4, 0.2],
        [4.9, 3.0, 1.4, 0.2],
        [4.7, 3.2, 1.3, 0.2],
        [4.6, 3.1, 1.5, 0.2],
        [5.0, 3.6, 1.4, 0.2],
        [4.6, 3.4, 1.4, 0.3],
        [5.0, 3.4, 1.5, 0.2],
        [4.4, 2.9, 1.4, 0.2],
        [4.9, 3.1, 1.5, 0.1],
        [5.4, 3.7, 1.5, 0.2],
        [4.8, 3.4, 1.6, 0.2],
        [4.8, 3.0, 1.4, 0.1],
        [4.3, 3.0, 1.1, 0.1],
        [5.1, 3.5, 1.4, 0.3],
        [5.7, 3.8, 1.7, 0.3],
        [5.4, 3.4, 1.7, 0.2],
        [4.6, 3.6, 1.0, 0.2],
        [5.0, 3.0, 1.6, 0.2],
        [5.2, 3.5, 1.5, 0.2],
    ]

    mcd = MinCovDet(support_fraction=0.5, random_state=global_random_seed)
    warn_msg = "Determinant has increased"
    with pytest.warns(RuntimeWarning, match=warn_msg):
        mcd.fit(X)


#!/usr/bin/python3

import sys
from pyquery import PyQuery as pq

if len(sys.argv) == 2:

    contest_url  = sys.argv[1]
    print("Scrapping contest from %s\n" % contest_url)
    dom = pq(url = contest_url)
    contest_name = dom('table.rtable > tbody > tr > th').text()
    n = dom('table.problems > tr').length

    with open('.contest_metadata', 'w') as out:

        out.write("Contest: %s\n"  % contest_name)
        out.write("URL: %s\n"      % contest_url)
        out.write("Problems: %d\n" % n)

        for problem in range(1, n):
            id   = dom('table.problems > tr').eq(problem).filter('tr > td').eq(0).text()
            name = dom('table.problems > tr').eq(problem).filter('tr > td').eq(1).filter('td > div > div > a').text()
            out.write("    %s - %s\n"% (id, name))
else:
    print("USAGE: %s <CONTEST_URL>" % sys.argv[0])


from django.conf.urls import url

from . import views

urlpatterns=[ url(r'^$',views.index,name='index'),
			url(r'^(?P<question_id>[0-9]+)/$',views.detail,name='detail'),
			url(r'^stats/(?P<question_id>[0-9]+)$',views.stats,name='stats'),
			url(r'^addstats/(?P<player_id>[0-9]+)$',views.addstats,name='addstats'),
			url(r'^addplayer/$',views.addplayer,name='addplayer')
			]


import torch
import torch.nn.functional as F
import math
import Bio.PDB
from Bio.PDB.DSSP import DSSP
import numpy as np
from torch_geometric.data import Data
from torch_geometric.nn import radius_graph
from aminoacids import aa_charge, ss_mapping, aa_1_mapping, aa_polar

class ProteinGraphBuilder:
    def __init__(self, max_distance=10, max_num_neighbors=128):
        self.max_distance = max_distance
        self.radius_graph = RadiusGraph(max_distance,
                                        loop=False,
                                        max_num_neighbors=max_num_neighbors)

    def build_graph(self, id: str,
                    pdb_file: str,
                    amd: int = -1,
                    label: int = -1, ) -> Data:
        parser = Bio.PDB.PDBParser()
        structure = parser.get_structure(id, pdb_file)

        model = next(structure.get_models())
        chain = list(next(model.get_chains()))
        seq_length = len(chain)

        aa = np.full((seq_length, 1), dtype=np.float32, fill_value=np.nan)
        ss = np.full((seq_length, 1), dtype=np.float32, fill_value=np.nan)

        ca_coordinates = np.full((seq_length, 3), dtype=np.float32, fill_value=np.nan)
        c_coordinates = np.full((seq_length, 3), dtype=np.float32, fill_value=np.nan)
        n_coordinates = np.full((seq_length, 3), dtype=np.float32, fill_value=np.nan)

        sequence = ""

        dssp_feats = DSSP(model, pdb_file, dssp='mkdssp')
        aa_keys = list(dssp_feats.keys())

        for idx in range(seq_length):
            feats = dssp_feats[aa_keys[idx]]

            ca_coordinates[idx] = chain[idx]['CA'].get_vector()[:3]
            c_coordinates[idx] = chain[idx]['C'].get_vector()[:3]
            n_coordinates[idx] = chain[idx]['N'].get_vector()[:3]

            sequence += feats[1]
            aa[idx] = aa_1_mapping[feats[1]]
            ss[idx] = ss_mapping[feats[2]]

        forward_vec, backward_vec = orientations(ca_cords=ca_coordinates)

        side_chain_vec = side_chains(origin=ca_coordinates,
                                     n=n_coordinates,
                                     c=c_coordinates)

        angles = dihedral_angles(np.stack([n_coordinates,
                                           ca_coordinates,
                                           c_coordinates], 1))

        node_v = np.concatenate([forward_vec,
                                 backward_vec,
                                 side_chain_vec], axis=1)

        out = Data(
            id=id,
            seq=sequence,
            ss=torch.from_numpy(ss).long(),
            aa=torch.from_numpy(aa).long(),
            amd=torch.tensor(amd).to(torch.float32),
            x=angles.to(torch.float32),
            v=torch.from_numpy(node_v.reshape((node_v.shape[0], node_v.shape[1] // 3, 3))).to(torch.float32),
            y=torch.tensor(label).to(torch.float32),
            pos=torch.from_numpy(ca_coordinates).to(torch.float32))

        self.radius_graph(out)
        self.add_edge_feats(out)

        return out

    @staticmethod
    def add_edge_feats(data: Data):
        edge_vec = data.pos[data.edge_index[0]] - data.pos[data.edge_index[1]]
        rbf_feats = rbf(edge_vec.norm(dim=-1), D_count=16)
        pos_feats = positional_embeddings(data.edge_index, num_embeddings=16)
        data.edge_s = torch.cat([rbf_feats, pos_feats], dim=1)
        data.edge_v = normalize(edge_vec).view(edge_vec.size(0), edge_vec.size(1) // 3, 3)

        return data

class RadiusGraph(object):
    def __init__(self, r, loop=False, max_num_neighbors=32):
        self.r = r
        self.loop = loop
        self.max_num_neighbors = max_num_neighbors

    def __call__(self, data: Data) -> Data:
        pos = data.pos.clone()
        batch = data.batch if "batch" in data else None
        edge_index = radius_graph(
            pos, self.r, batch, self.loop, self.max_num_neighbors,
        )
        data.edge_index = edge_index

        return data

def normalize(ndarray, dim=-1):
    return np.divide(ndarray, np.linalg.norm(ndarray, axis=dim, keepdims=True))

def orientations(ca_cords):
    forward = normalize(ca_cords[1:] - ca_cords[:-1])
    forward = np.vstack([forward, np.zeros((3,))])
    backward = normalize(ca_cords[:-1] - ca_cords[1:])
    backward = np.vstack([np.zeros((3,)), backward])
    return forward, backward

def dihedral_angles(cords, eps=1e-7):
    X = torch.reshape(torch.from_numpy(cords), [3 * cords.shape[0], 3])
    dX = X[1:] - X[:-1]
    U = normalize(dX, dim=-1)
    u_2 = U[:-2]
    u_1 = U[1:-1]
    u_0 = U[2:]

    # Backbone normals
    n_2 = normalize(torch.cross(u_2, u_1), dim=-1)
    n_1 = normalize(torch.cross(u_1, u_0), dim=-1)

    # Angle between normals
    cosD = torch.sum(n_2 * n_1, -1)
    cosD = torch.clamp(cosD, -1 + eps, 1 - eps)
    D = torch.sign(torch.sum(u_2 * n_1, -1)) * torch.acos(cosD)

    D = F.pad(D, [1, 2])
    D = torch.reshape(D, [-1, 3])
    # Lift angle representations to the circle
    D_features = torch.cat([torch.cos(D), torch.sin(D)], 1)
    return D_features

def side_chains(origin, n, c):
    c, n = normalize(c - origin), normalize(n - origin)
    bisector = normalize(c + n)
    perp = normalize(np.cross(c, n))
    vec = -bisector * math.sqrt(1 / 3) - perp * math.sqrt(2 / 3)
    return vec

def rbf(D, D_min=0., D_max=20., D_count=16):
    D_mu = torch.linspace(D_min, D_max, D_count)
    D_mu = D_mu.view([1, -1])
    D_sigma = (D_max - D_min) / D_count
    D_expand = torch.unsqueeze(D, -1)

    RBF = torch.exp(-((D_expand - D_mu) / D_sigma) ** 2)
    return RBF

def positional_embeddings(edge_index,
                          num_embeddings=None):
    d = edge_index[0] - edge_index[1]
    frequency = torch.exp(
        torch.arange(0, num_embeddings, 2, dtype=torch.float32)
        * -(np.log(10000.0) / num_embeddings)
    )
    angles = d.unsqueeze(-1) * frequency
    pos_encoding = torch.cat((torch.cos(angles), torch.sin(angles)), -1)
    return pos_encoding


from flask import Flask, request, render_template
from  flask import Response
import os
from flask_cors import CORS, cross_origin
from flask import Flask,request,app,jsonify,url_for,render_template
import pandas as pd
from preprocess import preprocess
from file_operation import file_op
import numpy as np

c=preprocess()
f=file_op()
app = Flask(__name__)
CORS(app)

os.putenv('LANG', 'en_US.UTF-8')
os.putenv('LC_ALL', 'en_US.UTF-8')

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/predict', methods=['POST'])
@cross_origin()
def predict_route():
    customer_id = int(request.form['CustomerID'])
    name = str(request.form['Name'])
    age = int(request.form['Age'])
    gender = str(request.form['Gender'])
    location = str(request.form['Location'])
    subscription_length = int(request.form['Subscription_Length_Months'])
    monthly_bill = int(request.form['Monthly_Bill'])
    total_usage = int(request.form['Total_Usage_GB'])
    data = {
        'CustomerID': [customer_id],
        'Name': [name],
        'Age': [age],
        'Gender': [gender],
        'Location': [location],
        'Subscription_Length_Months': [subscription_length],
        'Monthly_Bill': [monthly_bill],
        'Total_Usage_GB': [total_usage]
    }
    df = pd.DataFrame.from_dict(data)
    columns_to_drop = ['CustomerID', 'Name']
    df = df.drop(columns=columns_to_drop, errors='ignore')
    df=c.encode_cat(df)
    print(df)
    df=c.scale_numerical(df)
    kmeans=f.load_model('KMeans')
    clusters = kmeans.predict(df)
    df['clusters'] = clusters
    for i in clusters:
       # cluster_data = data[data['clusters'] == i]
        #cluster_data = cluster_data.drop(['clusters'], axis=1)
        df=df.drop(columns='clusters')
        #data_array = df.to_numpy()
        #reshaped_data = data_array.reshape(1, -1)\
        df = df.to_numpy()
        model_name = f.find_correct_model(i)
        reshaped_data = df.reshape(1, -1)
        model = f.load_model(model_name)
        result = model.predict(reshaped_data)
    #result = (model.predict(cluster_data))
        print(result)
    return render_template("home.html", prediction_text="The Prediction of churn is {}".format(result))

port = int(os.getenv("PORT",5000))
if __name__ == "__main__":
    app.run(port=port,debug=True,host="0.0.0.0")


import httpx
from bs4 import BeautifulSoup
from click import secho as echo
import time

def scrape_teams(url: str) -> None:
    max_pages = 2
    current_page = 1

    headers = {
            'user-agent': "Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0",
            'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            'accept-language': "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3",
            'referer': "http://hydraclubbioknikokex7njhwuahc2l67lfiz7z36md2jvopda7nchid.onion/catalog/1",
            'connection': "keep-alive",
            'upgrade-insecure-requests': "1",
            'sec-fetch-dest': "document",
            'sec-fetch-mode': "navigate",
            'sec-fetch-site': "same-origin",
            'sec-fetch-user': "?1",
            'cache-control': "max-age=0"
            }

    cookies = {
            'pregate': "1646068570.39e0d0c6765fc4e8c009a0f5dbe862bc.2e0f25f14562f061dd7fd2c2a33fc1e4",
            'gate': "1646068591.253c8d72ea0bedf45400473203b45c8e.880653c8c540b21f503efa0d974c50f2",
            '_session': "eyJpdiI6IjQ0U0RPSFg4M0owdUJTNG5hSEcrb0E9PSIsInZhbHVlIjoiZDhrSCtoZnFVRkEzdnRsM3JzdUZ6eTVPMnh6TTZaR2VMY0o0eDA3U1Q2N09JUFpOYWJQVkpIa3dZR2MyN1dtTCIsIm1hYyI6IjNkZWVmZGQwNDBmMTBjYjAxZDI5NDUyZDg1NzA3M2Y4YjQ4ZGU1NjE0YmFmYjRhZjkwZTQ2OTQ2MDJiNWM0ZGEifQ%3D%3D",
            'upc': "eyJpdiI6IlNLS1dRM0UyVDhmSU1aMmpcLzdRcFdRPT0iLCJ2YWx1ZSI6IitzdFFNM2hCTlhJclNDYXhoZFdaa3Z3d0JXU3RFQWRiSXlQVTNjZFkxZGs9IiwibWFjIjoiOTBiZTAzZjNkMTQxZDE5MTUzOWQxODI1NDRlMGNmOWQ2ZGQ2ZTE5NDk0YjhiZGNiYzBmZjRiYWU1ZjEzNzUyNSJ9",
            'r1': "1646068653800"
            }

    timeout = httpx.Timeout(10.0, connect=60.0)

    with httpx.Client(proxies='socks5://127.0.0.1:9050') as hs:

        while current_page <= max_pages:
            current_url = f'{url}?query=&sort_direction=desc&sort=rate&page={current_page}'
            print(current_url)

            raw_html = hs.get(current_url, headers=headers, cookies=cookies, timeout=timeout)

            soup = BeautifulSoup(raw_html.content, 'lxml')
            items = soup.find_all('div', class_='catalog_item')

            # itemsReegion = soup.find('li', class_='over').text
            # echo(f'{itemsReegion}',bg='blue')

            for n, i in enumerate(items, start=1):
                itemName = i.find('div', class_='title').text.strip()
                itemPrice = i.find('span').text.strip()
                print(f'{n}: {itemName} {itemPrice} ')

            # print(raw_html.status_code)

        # time.sleep(10)
            current_page += 1

def main() -> int:
    url = 'http://hydraclubbioknikokex7njhwuahc2l67lfiz7z36md2jvopda7nchid.onion/catalog/2'
    scrape_teams(url)
    return 0

if __name__ == '__main__':
    exit(main())