#!/usr/bin/env python3
import math
import torch
import torch.nn as nn

if torch.cuda.is_available():
    FloatTensor = torch.cuda.FloatTensor
    LongTensor = torch.cuda.LongTensor
else:
    FloatTensor = torch.FloatTensor
    LongTensor = torch.LongTensor

class PositionalEncoder(nn.Module):
    r"""
    A position encoder that derives a single-vector representation for a
    sentence consisting of a list of embedding vectors x, taking into account
    each word's position in the sentence. Calculates output representation m as
    follows:

        m = \sum_j l_j \circ x_j

    where

        l is a column vector where l_{kj} = (1 - j/J) - (k/d) (1 - 2j/J)
        x is a list of J word embeddings
        d is the dimension of the embedding space
        \circ is element-wise multiplication

    For more information, see
        Sainbayar Sukhbaatar, Arthur Szlam, Jason Weston, and Rob Fergus.
        End-To-End Memory Networks. In NIPS, 2015.
    """
    def __init__(self, hidden_size):
        super(PositionalEncoder, self).__init__()
        self.d = hidden_size

    def forward(self, embeddings):
        J = embeddings.shape[0]
        L = FloatTensor([[
            (1 - j/J) - (k/self.d) * (1 - 2*j/J) for k in range(1, self.d + 1)]
            for j in range(1, J + 1)])
        encoding = torch.sum(L * embeddings, dim=0)
        return encoding

class QRNCell(nn.Module):
    r"""
    A single QRN unit. Accepts input in the form of a statement x_t, a
    question q_t, and the previous hidden layer h_{t-1}. Calculates output
    hidden layer h_t as follows:

        z_t = \alpha(x_t, q_t) = \sigma(W^{(z)}(x_t \circ q_t) + b^{(z)})

        \tilde{h}_t = \rho(x_t, q_t) = \tanh(W^{(h)}[x_t; q_t] + b^{(h)})

        h_t = z_t \tilde{h}_t + (1 - z_t) h_{t-1}

    where

        \alpha is an update gate
        \rho is a reduce function
        \sigma is the sigmoid activation
        \tanh is the hyperbolic tangent activation
        W^{(z)} and W^{(h)} are weight matrices
        b^{(z)} and b^{(h)} are bias vectors
        \circ is element-wise multiplication
        [;] is vector concatenation along the row

    For more information, see
        Minjoon Seo, Sewon Min, Ali Farhadi, and Hannaneh Hajishirzi.
        Query-Reduction Networks for Question Answering. In ICLR, 2017.
    """
    def __init__(self, input_size, hidden_size):
        super(QRNCell, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size

        self.alpha = nn.Sequential(
            nn.Linear(input_size, hidden_size), nn.Sigmoid())
        self.rho = nn.Sequential(
            nn.Linear(input_size * 2, hidden_size), nn.Tanh())

        self.reset_parameters()

    def reset_parameters(self):
        stdv = 1.0 / math.sqrt(self.hidden_size)
        for weight in self.parameters():
            nn.init.uniform_(weight, -stdv, stdv)

    def forward(self, x, q, h_prev):
        z = self.alpha(x * q)
        h_tilde = self.rho(torch.cat((x, q), 0))
        h = z * h_tilde + (1 - z) * h_prev
        return h

class QRN(nn.Module):
    r"""
    A QRN model consisting of multiple QRN units chained together in layers,
    optionally bidirectional. Accepts as input a story (list of tensors
    representing statements) and a question (single tensor).

    Layers are stacked by passing the outputs of the previous layer as inputs
    to the current layer. Only the question is passed through the QRN; the
    story remains unchanged. If the ORN is not bidirectional, the question
    input to the next layer is given by

        q_t^{k+1} = h_t^{k}

    If the QRN is bidirectional, the question input to the next layer is
    instead given by

        q_t^{k+1} = \overrightarrow{h}_t^k + \overleftarrow{h}_t^k

    where

        \overrightarrow{h} represents the hidden layer in the forward direction
        \overleftarrow{h} represents the hidden layer in the backward direction

    For more information, see
        Minjoon Seo, Sewon Min, Ali Farhadi, and Hannaneh Hajishirzi.
        Query-Reduction Networks for Question Answering. In ICLR, 2017.
    """
    def __init__(self, input_size, hidden_size, n_layers, bidirectional=False):
        super(QRN, self).__init__()

        self.input_size = input_size
        self.hidden_size = hidden_size
        self.n_layers = n_layers
        self.bidirectional = bidirectional
        self.forward_layers = []
        self.backward_layers = []

        for layer in range(n_layers):
            self.forward_layers.append(QRNCell(input_size, hidden_size))
            if torch.cuda.is_available():
                self.forward_layers[-1].cuda()
            if bidirectional:
                self.backward_layers.append(QRNCell(input_size, hidden_size))
                if torch.cuda.is_available():
                    self.backward_layers[-1].cuda()

        self.reset_parameters()

    def reset_parameters(self):
        stdv = 1.0 / math.sqrt(self.hidden_size)
        for weight in self.parameters():
            nn.init.uniform_(weight, -stdv, stdv)

    def forward(self, story, question):
        q_states = []
        h_states_forward = []
        h_states_backward = []

        for layer in range(self.n_layers):
            q_states.append([])
            # initialize h_0 to zero vector
            h_states_forward.append(
                [FloatTensor(self.hidden_size).fill_(0)])
            if self.bidirectional:
                h_states_backward.append(
                    [FloatTensor(self.hidden_size).fill_(0)])

        for i in range(len(story)):
            q_states[0].append(question)

        for layer in range(self.n_layers):
            # forward pass
            for i, statement in enumerate(story):
                h_forward = self.forward_layers[layer](
                    statement, q_states[layer][i],
                    h_states_forward[layer][i])
                h_states_forward[layer].append(h_forward)

            # backward pass
            if self.bidirectional:
                for i, statement in enumerate(reversed(story)):
                    h_backward = self.backward_layers[layer](
                        statement, q_states[layer][-(i + 1)],
                        h_states_backward[layer][i])
                    h_states_backward[layer].append(h_backward)

            # calculate question for next layer if necessary
            if layer < self.n_layers - 1:
                if self.bidirectional:
                    for i in range(len(story)):
                        q_states[layer + 1].append(
                            h_states_forward[layer][i + 1] +
                            h_states_backward[layer][-(i + 1)])
                else:
                    for i in range(len(story)):
                        q_states[layer + 1].append(
                            h_states_forward[layer][i + 1])

        return h_states_forward[-1][-1]

class Model(nn.Module):
    r"""
    A QRN model similar to the one discussed in
        Minjoon Seo, Sewon Min, Ali Farhadi, and Hannaneh Hajishirzi.
        Query-Reduction Networks for Question Answering. In ICLR, 2017.

    Accepts as input a story (list of tensors representing statements) and a
    question (single tensor). Consists of
        * an embedding layer with dimension equal to the hidden layer size that
            encodes each word as a vector
        * a positional encoder that encodes each statement as a single vector
            by combining its word vectors
        * a QRN that outputs a hidden layer representing the network's
            predicted answer to the question
        * a final linear layer that decodes this prediction back into a
            single word
    """
    # TODO: test/make work on batch data
    def __init__(self, n_words, hidden_size, n_layers, bidirectional=False):
        super(Model, self).__init__()
        self.embed = nn.Embedding(n_words + 1, hidden_size, padding_idx=0)
        self.encoder = PositionalEncoder(hidden_size)
        self.QRN = QRN(hidden_size, hidden_size, n_layers, bidirectional)
        self.predict = nn.Linear(hidden_size, n_words + 1)
        self.reset_parameters()

    def reset_parameters(self):
        self.embed.reset_parameters()
        self.QRN.reset_parameters()
        self.predict.reset_parameters()

    def forward(self, story, question):
        story_embed = []
        for statement in story:
            statement_embed = self.encoder(self.embed(statement))
            story_embed.append(statement_embed)
        question_embed = self.encoder(self.embed(question))
        output = self.QRN(story_embed, question_embed)
        output = self.predict(output)
        return output


# # from django.urls import path
# # from . import views

# # urlpatterns = [
# #     path('categories/', views.category_list),
# #     path('categories/<int:pk>/', views.category_detail),
# #     path('customers/', views.customer_list),
# #     path('customers/<int:pk>/', views.customer_detail,name='customer-detail'),
# #     path('products/', views.products_list),
# #     path('products/<int:pk>/', views.products_detail,name='products-detail'),
# #     path('orders/', views.order_list),
# #     path('orders/<int:pk>/', views.order_detail,name='order-detail'),

# #     path('carts/', views.cart_list, name='cart-list'),
# #     path('carts/<int:pk>/', views.cart_detail, name='cart-detail'),
# #     path('cartitems/', views.cartitem_list, name='cartitem-list'),
# #     path('cartitems/<int:pk>/', views.cartitem_detail, name='cartitem-detail'),

# #     path('registration/', views.registration, name='customer-registration'),
# #     path('login/', views.login, name='customer-login'),
# #     path('logout/', views.logout, name='customer-logout'),

# # ]

# from django.urls import path
# from . import views

# urlpatterns = [

#     path('register/', views.customer_register, name='customer_registration'),
#     path('login/', views.customer_login, name='customer_login'),
#     path('logout/', views.customer_logout, name='customer_logout'),

#     # URL patterns for CustomerProfile views
#     path('profiles/', views.customer_profile_view, name='customer-profiles'),
#     path('profiles/<int:pk>/', views.customer_profile_detail_view, name='customer-profile-detail'),

#     # URL patterns for Category views
#     path('categories/', views.category_list, name='categories'),
#     path('categories/<int:pk>/', views.category_detail, name='category-detail'),

#     # URL patterns for Products views
#     path('products/', views.products_list, name='products'),
#     path('products/<int:pk>/', views.products_detail, name='products-detail'),

#     # URL patterns for Order views
#     path('orders/', views.order_list, name='orders'),
#     path('orders/<int:pk>/', views.order_detail, name='order-detail'),

#     # URL patterns for Cart views
#     path('carts/', views.cart_list, name='carts'),
#     path('carts/<int:pk>/', views.cart_detail, name='cart-detail'),

#     # URL patterns for CartItem views
#     path('cartitems/', views.cart_item_list, name='cart-items'),
#     path('cartitems/<int:pk>/', views.cart_item_detail, name='cart-item-detail'),

#     path('wishlist/',views.wishlist_list, name='wishlist-list'),
#     path('wishlist/<int:pk>/', views.wishlist_detail, name='wishlist-detail'),
# ]

from django.urls import path
from . import views
urlpatterns = [
    # path('register/', views.customer_registration_view, name='customer-registration'),
    # path('login/', views.customer_login_view, name='customer-login'),
    path('logout/', views.customer_logout_view, name='customer-logout'),

    # path('cust/detail/', views.customer_detail_view, name='customer-detail'),
    # path('cust/list/', views.customer_list_create_view, name='customer-list'),
    path('cust/detail/<int:pk>/', views.customer_detail_api_view, name='customer-detail-api'),

    path('pro/list/', views.product_list_create_view, name='product-list'),
    path('pro/detail/<int:pk>/', views.product_detail_api_view, name='product-detail-api'),

    # path('fea/list/', views.feature_list_create_view, name='feature-list'),
    # path('fea/detail/<int:pk>/', views.feature_detail_api_view, name='feature-detail-api'),

    # path('rev/list/', views.review_list_create_view, name='review-list'),
    # path('rev/detail/<int:pk>/', views.review_detail_api_view, name='review-detail-api'),

    path('order/list/', views.order_list_create_view, name='order-list'),
    path('order/detail/<int:pk>/', views.order_detail_api_view, name='order-detail-api'),

    path('ord-item/list/', views.order_item_list_create_view, name='order-item-list'),
    path('ord-item/detail/<int:pk>/', views.order_item_detail_api_view, name='order-item-detail-api'),

    # path('wishlist/list/', views.wishlist_list_create_view, name='wishlist-list'),
    # path('wishlist/detail/<int:pk>/', views.wishlist_detail_api_view, name='wishlist-detail-api'),

    # path('register/',views.customer_registration_view, name='customer-registration'),
    path('generate-otp/',views.generate_otp_view, name='generate-otp'),
    path('login/',views.customer_login_view, name='customer-login'),

     path('register/', views.customer_registration_view, name='customer-registration'),
    path('verify-otp/', views.otp_verification_view, name='otp-verification'),
]


'''
Module to choose the demand w.r.t. certain school and node compression

Xiaotong Guo
Jan 2019
'''

import Structure
from Parameters import MyGlobal
import math
from gurobipy import *
import TSP

def school_demand(school_id):
    """
    Function to filter demand for certain schools
    """
    demand = []
    for student in MyGlobal.demand:
        if student[1] == school_id:
            demand.append(list(student))
    return demand

def node_compression(demand, Beta, Gamma, alternate_mode_param, virtual_walking_dsitance):
    """
    Function for node compression techniques
    :param demand:
    :return:
    """
    bus_stops = list(MyGlobal.station_dict.keys())
    distance = MyGlobal.distance

    d2d = 0
    student_list = []
    for id, s in enumerate(demand):
        reachable_stop = []
        ori = int(s[0])
        dest = int(s[1])
        walking_distance = s[2]
        if walking_distance == 0:
            maximum_walking_distance = virtual_walking_dsitance * 1.60934
            # maximum_walking_distance = MyGlobal.fake_walking_distance * 1.60934
            d2d += 1
        else:
            maximum_walking_distance = walking_distance * 1.60934
        for stop in bus_stops:
            stop_node = stop
            d = distance[ori][stop_node]
            if d <= maximum_walking_distance:
                reachable_stop.append(stop_node)
        student = Structure.student(id,ori,dest,walking_distance,reachable_stop)
        student_list.append(student)

    print("The number of door-to-door students is: " + str(d2d))
    output_log = open("output_{}_{}_{}_{}.txt".format(MyGlobal.school_id, Beta, Gamma, alternate_mode_param), "a+")
    output_log.write("The number of door-to-door students is: " + str(d2d) + '\n')
    output_log.close()

    # Store in the MyGlobal object
    for student in student_list:
        MyGlobal.student_id_dict[student.id] = student

    # m = Model("IP")
    # m.setParam("OutputFlag", 0)
    #
    # mu = {}
    # for stop in bus_stops:
    #     for student in student_list:
    #         mu[(stop, student.id)] = m.addVar(vtype=GRB.BINARY, name='mu')
    #
    # nu = {}
    # for stop in bus_stops:
    #     nu[stop] = m.addVar(vtype=GRB.BINARY, name='nu')
    #
    # for student in student_list:
    #     stop_list = student.reachable_list
    #     m.addConstr(quicksum(mu[(id, student.id)] for id in stop_list) == 1, str(student.id))
    #
    # for student in student_list:
    #     stop_list = student.reachable_list
    #     for stop in bus_stops:
    #         if stop not in stop_list:
    #             m.addConstr(mu[(stop, student.id)] == 0)
    #
    # for stop in bus_stops:
    #     m.addConstr(quicksum(mu[(stop, student.id)] for student in student_list) <= MyGlobal.max_students_per_stop)
    #
    # for stop in bus_stops:
    #     for student in student_list:
    #         m.addConstr(nu[stop] >= mu[(stop, student.id)])
    #
    # m.setObjective(quicksum(nu[stop] for stop in bus_stops), GRB.MINIMIZE)
    # m.optimize()
    #
    # optimal_stops = []
    # for id, var in nu.items():
    #     if var.X == 1:
    #         optimal_stops.append(id)
    #
    # dic_stops = {}
    # for stop in optimal_stops:
    #     dic_stops[stop] = 0
    #
    # for (stop, student_id), var in mu.items():
    #     if var.X == 1:
    #         dic_stops[stop] += 1
    #         student = MyGlobal.student_id_dict[student_id]
    #         student.assigned_stop = stop

    m = Model("IP")
    m.setParam("OutputFlag", 0)
    x = {}
    for stop in bus_stops:
        x[int(stop)] = m.addVar(vtype=GRB.BINARY, name='x')

    for student in student_list:
        stop_list = student.reachable_list
        m.addConstr(quicksum(x[id] for id in stop_list) >= 1, str(student.id))

    m.setObjective(quicksum(x[i] for i in range(0,len(bus_stops))), GRB.MINIMIZE)
    m.optimize()

    optimal_stops = []
    for id, var in x.items():
        if var.X == 1:
            optimal_stops.append(id)

    dic_covered_stops = {}
    for student in student_list:
        dic_covered_stops[student.id] = []
        for stop in optimal_stops:
            if stop in student.reachable_list:
                dic_covered_stops[student.id].append(stop)

    dic_stops = {}
    for stop in optimal_stops:
        dic_stops[stop] = 0

    for student in student_list:
        if len(dic_covered_stops[student.id]) == 1:
            dic_stops[dic_covered_stops[student.id][0]] += 1
            student.assigned_stop = dic_covered_stops[student.id][0]

    for student in student_list:
        if len(dic_covered_stops[student.id]) > 1:
            listt = dic_covered_stops[student.id]
            comparing_list = []
            for i in listt:
                if dic_stops[i] >= MyGlobal.vehicle_capacity:
                    continue
                comparing_list.append((dic_stops[i], i))
            comparing_list.sort()
            dic_stops[comparing_list[-1][1]] += 1
            student.assigned_stop = comparing_list[-1][1]

    if sum(dic_stops.values()) != len(student_list):
        print("Error in node compression")

    return_demand = []
    for stop in optimal_stops:
        demand_ = [stop,MyGlobal.school_id,dic_stops[stop]]
        return_demand.append((demand_))

    return return_demand, student_list

def demand_to_request(demand, student_list):
    '''
    Convert the demand to request object and store in the list in MyGloble object
    '''

    for i, value in enumerate(demand):
        s_list = []
        d2d_student_list = []
        stop_id = value[0]

        for student in student_list:
            if student.assigned_stop == stop_id:
                s_list.append(student.id)
                if student.walking_distance == 0:
                    d2d_student_list.append(student.id)

        if d2d_student_list == []:
            request = Structure.request(MyGlobal.total_request_number, int(stop_id), int(value[1]), int(value[2]),
                                        s_list, d2d_student_list)
            MyGlobal.request_list.append(request)
            MyGlobal.request_id_dict[MyGlobal.total_request_number] = request
            MyGlobal.total_student_number = MyGlobal.total_student_number + value[2]
            MyGlobal.total_request_number = MyGlobal.total_request_number + 1
        else:
            if len(d2d_student_list) == 1:
                # if len(s_list) == len(d2d_student_list):
                #     student_ori = MyGlobal.student_id_dict[d2d_student_list[0]].ori
                #     routes = [stop_id,student_ori,stop_id]
                #     penalty = MyGlobal.time[stop_id,student_ori] + MyGlobal.time[student_ori,stop_id]
                #     request = Structure.request(MyGlobal.total_request_number, int(stop_id), int(value[1]),
                #                                 int(value[2]), s_list, d2d_student_list, penalty, routes)
                #     MyGlobal.request_list.append(request)
                #     MyGlobal.request_id_dict[MyGlobal.total_request_number] = request
                #     MyGlobal.total_student_number = MyGlobal.total_student_number + value[2]
                #     MyGlobal.total_request_number = MyGlobal.total_request_number + 1
                #     continue
                student_ori = MyGlobal.student_id_dict[d2d_student_list[0]].ori
                routes = [stop_id,student_ori,stop_id]
                penalty = MyGlobal.time[stop_id,student_ori] + MyGlobal.time[student_ori,stop_id]
            else:
                points = []
                for student in d2d_student_list:
                    student_ori = MyGlobal.student_id_dict[student].ori
                    points.append(student_ori)
                if stop_id not in points:
                    points = [stop_id] + points
                if len(points) == 2:
                    penalty = MyGlobal.time[points[0],points[1]] + MyGlobal.time[points[1],points[0]]
                    if points[0] == stop_id:
                        routes = points + [stop_id]
                    else:
                        routes = [stop_id] + points
                else:
                    penalty, routes = TSP.small_tsp(points)

            request = Structure.request(MyGlobal.total_request_number, int(stop_id), int(value[1]), int(value[2]),
                                        s_list, d2d_student_list, penalty, routes)
            MyGlobal.request_list.append(request)
            MyGlobal.request_id_dict[MyGlobal.total_request_number] = request
            MyGlobal.total_student_number = MyGlobal.total_student_number + value[2]
            MyGlobal.total_request_number = MyGlobal.total_request_number + 1


#!/usr/bin/env python3

import collections
from typing import Any, Dict

import numpy as np
import six
from caffe2.python import workspace
from ml.rl.preprocessing.normalization import NormalizationParameters
from ml.rl.training.discrete_action_trainer import DiscreteActionTrainer
from ml.rl.training.rl_trainer import RLTrainer

class LimitedActionDiscreteActionTrainer(DiscreteActionTrainer):
    def __init__(
        self, parameters, normalization_parameters: Dict[int, NormalizationParameters]
    ) -> None:
        self._quantile_states: Any = collections.deque(
            maxlen=parameters.action_budget.window_size
        )
        self._quantile = 100 - parameters.action_budget.action_limit
        self.quantile_value = 0
        self._limited_action = np.argmax(
            np.array(parameters.actions) == parameters.action_budget.limited_action
        )
        self._discount_factor = parameters.rl.gamma
        self._quantile_update_rate = parameters.action_budget.quantile_update_rate
        self._quantile_update_frequency = (
            parameters.action_budget.quantile_update_frequency
        )
        self._update_counter = 0
        DiscreteActionTrainer.__init__(self, parameters, normalization_parameters)
        self._max_q = parameters.rl.maxq_learning

    def action_values(self, states, action_idx):
        workspace.FeedBlob("states", states)
        workspace.RunNet(self.all_q_score_model.net)
        q = workspace.FetchBlob(self.all_q_score_output)
        return q[:, action_idx]

    def train_numpy(self, tdp, evaluator):
        self._quantile_states.extendleft(tdp.states)
        if (
            self._update_counter % self._quantile_update_frequency
            == self._quantile_update_frequency - 1
        ):
            self._update_quantile()
        self._update_counter += 1

        if self._max_q:
            workspace.FeedBlob("states", tdp.states)
            workspace.FeedBlob("actions", tdp.possible_next_actions)
            workspace.RunNet(self.q_score_model.net)
            q_values = workspace.FetchBlob(self.q_score_output)
            q_next_actions = np.argmax(q_values, axis=1).reshape(-1, 1)
            q_next_actions_mask = np.zeros(
                [q_next_actions.shape[0], self.num_actions], dtype=np.float32
            )
            for x in range(q_next_actions.shape[0]):
                q_next_actions_mask[q_next_actions[x, 0], 0] = 1.0
            q_next_actions = q_next_actions_mask
        else:
            q_next_actions = tdp.next_actions
        penalty = self._reward_penalty(tdp.actions, q_next_actions, tdp.not_terminals)
        assert penalty.shape == tdp.rewards.shape, (
            "" + str(penalty.shape) + "" + str(tdp.rewards.shape)
        )
        tdp.rewards = tdp.rewards - penalty
        RLTrainer.train_numpy(self, tdp, evaluator)

    def _update_quantile(self):
        states = np.array(self._quantile_states, dtype=np.float32)
        limited_action_values = self.action_values(states, self._limited_action)
        base_action_values = np.max(
            np.array(
                [
                    self.action_values(states, action)
                    for action in six.moves.range(self.num_actions)
                    if action != self._limited_action
                ]
            ),
            axis=0,
        )
        target = np.percentile(
            limited_action_values - base_action_values, self._quantile
        )
        print("REWARD PENALTY TARGET:", target)
        self.quantile_value += self._quantile_update_rate * target
        print("QUANTILE:", self.quantile_value)

    def _reward_penalty(self, actions, next_actions, not_terminals):
        print(not_terminals)
        print(not_terminals.shape)
        print(actions.shape)
        print(next_actions.shape)
        return (
            (
                (
                    (actions[:, self._limited_action] > 0.999)
                    - self._discount_factor
                    * (not_terminals[:, 0])
                    * (next_actions[:, self._limited_action] > 0.999)
                )
                * self.quantile_value
            )
            .astype(np.float32)
            .reshape(-1, 1)
        )


import pymongo
import pandas as pd
import json
from sensor.config import mongo_client

COPY_FILE_PATH="/config/workspace/aps_failure_training_set1.csv"
DATABASE_NAME="aps"
COLLECTION_NAME="sensor"

if __name__=="__main__":
    df=pd.read_csv(COPY_FILE_PATH)
    print(f"Rows and Columns:{df.shape}")

    #Converting dataframe into the json format so that we can dump data into the mongo db

    df.reset_index(drop=True,inplace=True)

    json_record=list(json.loads(df.T.to_json()).values())
    print(json_record[0])

    mongo_client[DATABASE_NAME][COLLECTION_NAME].insert_many(json_record)


def who_is_missing(file_name):
	"""
	:param file_name: path to text file.
	:type file_name: string
	:return: the missing number.
	rtype: int
	"""
	with open(file_name) as numbers_file:
		string_of_numbers = numbers_file.read().splitlines()[0]
		list_of_numbers = [int(x) for x in sorted(string_of_numbers.split(","))]
		compare_num = 1
		for num in list_of_numbers:
			if (num == compare_num):
				compare_num = compare_num + 1
			else:
				with open(r"C:\Users\Liraz\Desktop\hangman\hangman-unit9\found.txt", 'w') as output_file:
					output_file.write(str(compare_num))
				break

def main():
	who_is_missing(r"C:\Users\Liraz\Desktop\hangman\hangman-unit9\findMe.txt")

if __name__ == "__main__":
	main()


import numpy as np
from scipy.io import loadmat
import pyfar as pf

def ita_ifft(data, signaltype, n_samples=None):
    """ Calculate the IFFT as done in the ITA-Toolbox for MATLAB
    The signal is assumed to be real valued in the time domain, therefore
    only the right sided spectrum is required here. Further, the frequency
    domain data is defined as its effective value inside the ITA-Toolbox
    for MATLAB, requiring a multiplication by sqrt(2) when applying the IFFT.

    Notes
    -----
        Currently only supports even numbers of samples

    Parameters
    ----------
    data : ndarray
        Data in the frequency domain
    signaltype : string
        Signal type, can either be an energy or a power signal

    Returns
    -------
    data : ndarray
        Data in the time domain

    """
    if n_samples is None:
        n_samples = 2*(data.shape[0] - 1)

    n_samples_is_even = ((n_samples % 2) == 0)

    if signaltype == 'power':
        data *= n_samples
        if n_samples_is_even:
            data[-1, :] = data[-1, :] * np.sqrt(2)
            data[1:-2, :] = data[1:-2, :] / np.sqrt(2)
        else:
            data[1:-1, :] = data[1:-1, :] / np.sqrt(2)
    return np.fft.irfft(data, axis=0)

def read_ita(filename):
    """Read .ita audio files. The standard audio file format of the
    ITA-Toolbox for MATLAB. This function returns the time data of the signal
    regardless of the domain the signal was stored in inside the file.

    Notes
    -----
        This function currently only works with .ita files saved in the MATLAB
        file format with versions v4, v6, and v7 to v7.2. Version v7.3 MATLAB
        files are not supported yet.

    Parameters
    ----------
    filename : string
        Name of the file to be read

    Returns
    -------
    data : ndarray, double
        Time data stored in the file
    samplingrate : integer
        Sampling rate of the signal
    type : string
        Signal type, can either be an energy or a power signal

    """
    matfile = loadmat(
        filename, struct_as_record=False, squeeze_me=True, appendmat=False)
    mfiledata = matfile['ITA_TOOLBOX_AUDIO_OBJECT']

    if mfiledata.domain == 'time':
        data = mfiledata.data
        try:
            samplingrate = mfiledata.samplingRate
        except AttributeError:
            times = mfiledata.abscissa
            return pf.TimeData(data.T, np.squeeze(times))

    elif mfiledata.domain == 'freq':
        data = ita_ifft(mfiledata.data, mfiledata.signalType)
        try:
            samplingrate = mfiledata.samplingRate
        except AttributeError:
            freqs = mfiledata.abscissa
            return pf.FrequencyData(data.T, np.squeeze(freqs))

    if mfiledata.signalType == 'energy':
        norm = 'none'
    else:
        norm = 'rms'

    return pf.Signal(data.T, samplingrate, fft_norm=norm)


import sys
import time
from typing import Optional

from controllers.base_controller import BaseController

from controllers.the_abominable_0 import TheAbominable0
from game.board import BoardManager
from config import game_end_delay, step_limit, model_save_interval
from ui.progress_bar import print_progress_bar
from ui.ui import UI

def exit_smoothly():
    print("Exiting smoothly")
    global controller
    print("Saving weights")
    controller.save()
    exit()

def reset():
    global game_number
    game_number += 1

    if game_number % model_save_interval == 0:
        print(f"Saving weights on game {game_number}: ")
        controller.save()

    board_manager.reset()
    controller.reset()

ui: Optional[UI] = None
if "--no-ui" not in sys.argv:
    ui = UI()

board_manager = BoardManager()
controller: BaseController = TheAbominable0(board_manager)

game_number = 0
step = 0
while True:
    try:
        if step >= step_limit:
            print(f"failed to finish on game {game_number + 1}: ")
            reset()
            step = 0

        board_manager.update_actor_position(controller.move())
        if ui:
            ui.draw(
                board=board_manager.get_current_board(),
                actor_path=controller.get_actor_path(),
                failed_to_move=board_manager.failed_to_move(),
            )
            if ui.check_if_terminated():
                exit_smoothly()
        else:
            # only works when starting from terminal
            print_progress_bar(step, step_limit, prefix=f"game {game_number + 1}:")

        if board_manager.has_game_ended(controller.get_current_position()):
            if ui:
                time.sleep(game_end_delay)
            reset()
            step = 0
        # elif ui:
        #     time.sleep(frame_delay)

        step += 1
    except Exception as e:
        print(f"{e} on game {game_number + 1}: ")
        reset()
        step = 0

    except KeyboardInterrupt:
        exit_smoothly()


import helper as hp
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from graphics import Color

class Table:

    def __init__(self, df=None, param=None, filePath=None, numpy=None, features=None, constrainX=None, constrainY=None, drawTable=True, classColors=None):
        """
        Create a Table to store data.

        Args 1:
        ----------
        - filePath: String - path to csv & param file
        - param: Dict? - overwrites param file

        Args 2:
        ----------
        - df: pandas.DataFrame - references dataframe w/o copy
        - filePath: String? - path to param file only
        - param: Dict? - overwrites param file

        Args 3:
        ----------
        - numpy: numpy.ndarray - generate DataFrame from numpy
        - filePath: String? - path to param file only
        - param: Dict? - overwrites param file

        Optional Args
        - features: int - number of x columns to keep. None=all
        - constrainX: (minX, maxX) - constrain all x cols to range
        - constrainY: (minY, maxY) - constrain y col to range
        """
        # get data parameters
        self.filePath = filePath
        self.fileName = self.filePath.split("/")[-1].split(".")[0] if filePath != None else None
        self.param = hp.loadJSON(filePath + ".json") if not param else param
        self.drawTable = drawTable

        # get column names
        self.yName = self.param['target']
        self.xNames = self.param['columns']
        if features != None:
            self.xNames = self.xNames[:features]
        self.features = len(self.xNames)
        self.colNames = [self.yName] + self.xNames
        self.constrainX = constrainX
        self.constrainY = constrainY

        # process data frame
        self.data = df if df is not None else (pd.read_csv(hp.resourcePath(self.filePath + ".csv")) if self.filePath != None else pd.DataFrame(data=numpy, columns=self.colNames))
        self.data = self.data.drop_duplicates(self.colNames)
        self.data = self.data[self.colNames]
        # print("Table Length:", len(self.data.index))

        # convert float labels to int
        # self.data[self.yName] = self.data[self.yName].astype('int64')

        # constraining is used for logistic model to map range min,max to 0,1
        if(self.constrainX != None):
            x = self.data[self.xNames]
            self.data[self.xNames] = self.constrainX[1] * (x - x.min()) / x.max() + self.constrainX[0]

        if(self.constrainY != None):
            y = self.data[self.yName]
            self.data[self.yName] = self.constrainY[1] * (y - y.min()) / y.max() + self.constrainY[0]

        self.dataX = self.data[self.xNames]
        self.dataY = self.data[self.yName]

        self.y = self.dataY.to_numpy()
        self.y = self.y.reshape([self.y.shape[0], 1])
        self.x = self.dataX.to_numpy()

        self.classSet = self.dataY.unique()
        self.classCount = len(self.classSet)
        self.rowCount = len(self.data.index)  # total rows of data
        self.colCount = len(self.xNames)  # does not include target
        self.isBoolCol = [self.data[colName].isin([0, 1]).all() for colName in self.colNames]

        self.minX1 = self.data[self.xNames[0]].min()
        self.maxX1 = self.data[self.xNames[0]].max()
        x2Name = self.yName if self.features == 1 else self.xNames[1]
        self.minX2 = self.data[x2Name].min()
        self.maxX2 = self.data[x2Name].max()

        if classColors != None:
            self.classColors = classColors
        else:
            self.classColors = {}
            i = 0
            for label in sorted(list(self.classSet)):
                # using str(label) because Table.flatten() is used for Grid.items and it may convert to string
                self.classColors[str(label)] = hp.calmColor(i / self.classCount)
                i += 1

        # self.selectedRow = None
        self.selectedCol = None  # index
        self.lockedCols = []
        self.graphics = []

        self.mapper = {d['column']: d['values'] for d in (self.param['map'] if "map" in self.param else {})}

        # Hide Code

        # self.columns = self.data.columns
        # self.loc = self.data.loc
        # self.x = np.array(self.x)
        # from sklearn.preprocessing import StandardScaler
        # self.x = StandardScaler().fit_transform(self.x)
        # self.y = np.array(self.y).reshape([self.rowCount, 1])

    def majorityInColumn(self, column):
        return self.data[column].value_counts().idxmax()

    def majorityInTargetColumn(self):
        return self.majorityInColumn(self.yName)

    def init(self, df, param=None, filePath=None, features=None, constrainX=None,
             constrainY=None, drawTable=None, classColors=None):
        """
        Create a new table based on the given parameters or the current table
        """
        return Table(df=df, param=self.param if param == None else param, filePath=self.filePath if filePath == None else filePath, features=self.features if features == None else features, constrainX=self.constrainX if constrainX == None else constrainX,
                     constrainY=self.constrainY if constrainY == None else constrainY, drawTable=self.drawTable if drawTable == None else drawTable, classColors=self.classColors if classColors == None else classColors)

    def partition(self, testing=0.3) -> tuple:
        """
        Split the table into two tables by rows
        """
        train, test = train_test_split(self.data, test_size=testing)
        return self.init(train), self.init(test)

    def matchValue(self, colIndex, value):
        """
        Create a new table where the row contains a value at the given column.
        """
        return self.init(df=self[self[self.xNames[colIndex]] == value])

    def minX(self, column=None):
        """
        Get the minimum value in the x column.
        """
        return self.dataX.min()[column if column != None else self.xNames[0]]

    def maxX(self, column=None):
        """
        Get the maximum value in the x column.
        """
        return self.dataX.max()[column if column != None else self.xNames[0]]

    def minY(self):
        return self.y.min()

    def maxY(self):
        return self.y.max()

    def uniqueVals(self, colIndex):
        return self[self.xNames[colIndex]].unique()

    def flattenValues(self):
        return self.data.values.flatten()

    def flatten(self):
        return np.concatenate([self.colNames, self.flattenValues()])

    def addGraphic(self, graphic):
        self.graphics.append(graphic)

    def tableChange(self, colIndex=None, isSelect=None, isLock=None, isNewTable=False, reset=False):
        """
        colIndex: index (x values starting at 1)
        isSelect: True, False or None(no change)
        isLock:True, False or None(no change)
        """

        if reset:
            self.selectedCol = None
            self.lockedCols = []
            colIndex = None
            isSelect = False
            isLock = False
            isNewTable = True
        else:

            # can't lock/select a none colIndex or unlock nothing
            # you can select what you unlock though
            if ((isLock or (isLock != False and isSelect)) and colIndex == None) or (isLock == False and len(self.lockedCols) == 0) or (isSelect == False and self.selectedCol == None):
                # print("Fail Change | Cond 1:", ((isLock or isSelect) and column == None), "Cond 2:", (isLock == False and len(self.lockedCols) == 0), "Cond 3:", (isSelect == False and self.selectedCol == None))
                return

            if isLock:
                self.lockedCols.append(colIndex)
            elif isLock == False:
                # you can select this item
                colIndex = self.lockedCols.pop()

            if isSelect != None:
                self.selectedCol = colIndex if isSelect else None

        # Change None value to previous values
        # isSelect = self.selectedCol != None and self.selectedCol == colIndex
        # isLock = colIndex in self.lockedCols
        for graphic in self.graphics:
            graphic.tableChange(colIndex=colIndex, isSelect=isSelect, isLock=isLock, isNewTable=isNewTable, reset=reset)

    def getEncodedData(self):
        """
        Creates a new Table where categorized data is converted to boolean by adding a column for each unique value per column.
        Based on pd.get_dummies()
        """
        dataX = pd.get_dummies(self.dataX)

        param = self.param.copy()
        param['columns'] = list(dataX.columns)
        return self.init(df=pd.concat([dataX, self.dataY], axis=1, join="inner"), param=param)

    def map(self, colIndex, value):
        column = self.colNames[colIndex]
        return self.mapper[column][value] if column in self.mapper and value in self.mapper[column] else str(value)

    def getPt(self, x, y, color):  # get many points
        return (hp.map(x, self.minX1, self.maxX1, -1, 1, clamp=False), hp.map(y, self.minX2, self.maxX2, -1, 1, clamp=False), color)

    def getPts(self):
        if self.drawTable:
            if self.features >= 2:
                return [self.getPt(self.x[i][0], self.x[i][1], self.classColors[str(self.y[i][0])]) for i in range(self.rowCount)]
            return [self.getPt(self.x[i][0], self.y[i], Color.green) for i in range(self.rowCount)]

    def __getitem__(self, column):
        return self.data[column]

if __name__ == '__main__':
    hp.clear()
    print("Running TABLE MAIN")
    table = Table(filePath="examples/logistic/sigmoid", constrainX=(0, 1), constrainY=(0, 1))
    print("Row Count:", table.rowCount)
    print("Target:", table.yName)
    print("Features:", table.xNames)
    print("Classes(" + str(table.classCount) + "):", table.classSet)
    print()

    colName = table.xNames[0]
    print("Majority in", colName, "is", table.majorityInColumn(colName))
    print("Majority in", table.yName, "is", table.majorityInTargetColumn())
    print(colName, "Min:", table.minX(colName), "Max:", table.maxX(colName))
    print(table.yName, "Min:", table.minY(), "Max:", table.maxY())
    print("Head:")
    print(table.data.head())
    print("Flatten:")
    print(table.flatten())

    # print(table.createXYTable().data)


import unittest
from checkmysql.connector import MySQLConn

class MySQLTest(unittest.TestCase):
    def test_connection(self):
        con = MySQLConn.create('localhost', 'test', 'test', 'test', 3306)
        res = con.fetch("select 1 as checkin;")

        if res[0].get('checkin', None) == 1:
            print('db check ok')
        else:
            print('db check ko')

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


import socket
import json

import config

# サーバーのアドレス
adress = (config.calc_address, config.calc_port)

def calc_score_for_test(plan_list: dict, word: str):
    # 計算後の単語を格納する辞書型
    similarities_dict = {}

    cl = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    cl.connect(adress)

    for plan in plan_list:
        send_data = json.dumps({"word1" : plan, "word2" : word})
        cl.send(send_data.encode("utf-8"))

        similarities_dict[plan] = cl.recv(1024).decode("utf-8")

    return json.dumps(similarities_dict, ensure_ascii=False)

def calc_score(plan: str, word: str):

    cl = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    cl.connect(adress)

    send_data = json.dumps({"word1" : plan, "word2" : word})
    cl.send(send_data.encode("utf-8"))

    ans = cl.recv(1024).decode("utf-8")

    cl.close()

    return ans

def main():
    word1 = input("word1 : ")
    word2 = input("word2 : ")
    ans = calc_score(word1, word2)
    print(ans)

if __name__ == "__main__":
    main()


import logging.config
import signal
from time import sleep
from argparse import ArgumentParser, Namespace
from cmdb.utils.logger import get_logging_conf
from cmdb.utils.wraps import timing
from cmdb.utils.termcolor import colored
from sys import exit

try:
    from cmdb.utils.error import CMDBError
except ImportError:
    CMDBError = Exception

from cmdb.utils.system_config import SystemConfigReader
from cmdb.database.managers import DatabaseManagerMongo

# setup logging for startup
logging.config.dictConfig(get_logging_conf())
LOGGER = logging.getLogger(__name__)

def _activate_debug():
    """
    Activate the debug mode
    """
    import cmdb
    cmdb.__MODE__ = 'DEBUG'
    LOGGER.warning("DEBUG mode enabled")

def _check_database():
    """
    Checks whether the specified connection of the configuration is reachable.
    Returns: Datebase if response

    """
    from cmdb.database.errors.connection_errors import ServerTimeoutError
    ssc = SystemConfigReader()
    LOGGER.info(f'Checking database connection with {ssc.config_name} data')
    database_options = ssc.get_all_values_from_section('Database')
    dbm = DatabaseManagerMongo(
        **database_options
    )
    try:
        connection_test = dbm.connector.is_connected()
    except ServerTimeoutError:
        connection_test = False
    LOGGER.debug(f'Database status is {connection_test}')
    if connection_test is True:
        return dbm
    retries = 0
    while retries < 3:
        retries += 1
        LOGGER.warning(
            f'Retry {retries}: Checking database connection with {SystemConfigReader.DEFAULT_CONFIG_NAME} data')

        connection_test = dbm.connector.is_connected()
        if connection_test:
            return dbm
    return None

def _start_app():
    """
    Starting the services
    """
    import cmdb.process_management.process_manager

    global app_manager

    # install signal handler
    signal.signal(signal.SIGTERM, _stop_app)

    # start app
    app_manager = cmdb.process_management.process_manager.ProcessManager()
    app_status = app_manager.start_app()
    LOGGER.info(f'Process manager started: {app_status}')

def _stop_app(signum, frame):
    global app_manager
    app_manager.stop_app()

def _init_config_reader(config_file):
    import os

    path, filename = os.path.split(config_file)
    if filename is not SystemConfigReader.DEFAULT_CONFIG_NAME or path is not SystemConfigReader.DEFAULT_CONFIG_LOCATION:
        SystemConfigReader.RUNNING_CONFIG_NAME = filename
        SystemConfigReader.RUNNING_CONFIG_LOCATION = path + '/'
    SystemConfigReader(SystemConfigReader.RUNNING_CONFIG_NAME,
                       SystemConfigReader.RUNNING_CONFIG_LOCATION)

def build_arg_parser() -> Namespace:
    """
    Generate application parameter parser
    Returns: instance of OptionParser

    """

    from cmdb import __title__
    _parser = ArgumentParser(
        prog='DATAGERRY',
        usage="usage: {} [options]".format(__title__),
    )

    _parser.add_argument('--keys', action='store_true', default=False, dest='keys',
                         help="init keys")

    _parser.add_argument('-d', '--debug', action='store_true', default=False, dest='debug',
                         help="enable debug mode: DO NOT USE ON PRODUCTIVE SYSTEMS")

    _parser.add_argument('-s', '--start', action='store_true', default=False, dest='start',
                         help="starting cmdb core system - enables services")

    _parser.add_argument('-c', '--config', default='./etc/cmdb.conf', dest='config_file',
                         help="optional path to config file")

    return _parser.parse_args()

@timing('CMDB start')
def main(args):
    """
    Default application start function
    Args:
        args: start-options
    """
    LOGGER.info("DATAGERRY starting...")
    if args.debug:
        _activate_debug()
    _init_config_reader(args.config_file)
    from cmdb.database.errors.connection_errors import DatabaseConnectionError

    # create / check connection database managers
    dbm = None

    # check db-settings and run update if needed
    if args.start:
        try:
            dbm = _check_database()
            if not dbm:
                raise DatabaseConnectionError('')
            LOGGER.info("Database connection established.")
        except CMDBError as conn_error:
            LOGGER.critical(conn_error.message)
            exit(1)
        from cmdb.__check__ import CheckRoutine
        check_routine = CheckRoutine(dbm)
        # check db-settings
        try:
            check_status = check_routine.checker()
        except Exception as err:
            LOGGER.error(err)
            check_status = check_routine.get_check_status()
            LOGGER.error(f'The check did not go through as expected. Please run an update. \n Error: {err}')
        if check_status == CheckRoutine.CheckStatus.HAS_UPDATES:
            # run update
            from cmdb.__update__ import UpdateRoutine
            update_routine = UpdateRoutine()
            try:
                update_status = update_routine.start_update()
            except RuntimeError as err:
                LOGGER.error(err)
                update_status = update_routine.get_updater_status()
                LOGGER.warning(f'The update did not go through as expected - Status {update_status}')
            if update_status == UpdateRoutine.UpateStatus.FINISHED:
                check_status = CheckRoutine.CheckStatus.FINISHED
            else:
                exit(1)
        if check_status == CheckRoutine.CheckStatus.FINISHED:
            # run setup if needed
            from cmdb.__setup__ import SetupRoutine
            setup_routine = SetupRoutine(dbm)
            try:
                setup_status = setup_routine.setup()
            except RuntimeError as err:
                LOGGER.error(err)
                setup_status = setup_routine.get_setup_status()
                LOGGER.warning(f'The setup did not go through as expected - Status {setup_status}')
            if setup_status == SetupRoutine.SetupStatus.FINISHED:
                pass
            else:
                exit(1)
        else:
            pass

    if args.keys:
        from cmdb.__setup__ import SetupRoutine
        setup_routine = SetupRoutine(dbm)
        setup_status = None
        try:
            setup_routine.init_keys()
        except RuntimeError as err:
            LOGGER.error(err)
            setup_status = setup_routine.get_setup_status()
            LOGGER.warning(f'The key generation did not go through as expected - Status {setup_status}')
        if setup_status == SetupRoutine.SetupStatus.FINISHED:
            exit(0)
        else:
            exit(1)

    if args.start:
        _start_app()
    sleep(0.2)  # prevent logger output
    LOGGER.info("DATAGERRY successfully started")

if __name__ == "__main__":

    welcome_string = colored('Welcome to DATAGERRY \nStarting system with following parameters: \n{}\n', 'yellow')
    brand_string = colored('''
    ########################################################################

    @@@@@     @   @@@@@@@ @           @@@@@  @@@@@@@ @@@@@   @@@@@  @@    @@
    @    @    @@     @    @@         @@@@@@@ @@@@@@@ @@@@@@  @@@@@@ @@@  @@@
    @     @  @  @    @   @  @       @@@   @@ @@@     @@   @@ @@   @@ @@  @@
    @     @  @  @    @   @  @       @@       @@@@@@  @@   @@ @@   @@  @@@@
    @     @ @    @   @  @    @      @@   @@@ @@@@@@  @@@@@@  @@@@@@   @@@@
    @     @ @@@@@@   @  @@@@@@      @@   @@@ @@@     @@@@@   @@@@@     @@
    @     @ @    @   @  @    @      @@@   @@ @@@     @@ @@@  @@ @@@    @@
    @    @ @      @  @ @      @      @@@@@@@ @@@@@@@ @@  @@@ @@  @@@   @@
    @@@@@  @      @  @ @      @       @@@@@@ @@@@@@@ @@  @@@ @@  @@@   @@

    ########################################################################\n''', 'green')
    license_string = colored('''Copyright (C) 2023 becon GmbH
licensed under the terms of the GNU Affero General Public License version 3\n''', 'yellow')

    try:
        options = build_arg_parser()
        print(brand_string)
        print(welcome_string.format(options.__dict__))
        print(license_string)
        sleep(0.2)  # prevent logger output
        main(options)
    except Exception as e:
        import cmdb

        if cmdb.__MODE__ == 'DEBUG':
            import traceback

            traceback.print_exc()
        LOGGER.critical("There was an unforeseen error {}".format(e))
        LOGGER.info("DATAGERRY stopped!")
        exit(1)


#!/usr/bin/python3

"""
Einstein's riddle
The situation
  There are 5 houses in five different colors.
  In each house lives a person with a different nationality.
  These five owners drink a certain type of beverage, smoke a certain brand of cigar and keep a certain pet.
  No owners have the same pet, smoke the same brand of cigar or drink the same beverage.
  The question is: Who owns the fish?
Hints
  The Brit lives in the red house
  the Swede keeps dogs as pets
  The Dane drinks tea
  The green house is on the left of the white house
  The green house's owner drinks coffee
  The person who smokes Pall Mall rears birds
  The owner of the yellow house smokes Dunhill
  The man living in the center house drinks milk
  The Norwegian lives in the first house
  The man who smokes blends lives next to the one who keeps cats
  The man who keeps horses lives next to the man who smokes Dunhill
  The owner who smokes BlueMaster drinks beer
  The German smokes Prince
  The Norwegian lives next to the blue house
  The man who smokes blend has a neighbor who drinks water
"""

from ortools.sat.python import cp_model

def solve_fish():
  model = cp_model.CpModel()

  # House colors
  white = model.NewIntVar(1, 5, 'white')
  red = model.NewIntVar(1, 5, 'red')
  green = model.NewIntVar(1, 5, 'green')
  yellow = model.NewIntVar(1, 5, 'yellow')
  blue = model.NewIntVar(1, 5, 'blue')

  # Nationalities
  brit = model.NewIntVar(1, 5, 'brit')
  swede = model.NewIntVar(1, 5, 'swede')
  dane = model.NewIntVar(1, 5, 'dane')
  norwegian = model.NewIntVar(1, 5, 'norwegian')
  german = model.NewIntVar(1, 5, 'german')

  # Pets
  dog = model.NewIntVar(1, 5, 'dog')
  bird = model.NewIntVar(1, 5, 'bird')
  cat = model.NewIntVar(1, 5, 'cat')
  fish = model.NewIntVar(1, 5, 'fish')
  horse = model.NewIntVar(1, 5, 'horse')

  # Drinks
  tea = model.NewIntVar(1, 5, 'tea')
  coffee = model.NewIntVar(1, 5, 'coffee')
  milk = model.NewIntVar(1, 5, 'milk')
  water = model.NewIntVar(1, 5, 'water')
  beer = model.NewIntVar(1, 5, 'beer')

  # Smokes
  pall_mall= model.NewIntVar(1, 5, 'pal mall')
  dunhill = model.NewIntVar(1, 5, 'dunhill')
  blends = model.NewIntVar(1, 5, 'blends')
  bluemaster = model.NewIntVar(1, 5, 'bluemaster')
  prince=model.NewIntVar(1, 5, 'prince')

  # Add features to the main model
  model.AddAllDifferent([white, red, green, yellow, blue])
  model.AddAllDifferent([brit, swede, dane, norwegian, german])
  model.AddAllDifferent([dog, bird, cat, fish, horse])
  model.AddAllDifferent([tea, coffee, milk, water, beer])
  model.AddAllDifferent([pall_mall, dunhill, prince, blends, bluemaster])

  # Add relationships
  model.Add(brit == red)
  model.Add(swede == dog)
  model.Add(dane == tea)
  model.Add(white == green + 1)
  model.Add(coffee == green)
  model.Add(pall_mall == bird)
  model.Add(dunhill == yellow)
  model.Add(milk == 3)
  model.Add(norwegian == 1)

  # model.Add(kools == yellow)
  diff_cat_blends = model.NewIntVar(-4, 4, 'diff_cat_blends')
  model.Add(diff_cat_blends == cat - blends)
  model.AddAbsEquality(1, diff_cat_blends)

  diff_horse_dunhill = model.NewIntVar(-4, 4, 'diff_horse_dunhill')
  model.Add(diff_horse_dunhill == horse - dunhill)
  model.AddAbsEquality(1, diff_horse_dunhill)

  model.Add(bluemaster == beer)
  model.Add(german == prince)

  diff_norwegian_blue = model.NewIntVar(-4, 4, 'diff_norwegian_blue')
  model.Add(diff_norwegian_blue == norwegian - blue)
  model.AddAbsEquality(1, diff_norwegian_blue)

  diff_blends_water = model.NewIntVar(-4, 4, 'diff_blends_water')
  model.Add(diff_blends_water == blends - water)
  model.AddAbsEquality(1, diff_blends_water)

  # Use model to find solution
  solver = cp_model.CpSolver()
  status = solver.Solve(model)

  if status == cp_model.FEASIBLE:
    people = [brit, swede, dane, norwegian, german]
    fish_owner = [p for p in people if solver.Value(p) == solver.Value(fish)][0]
    print('The', fish_owner.Name(), 'owns the fish')
  else:
    print('No solutions were found.')

solve_fish()

"""Output
$ python3 einstein_fish_sat.py

The german owns the fish
"""


from functools import partial
import numpy as np
import jax
from jax.experimental.pjit import pjit
from jax.sharding import PartitionSpec as P
from flax.core.frozen_dict import freeze
from .utils import \
    add_idxes, collate_fn_wrapper, pred_fn_wrapper, default_output_fn

class Predictor:
    def __init__(self,
                 deployer,
                 collate_fn,
                 pred_fn,
                 output_fn=None,
                 params_sharding_rules=None):
        """
        初始化实例变量，处理输出函数，完成部署参数设置和参数初始化，包装预测函数，注意partial函数
        """
        self._deployer = deployer        #部署器对象，负责模型的部署和运行
        self._collate_fn = partial(collate_fn_wrapper, collate_fn=collate_fn)     #数据组合函数的包装，用于将数据组合成批次

        self._params_sharding_rules = params_sharding_rules   #参数分片规则
        self._pred_fn = partial(                   #包装后的预测函数，用于执行实际的模型预测
            pred_fn_wrapper,
            pred_fn=pred_fn,
            under_pmap=self._deployer.mesh is None)
        self._params_spec = None          #模型参数规范
        self._p_pred_step = None          #预测时的时间步

        if output_fn is None:             #输出函数
            self._output_fn = default_output_fn
        else:
            self._output_fn = output_fn

    def setup_running_step(self,
                           pred_fn,
                           dummy_batch,
                           params,
                           params_sharding_rules):
        """
        设置运行步骤，根据部署器的情况选择使用jax.pmap或pjit进行批处理
        """
        if self._deployer.mesh is None:          #不在分布式环境下，可以使用jax.pmap进行批处理
            self._p_pred_step = jax.pmap(pred_fn, axis_name='batch')
        else:
            data_spec = {                        #指定数据划分方式
                key: P(*(('dp',) + (None,) * (len(value.shape) - 1)))
                for key, value in dummy_batch.items()
            }

            self._params_spec = self._deployer.get_params_spec(                 #获取模型参数划分方式规范
                params=params, params_sharding_rules=params_sharding_rules)

            self._p_pred_step = pjit(
                pred_fn,
                in_shardings=(None, self._params_spec, data_spec),   #第一个参数无需划分，模型参数按照self._params_spec进行划分，数据按照data_spec划分
                out_shardings=None)

    def predict(self,
                examples,
                per_device_batch_size,
                params,
                params_meshed=False,
                desc=''):
        """
        用于执行预测任务，执行预测前进行一系列数据处理、模型参数处理、批处理操作
        examples:数据列表
        per_device_batch_size:每设备批次大小
        params:模型参数
        """
        params = freeze(params)     #冻结参数，确保参数是不可变的，以便在后续的处理中不会被修改

        raw_n_inputs = len(examples)      #原始输入数据样本数量
        _, global_batch_size = self._deployer.process_batch_size(     #_表示一个占位符，该变量的值后续并不会被使用到，因此我们不关心其取值
            per_device_batch_size=per_device_batch_size)       #根据每设备批次大小计算全局批次大小
        examples = examples + [examples[0]] * (global_batch_size - 1)   #将examples复制并补齐，确保批次大小是全局批次大小的整数倍
        examples = add_idxes(examples=examples)

        params = self._deployer.process_to_run_model(params)  #参数处理

        data_batches = self._deployer.get_model_input_batches(         #获取数据批次
            examples=examples,
            per_device_batch_size=per_device_batch_size,
            collate_fn=self._collate_fn,
            shuffle=False,
            shuffle_rng=None,
            desc=f'Predicting ({desc})')

        preds = []
        for batch in data_batches:
            if self._p_pred_step is None:           #如果运行步骤尚未设置，则设置运行步骤
                self.setup_running_step(
                    pred_fn=self._pred_fn,
                    dummy_batch=batch,
                    params=params,
                    params_sharding_rules=self._params_sharding_rules)

            if (self._params_spec is not None) and (not params_meshed):   #如果存在参数分片规则并且尚未切分，调用shard_params函数进行参数分片
                params = self._deployer.shard_params(
                    params=params, params_spec=self._params_spec)
                params_meshed = True

            pred_rng = self._deployer.process_to_run_model(         #生成用于预测的随机数
                self._deployer.gen_rng(), is_prng_key=True)

            batch_preds_with_idxes = self._deployer.run_model_step(   #执行定义运行步骤，得到带有序列的预测结果
                step_fn=self._p_pred_step,
                input_args=(pred_rng, params, batch))

            batch_preds = self._deployer.process_batch_preds(       #处理预测结果，得到带索引的预测结果？？
                batch_preds_with_idxes=batch_preds_with_idxes)
            batch_preds = jax.tree_util.tree_map(np.asarray, batch_preds)   #预测结果转化为numpy数组

            batch_preds = self._output_fn(batch_preds)
            assert isinstance(batch_preds, list) and \
                   len(batch_preds) == global_batch_size    #断言检查，确保输出函数返回的结果是一个列表，且长度等于全局批次大小
            preds.extend(batch_preds)

        return preds[:raw_n_inputs]


# Author: kk.Fang(fkfkbill@gmail.com)

def code(rule, entries, **kwargs):
    schema_name: str = kwargs["schema_name"]
    cmdb_connector = kwargs["cmdb_connector"]

    sql = f"""
    select t.table_name, t.partition_name, count(1) as subpart_count
        from DBA_TAB_SUBPARTITIONS t
        where t.table_owner = '{schema_name}'
        group by t.table_name, t.partition_name
        having count(1) >= {rule.gip('subpart_tab_num')}
    """
    for i in cmdb_connector.select_dict(sql):
        yield i

code_hole.append(code)


import tensorflow as tf
import numpy as np

from baselines.common import tf_util as U
from gym import error, spaces, utils
from math import *

def getAngle(x1,y1,x2,y2):
    s = x1*x2 + y1*y2
    v = x1*y2 - y1*x2

    l1 = np.sqrt(x1*x1 + y1*y1)
    l2 = np.sqrt(x2*x2 + y2*y2)
    pr = l1*l2

    if l1 < 10e-4 or l2 < 10e-4 or pr < 10e-3:
        return 0.0

    s = s / pr
    if s < -1.0:
        s = -1.0
    if s > 1.0:
        s = 1.0

    alpha = np.arccos(s)

    if v < 0.0:
        alpha *= -1.0

    return alpha


#passo 1 = importar banco de dados

#passo 2 = calcular o faturamento de cada loja

#passo 3 = calcular a quantidade de produtos vendidos de cada loja

#passo 4 = calcular o ticket médio de cada loja

# Passo 1
import pandas as pd
tabela_vendas = pd.read_excel("Vendas.xlsx")
display(tabela_vendas)

# Passo 2
tabela_faturamento = tabela_vendas[["ID Loja", "Valor Final"]].groupby("ID Loja").sum()   # agrupar por ID Loja e fazer a soma do valor final
tabela_faturamento = tabela_faturamento.sort_values(by = "ID Loja", ascending= True) # a função sort_value vai ordenar em ordem alfabética ascendente (ascending = True)
display(tabela_faturamento)

# Passo 3
tabela_quantidade = tabela_vendas[["ID Loja", "Quantidade"]].groupby("ID Loja").sum()  # groupby("nome da coluna") em sql group by, vai definir o indice para "ID Loja"
display(tabela_quantidade)

# Passo 4
ticket_medio = (tabela_faturamento['Valor Final'] / tabela_quantidade['Quantidade']).to_frame()
ticket_medio = ticket_medio.rename(columns={0:"Ticket Medio"}) # trocar nome da coluna '0' para Ticket Medio
display(ticket_medio)


import django
from django.db import models
from django.contrib.auth.models import User
from BynryConsumerModuleapp.models import City
from consumerapp.models import ConsumerDetails

# Create your models here.
ROLE_STATUS = (
    ('Active', 'Active'),
    ('Inactive', 'Inactive'),
)
IS_REGISTERED = (
    (True, True),
    (False, False),
)

# class WebUserProfile(User):
#     consumer_id = models.ForeignKey(ConsumerDetails, blank=True, null=True)
#
#     status = models.CharField(max_length=20, default='Active', choices=ROLE_STATUS)
#     created_by = models.CharField(max_length=500, blank=False, null=False)
#     updated_by = models.CharField(max_length=500, blank=True, null=True)
#     created_on = models.DateTimeField(default=django.utils.timezone.now)
#     updated_on = models.DateTimeField(blank=True, null=True)
#     is_deleted = models.BooleanField(choices=IS_DELETED, default=False)
#
#     def __unicode__(self):
#         return unicode(str(self.username))

class ConsumerData(models.Model):
    consumer_no = models.ForeignKey(ConsumerDetails, blank=True, null=True)
    name = models.CharField(max_length=200, blank=False, null=True)
    email_id = models.CharField(max_length=50, blank=True, null=True)
    contact_no = models.CharField(max_length=50, blank=True, null=True)
    city = models.ForeignKey(City, blank=False, null=True)
    is_registered = models.BooleanField(choices=IS_REGISTERED, default=False)
    created_by = models.CharField(max_length=500, blank=False, null=True)
    updated_by = models.CharField(max_length=500, blank=True, null=True)
    created_on = models.DateTimeField(default=django.utils.timezone.now)
    updated_on = models.DateTimeField(blank=True, null=True)

    def __unicode__(self):
        return unicode(str(self.name))


import numpy as np
import matplotlib.pyplot as plt
from scipy.special import erfc

# Define the signal-to-noise ratio (SNR) in dB
snr_dB = np.arange(0, 15, 1)
snr = 10 ** (snr_dB / 10)

# Define the number of bits
# N = 10**6

# Calculate the BER for ASK modulation
ber1 = 0.5 * (erfc(np.sqrt(0.1*snr))) # BASK 50% depth
ber2 = 0.5 * (erfc(np.sqrt(0.5*snr))) # BASK 100% depth

# Plot the BER diagram
plt.semilogy(snr_dB, ber1, 'o-', color='r',label = 'BASK 50%')
plt.semilogy(snr_dB, ber2, 'o-', color='g',label = 'BASK 100%')
plt.xlabel('Signal-to-Noise Ratio (dB)')
plt.ylabel('Bit Error Rate (BER)')
plt.title('BER Diagram for ASK Modulation')
plt.grid(True)
plt.yticks(fontsize=5)
plt.xticks(snr_dB,fontsize=12)
plt.legend()
plt.show()


# %%
import pandas as pd
import numpy as np
from collections import defaultdict, Counter
from typing import Tuple
from urllib.parse import urljoin

import torch
import torch.nn.functional as F

import re
import os
import pickle

# %% [markdown]
# ## Create corpus, vocabulary, word2idx and idx2word

# %%
def clean_input(sentence: list) -> list:
    # sentence = re.sub(r'[0-9０-９a-zA-Zａ-ｚＡ-Ｚ]+', " ", sentence)
    # sentence = re.sub(r'[\．_－―─！＠＃＄％＾＆\-‐|\\＊\“（）＿■×+α※÷⇒—●★☆〇◎◆▼◇△□(：〜～＋=)／*&^%$#@!~`){}［］…\[\]\"\'\”\’:;<>?＜＞〔〕〈〉？、。・,\./『』【】「」→←○《》≪≫\n\u3000]+', " ", sentence)
    sentence = sentence.replace('-', ' ').replace('\\', ' ').replace(',', ' ').split()
    return sentence

# %%
def make_corpus(full_path: str) -> list:
    corpus = []
    df_full = pd.read_csv(full_path, sep=';', header=None)
    for _, row in df_full.iterrows():
        corpus.append(clean_input(row[0]))
    return corpus

# %%
def make_vocabulary(corpus: list) -> Tuple:
    '''
    make vocabulary
    '''
    MIN_CNT = 0  # trim words
    cnter = Counter()
    for sentence in corpus:
        for token in sentence:
            cnter[token] += 1

    trimmed_vocabulary = set()

    for sentence in corpus:
        for token in sentence:
            if cnter[token] > MIN_CNT:
                trimmed_vocabulary.add(token)

    trimmed_vocabulary = sorted(trimmed_vocabulary)

    word2idx = {w: idx+1 for (idx, w) in enumerate(trimmed_vocabulary)}
    word2idx.update({"<PAD>": 0})

    idx2word = {idx+1: w for (idx, w) in enumerate(trimmed_vocabulary)}
    idx2word.update({0: "<PAD>"})

    print("trimmed_vocabulary size: {}".format(len(trimmed_vocabulary)))
    print("word2idx size: {}".format(len(word2idx)))
    print("idx2word size: {}".format(len(idx2word)))

    return trimmed_vocabulary, word2idx, idx2word

# %%
def save_to_pickle(path, trimmed_vocabulary, word2idx, idx2word, corpus):
    name = path.split('.')[0].split('/')[1] + '/'
    save_url = '/mnt/LSTA6/data/zhu/Rekihaku/all_data/corpus/'
    save_url = urljoin(save_url, name)

    print("Saving to {}".format(save_url))
    with open(urljoin(save_url, 'voc.p'), 'wb') as f:
        pickle.dump(trimmed_vocabulary, f)
        print("Saved: {}".format(urljoin(save_url, 'voc.p')))
    with open(urljoin(save_url, 'word2idx.p'), 'wb') as f:
        pickle.dump(word2idx, f)
        print("Saved: {}".format(urljoin(save_url, 'word2idx.p')))
    with open(urljoin(save_url, 'idx2word.p'), 'wb') as f:
        pickle.dump(idx2word, f)
        print("Saved: {}".format(urljoin(save_url, 'idx2word.p')))
    with open(urljoin(save_url, 'corpus.p'), 'wb') as f:
        pickle.dump(corpus, f)
        print("Saved: {}".format(urljoin(save_url, 'corpus.p')))
    print()

# %%
def execute():
    for full_path in ['tokenize/name_of_item_texts.full', 'tokenize/name_of_collection_texts.full', 'tokenize/notes_texts.full']:
        corpus = make_corpus(full_path)
        print("path: {}".format(full_path))
        trimmed_vocabulary, word2idx, idx2word = make_vocabulary(corpus)
        save_to_pickle(full_path, trimmed_vocabulary, word2idx, idx2word, corpus)

execute()

# %%
with open('name_of_item_list_18492.p', 'rb') as f:
    name_of_item_list = pickle.load(f)

assert len(name_of_item_list) == 18492

# %%
def make_mapping(path):
    # load pickles
    voc_url = urljoin(path, 'voc.p')
    idx2word_url = urljoin(path, 'idx2word.p')
    word2idx_url = urljoin(path, 'word2idx.p')
    corpus_url = urljoin(path, 'corpus.p')
    with open(voc_url, 'rb') as f:
        voc = pickle.load(f)
    with open(idx2word_url, 'rb') as f:
        idx2word = pickle.load(f)
    with open(word2idx_url, 'rb') as f:
        word2idx = pickle.load(f)
    with open(corpus_url, 'rb') as f:
        corpus = pickle.load(f)

    max_len = 0
    idx_vectors = []

    def sentence2vector(sentence: list):
        # return torch.tensor([word2idx[w] for w in sentence if w in word2idx], dtype=torch.long)
        return [word2idx[w] for w in sentence if w in word2idx]
        # return np.array([word2idx[w] for w in sentence if w in word2idx])

    for sentence in corpus:
        idx_vector = sentence2vector(sentence)
        idx_vectors.append(idx_vector)
        max_len = max(max_len, len(idx_vector))

    assert len(idx_vectors) == 18492
    assert len(corpus) == 18492

    name2vecs = defaultdict(lambda: defaultdict(list))
    key = path.split("corpus/")[1].replace('_texts/', '')

    for name, sentence in zip(name_of_item_list, corpus):
        # print("name: {}, sentence: {}".format(name, sentence))
        sentence_vec = sentence2vector(sentence)
        # sentence_vec = sentence_vec + [0] * (max_len - len(sentence_vec))  # zero padding
        sentence_vec = np.array(sentence_vec)
        # assert len(sentence_vec) == max_len
        # while len(sentence_vec) < max_len:
            # sentence_vec.append(0)
        # sentence_vec = torch.tensor(sentence_vec, dtype=torch.long)
        name2vecs[name][key].append(sentence_vec)

    print("corpus: {}, max_len = {}".format(path, max_len))
    return dict(name2vecs), max_len

# %%
# corpus_url = '/mnt/LSTA6/data/zhu/Rekihaku/all_data/corpus/notes_texts/'                  # max_len = 410
# corpus_url = '/mnt/LSTA6/data/zhu/Rekihaku/all_data/corpus/name_of_item_texts/'           # max_len = 44
corpus_url = '/mnt/LSTA6/data/zhu/Rekihaku/all_data/corpus/name_of_collection_texts/'       # max_len = 35

name2vecs, max_len = make_mapping(corpus_url)

# %%
save_url = urljoin(corpus_url, 'name2vecs.p')
print(save_url)
with open(save_url, 'wb') as f:
    pickle.dump(name2vecs, f)

# %%
# len(name2vecs)
name2vecs['疱瘡紅摺絵松洛画（おきあがりかるくはづむやはねのおと）']

# %%
save_url = urljoin(corpus_url, 'voc.p')
with open(save_url, 'rb') as f:
    voc = pickle.load(f)


import random

alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
letter = random.choice(alphabet)
guess_count = 0
correct = alphabet.index(letter) + 1
while guess_count < 3:
    guess = input("Guess what letter of the alphabet " + letter + " is:")
    if guess == str(correct):
        print("You got it right!")
        break
    elif guess != str(correct):
        guess_count = guess_count + 1
        if guess_count == 3:
            break
print("Hope you enjoyed the game!")


from ..repositories.food_repository import FoodRepository
from ..repositories.nutritional_record_repository import NutritionalRecordRepository
from ..utils.food_selector import FoodSelector
from ..utils.glycemic_status_calculator import GlycemicStatusCalculator

class SuggestionService():
    def __init__(self):
        self.food_repository = FoodRepository()
        self.nutritional_record_repository = NutritionalRecordRepository()
        self.food_selector = FoodSelector()
        self.glycemic_status_calculator = GlycemicStatusCalculator()

    def get_suggestion(self, user_id):
        last_nutritional_record = self.nutritional_record_repository.get_last_nutritional_record(user_id)
        if last_nutritional_record is None:
            return
        glycemic_status = self.glycemic_status_calculator.calculate_glycemic_status(last_nutritional_record)
        if glycemic_status == "hypoglycemia":
            food = self.food_selector.select_food()
            return "Tiene muy poca azúcar en sangre, beba urgentemente un vaso de bebida azucarada, y después coma un " \
                   "alimento sólido. " + food + "Cuando sus niveles de azúcar en sangre se normalicen, se le recomienda " \
                      "consultar a su nutricionista."
        elif glycemic_status == "hyperglycemia":
            return "Tiene exceso de azúcar en sangre, se le recomienda beber agua, tomar su medicación  y " \
                   "consultar a su  nutricionista."
        else:
            return "Su nivel de azúcar en sangre es normal, se le recomienda mantener una dieta equilibrada " \
                   "y hacer ejercicio regularmente. No deje de tomar su medicación y consultar a su médico nutricionista."


from jsonstruct.pickler import Pickler
from jsonstruct.unpickler import Unpickler
from jsonstruct.backend import JSONBackend
from jsonstruct.version import VERSION

# ensure built-in handlers are loaded
__import__('jsonstruct._handlers')

__all__ = ('encode', 'decode')
__version__ = VERSION

json = JSONBackend()

# Export specific JSONPluginMgr methods into the jsonstruct namespace
set_preferred_backend = json.set_preferred_backend
set_encoder_options = json.set_encoder_options
load_backend = json.load_backend
remove_backend = json.remove_backend

def encode(value, max_depth=None, is_filter_none_attr=True):
    """
    Return a JSON formatted representation of value, a Python object.

    The keyword argument 'unpicklable' defaults to False.
    If set to False, the output will not contain the information
    necessary to turn the JSON data back into Python objects.

    The keyword argument 'max_depth' defaults to None.
    If set to a non-negative integer then jsonstruct will not recurse
    deeper than 'max_depth' steps into the object.  Anything deeper
    than 'max_depth' is represented using a Python repr() of the object.

    >>> encode('my string')
    '"my string"'
    >>> encode(36)
    '36'

    >>> encode({'foo': True})
    '{"foo": true}'

    >>> encode({'foo': True}, max_depth=0)
    '"{\\'foo\\': True}"'

    >>> encode({'foo': True}, max_depth=1)
    '{"foo": "True"}'

    """
    j = Pickler(unpicklable=False,
                max_depth=max_depth,
                is_filter_none_attr=is_filter_none_attr)
    return json.encode(j.flatten(value))

def decode(string, cls=None):
    """
    Convert a JSON string into a Python object.

    >>> str(decode('"my string"'))
    'my string'
    >>> decode('36')
    36
    """
    j = Unpickler()
    return j.restore(json.decode(string), cls)


import csv

def calculate_summary_stats(depth_values):
    """Calculate summary statistics for a list of depth values."""
    stats = {
        'mean': sum(depth_values) / len(depth_values),
        'median': sorted(depth_values)[len(depth_values) // 2],
        'min': min(depth_values),
        'max': max(depth_values),
    }
    return stats

def process_depth_file(file_path, output_csv):
    haplotype_stats = {}

    with open(file_path, 'r') as file:
        for line in file:
            # Assuming the depth file has three columns: chromosome, position, and depth
            chromosome, position, depth = line.strip().split('\t')

            # Extract the haplotype information from the chromosome name (modify as per your naming convention)
            haplotype = chromosome.split(':')[0]

            # If haplotype not encountered before, initialize an empty list for depth values
            if haplotype not in haplotype_stats:
                haplotype_stats[haplotype] = []

            # Add depth value to the haplotype's list
            haplotype_stats[haplotype].append(int(depth))

    # Calculate summary statistics for each haplotype
    haplotype_summary_stats = {
        haplotype: calculate_summary_stats(depth_values)
        for haplotype, depth_values in haplotype_stats.items()
    }

    # Get the fieldnames from the first haplotype's stats
    fieldnames = ['Haplotype'] + list(next(iter(haplotype_summary_stats.values())).keys())

    # Write the summary statistics to a CSV file
    with open(output_csv, 'w', newline='') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        for haplotype, stats in haplotype_summary_stats.items():
            writer.writerow({'Haplotype': haplotype, **stats})

# Usage example
depth_file = '/g/data/xl04/ka6418/sequence_alignment/bam_outputs/tiliqua_rugosa/pacbio/merged/assembly.haplotype1/coverage.txt'  # Replace with the path to your depth file
output_csv = 'summary_stats.csv'  # Replace with the desired path for the output CSV file
process_depth_file(depth_file, output_csv)


# -*- coding: utf-8 -*-
"""
Created on Tue Nov 20 21:41:19 2018

@author: emreAkoglu
"""

#kutuphaneler
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

veriler = pd.read_csv('eksikveriler.csv')

ulke = veriler.iloc[:,0:1].values
print(ulke)

from sklearn.preprocessing import LabelEncoder ,OneHotEncoder

le = LabelEncoder() # Değerleri bir sayıya çeviriyor, her değeri bir sayıya çeviriyor

ulke[:,0] = le.fit_transform(ulke[:,0])

print(ulke)

#from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder(categorical_features='all') # Her başlık altına denk gelen sayıyı 1 yaparak çeviriyor

ulke = ohe.fit_transform(ulke).toarray()

print (ulke)

sonuc = pd.DataFrame(data = ulke, index = range(22))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author rsg
#
from collections import deque

class Node(object):

    def __init__(self, left=None, right=None, data=None):
        self.left = left
        self.right = right
        self.data = data

node_f = Node(data='f')
node_d = Node(data='d')
node_c = Node(data='c')
node_e = Node(left=node_f, data='e')
node_b = Node(left=node_d, right=node_e, data='b')
node_a = Node(left=node_b, right=node_c, data='a')
root = node_a

def inorder_traversal(root):
    stack = list()
    current = root
    done = 0

    while not done:
        if current:
            stack.append(current)
            current = current.left
        else:
            if stack:
                current = stack.pop()
                print(current.data)
                current = current.right
            else:
                done = 1

class Node2(object):

    def __init__(self, data):
        self.data = data
        self.sub = []

# node_f = Node2(data='f')
# node_d = Node2(data='d')
# node_c = Node2(data='c')
# node_e = Node2(data='e')
# node_e.sub.append(node_f)
# node_b = Node2(data='b')
# node_b.sub.append(node_d)
# node_b.sub.append(node_e)
# node_a = Node2(data='a')
# node_a.sub.append(node_b)
# node_a.sub.append(node_c)
# root = node_a

def depth_first_search(root):
    "Traverses through a tree depth-first by putting nodes on a stack"
    stack = deque()
    result_list = []
    stack.append(root)
    while len(stack) != 0:
        # Take off the node at the top of the stack, add it to the list
        temp_node = stack.pop()
        result_list.append(temp_node.data)
        # Add the children of that node to the top of the stack
        if temp_node.right is not None:
            stack.append(temp_node.right)
        if temp_node.left is not None:
            stack.append(temp_node.left)
    return result_list

if __name__ == '__main__':
    print(depth_first_search(root))


import os

import torch
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.tensorboard import SummaryWriter

from ignite import engine, metrics
from tqdm import tqdm

def train_model(model, train_loader, val_loader_make, epochs, log_interval, checkpoint_dir):
    writer = SummaryWriter()
    device = 'cuda' if torch.cuda.is_available() else 'cpu'

    if torch.cuda.device_count() > 1:
        print("Let's use", torch.cuda.device_count(), "GPUs!")
        model = torch.nn.DataParallel(model)

    loss_fn = torch.nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters())

    m = {'accuracy': metrics.Accuracy(), 'loss': metrics.Loss(loss_fn)}
    trainer = engine.create_supervised_trainer(
        model, optimizer, loss_fn,
        output_transform=lambda x, y, y_pred, loss: (loss.item(), y, y_pred), device=device
    )
    evaluator = engine.create_supervised_evaluator(model, metrics=m, device=device)

    acc_metric = metrics.RunningAverage(metrics.Accuracy(output_transform=lambda x: (x[2], x[1])))
    acc_metric.attach(trainer, 'running_avg_accuracy')
    avg_loss = metrics.RunningAverage(output_transform=lambda x: x[0])
    avg_loss.attach(trainer, 'running_avg_loss')

    desc = "Loss: {:.7f}, Accuracy: {:.7f}"
    train_batches = len(train_loader)

    pbar = tqdm(
        initial=0, leave=False, total=train_batches,
        desc=desc.format(0, 0)
    )

    @trainer.on(engine.Events.ITERATION_COMPLETED)
    def log_training_loss(engine):
        iter = (engine.state.iteration - 1) % train_batches + 1

        avg_loss = engine.state.metrics['running_avg_loss']
        avg_accuracy = engine.state.metrics['running_avg_accuracy']

        if iter % log_interval == 0:
            writer.add_scalar('Loss/train', avg_loss, engine.state.iteration - 1)
            writer.add_scalar('Accuracy/train', avg_accuracy, engine.state.iteration - 1)
            pbar.desc = desc.format(avg_loss, avg_accuracy)
            pbar.update(log_interval)

    @trainer.on(engine.Events.EPOCH_COMPLETED)
    def log_validation_results(engine):
        evaluator.run(val_loader_make())

        metrics = evaluator.state.metrics
        avg_accuracy = metrics['accuracy']
        avg_loss = metrics['loss']

        writer.add_scalar('Accuracy/val', avg_accuracy, engine.state.iteration - 1)
        writer.add_scalar('Loss/val', avg_loss, engine.state.iteration - 1)

        tqdm.write(
            "Validation Results - Epoch: {}  Avg accuracy: {:.6f} Avg loss: {:.6f}"
            .format(engine.state.epoch, avg_accuracy, avg_loss))

        pbar.n = pbar.last_print_n = 0

        # Checkpoint
        model_name = type(model).__name__
        checkpoint_path = os.path.join(checkpoint_dir, f"{model_name}-{engine.state.epoch}epoch-{avg_accuracy}valacc.pt")
        torch.save(model.state_dict(), checkpoint_path)

    trainer.run(train_loader, max_epochs=epochs)
    pbar.close()


import gym
import pybulletgym
import tensorflow as tf

def main():
    env = gym.make("HopperPyBulletEnv-v0")
    env.render("human")
    env.reset()
    while True:
        action = env.action_space.sample()
        o,r,d,i = env.step(action)
        if d:
            env.reset()

def name_scope_test():
    with tf.name_scope('test'):
        inputs = tf.keras.layers.Input([1,], name='ob')
        net = tf.keras.layers.Dense(units=64, name='fc1')(inputs)
        G = tf.keras.Model(inputs=inputs, outputs=net)
        layers = G.get_layer(name='fc1')
        print(layers[0].name)

if __name__ == '__main__':
    # print('test')
    name_scope_test()


import re
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')

from nltk import word_tokenize, pos_tag, pos_tag_sents
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.corpus import wordnet
#nltk.download('averaged_perceptron_tagger')
from collections import Counter
from itertools import chain
import pandas as pd
from nltk import word_tokenize, pos_tag

def cleanText(path2, cleanedPath):
    # Read the input file in path2 and write to cleaned file cleanedPath
    fp = open(path2,"rb") #read in binary mode
    text = fp.read().decode('utf-8', 'ignore').lower() #decode it in utf-8. Wherever it doesn't work, ignore the word
    fp.close()

    fp = open(cleanedPath, "w+", encoding="utf-8")
    fp.write(str(text))
    fp.close()

# download the file from the following path
# https://www.kaggle.com/kazanova/sentiment140/downloads/training.1600000.processed.noemoticon.csv/2
path2='Dataset/Sentiment140/training.1600000.processed.noemoticon.csv'
cleanedPath = "Dataset/Sentiment140/training.csv"

# Clean up the input file of non-ascii characters
cleanText(path2, cleanedPath)

# Read from the cleanedPath
df=pd.read_csv(cleanedPath)
df.columns = ["target","ids","date","flag","user","text"]
df = df[["target","text"]]
print(df.head(5))

print(df["target"].unique())
print(len(df[df["target"]==0]))
print(len(df[df["target"]==4]))

#delete tags "@Lynn"

# Remove the anchor tags
def removeTags(s):
    s = re.sub("@[a-zA-Z0-9_]+"," ", s) #remove the @<the word>
    s = re.sub("#[a-zA-Z0-9_]+"," ", s) #remove the #<the word>
    return s

df.columns = ["target","ids","date","flag","user","text"]
df["text"] = df["text"].apply(removeTags)

df.head(10)


# Part one

inp = open('input.txt', 'r').read().splitlines()

calories = []
holding = 0

for i in range(0,len(inp)):
    line = inp[i]
    if (line != ''):
        holding += int(line)
    if (line == '' or i == len(inp)-1):
        calories.append(holding)
        holding = 0
        continue

print(sorted(calories)[len(calories)-1])


from sqlalchemy import Table, select, Column,Date, String, ForeignKey, Integer, create_engine
from sqlalchemy.orm import sessionmaker
from rsr import Person, Score, Base, engine
from stats import  get_clan_members
import pandas as pd

def use_db():
    # Import the DB Models to be created.
    Session = sessionmaker(bind=engine)
    Session.configure(bind=engine)  # once engine is available
    session = Session()
    return session
sess = use_db()

rsr_clan = get_clan_members()

for p in rsr_clan[['pid','Player']].values.tolist():
    q = sess.query(Person).filter(Person.pid == p[0])
    if len(q.all())==0:
        #Record does not exist
        sess.add(Person(p[0],p[1]))
        print(f"Adding {p}")
    else:
        print(f"Already exists {p}")
sess.commit()

# Sanity check see what is there
for s in sess.query(Person).all():
    print(str(s))


# The condition to perform a Binary Search
# is to insure that the list is sorted

def BinarySearch(target, start, end, arr):
    if start > end:
        return 'Not found'
    # Declaring middle index by finding the avg of the start and end
    middle = (start + end) // 2
    # Base condition | eventually comes here if target is in arr
    if arr[middle] == target:
        return f'{target} was found at index {middle}'

    elif target > arr[middle]:
        return BinarySearch(target, middle+1, end, arr)

    elif target < arr[middle]:
        return BinarySearch(target, start, middle-1, arr)

if __name__ == '__main__':
    array = [x for x in range(20)]
    lookfor = int(input("Please input a number: "))
    result = BinarySearch(lookfor, 0, len(array)-1, array)
    print(result)


import os
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
import researchpy as rp

"""
Fonction ANOVA entre variable qualitative et quantitative
"""

def anova_function(data: pd.DataFrame, variable1: str, variable2: str, title: str,
                   with_cf: bool, location: str, minimum: int, user_id: str):
    # Filtrer lieu
    if location != "Tous les lieux":
        data = data.loc[data["Lieu"] == location]

    # Si sans cf
    if not with_cf:
        data = data.loc[data["cf"] != "cf."]

    data = data[[variable1, variable2]]
    data = data.dropna()

    # Minimum d'occurences par modalité
    if minimum < 3:
        minimum = 3

    variables_size = data.groupby([variable1]).size()
    print(variables_size)
    for index, value in variables_size.items():
        if value < minimum:
            data = data.loc[data[variable1] != index]

    # dossier d'export
    directory = "files/anova_" + user_id
    if not os.path.exists(directory):
        os.makedirs(directory)

    # graphe boîte à moustaches
    plt.figure()
    sns.boxplot(x=variable1, y=variable2, data=data)
    plt.title(title)
    plt.savefig(directory + "/boite_moustaches.svg", bbox_inches='tight')

    writer = pd.ExcelWriter(directory + '/anova.xlsx', engine='xlsxwriter')
    data.to_excel(writer, sheet_name='Données')

    # Analyse des données
    analyzed_data = rp.summary_cont(data[variable2].groupby(data[variable1]))
    data_excel = pd.DataFrame(analyzed_data)
    data_excel.to_excel(writer, sheet_name='Analyse des données')
    mods = data[variable1].unique()

    args = []
    for mod in mods:
        args.append(data[variable2][data[variable1] == mod])

    # Validité de l'ANOVA
    anova_validity = True

    # Test de normalité de Shapiro-Wilk
    i = 0
    shapiro_dataframe = pd.DataFrame([], )
    for arg in args:
        statistic_shapiro, p_value_shapiro = stats.shapiro(arg)
        if p_value_shapiro < 0.05:
            interpretation = "La modalité ne suit pas une distribution normale."
            anova_validity = False
        else:
            interpretation = "La modalité suit une distribution normale."

        shapiro_row = pd.DataFrame([[mods[i], statistic_shapiro, p_value_shapiro, interpretation]], columns=['Modalité', 'Statistique', 'P-value', 'Interprétation'] )
        shapiro_dataframe = pd.concat([shapiro_dataframe, shapiro_row])

        i = i + 1
    shapiro_dataframe.to_excel(writer, sheet_name='Test Shapiro-Wilk', index=False)

    # Test de Levene
    statistic_levene, p_value_levene = stats.levene(*args, center="mean")
    if p_value_levene < 0.05:
        interpretation = "Les variances des populations ne sont pas homogènes."
        anova_validity = False
    else:
        interpretation = "Les variances des populations sont homogènes."
    levene_dataframe = pd.DataFrame({"Statistiques": [statistic_levene], "P-value": [p_value_levene], "Interprétation": interpretation})
    levene_dataframe.to_excel(writer, sheet_name='Test Levene', index=False)

    # Anova one-way
    statistic_anova, p_value_anova = stats.f_oneway(*args)
    if p_value_anova < 0.05:
        interpretation = "Il y a une différence entre les moyennes des modalités."
    else:
        interpretation = "Il n'y a pas de différence entre les moyennes des modalités."
    anova_dataframe = pd.DataFrame({"Statistiques": [statistic_anova], "P-value": [p_value_anova], "Interprétation": interpretation})
    anova_dataframe.to_excel(writer, sheet_name='Test ANOVA', index=False)

    if anova_validity:
        kruskal_dataframe = pd.DataFrame([], )
    else:
        statistic_kruskal, p_value_kruskal = stats.kruskal(*args)
        if p_value_kruskal < 0.05:
            interpretation = "Au moins un échantillon domine stochastiquement un autre échantillon."
        else:
            interpretation = "Il n'y a pas de différences entre les échantillons."
        kruskal_dataframe = pd.DataFrame({"Statistiques": [statistic_kruskal], "P-value": [p_value_kruskal], "Interprétation": interpretation})
        kruskal_dataframe.to_excel(writer, sheet_name='Test Kruskal-Wallis', index=False)

    writer.save()

    return shapiro_dataframe, levene_dataframe, anova_dataframe, kruskal_dataframe


import streamlit as st
import pickle
import numpy as np

data = pickle.load(open("saved_steps.pkl", "rb"))

regressor = data["model"]
le_education = data["lbl_education"]
le_country = data["lbl_country"]

def show_prediction_page():
    st.title("Jobs4U Salary Prediction")
    st.write("""### Use our tool to predict your salary""")

    education = (
        "Less than a Bachelors",
        "Bachelor’s degree",
        "Master’s degree",
        "Post grad",
    )

    countries = (
        "United States of America",
        "India",
        "Germany",
        "United Kingdom of Great Britain and Northern Ireland",
        "Canada",
        "France",
        "Brazil",
        "Spain",
        "Netherlands",
        "Australia",
        "Poland",
        "Italy",
        "Russian Federation",
        "Sweden",
        "Turkey",
        "Switzerland",
        "Israel",
        "Norway"
    )

    country = st.selectbox("Country", countries)
    education = st.selectbox("Education Level", education)

    expericence = st.slider("Years of Experience", 0, 50, 3)

    ok = st.button("Predict Salary")
    if ok:
        X = np.array([[country, education, expericence ]])
        X[:, 0] = le_country.transform(X[:,0])
        X[:, 1] = le_education.transform(X[:,1])
        X = X.astype(float)

        salary = regressor.predict(X)
        st.subheader(f"The estimated salary is ${salary[0]:.2f}")


from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Post(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, default=User)
    title = models.CharField(max_length=50)
    content = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    image = models.ImageField(null=True, blank=True, upload_to='images/')

    def __str__(self):
        return self.title + ' | ' + str(self.user)

    class Meta:
        ordering = ['-created']

class Profile(models.Model):
    user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
    bio = models.TextField()
    profile_pic = models.ImageField(null=True, blank=True, upload_to='images/profile')
    car = models.CharField(max_length=100, null=True, blank=True)
    mods = models.TextField(null=True, blank=True)
    pic_1 = models.ImageField(null=True, blank=True, upload_to='images/profile')
    pic_2 = models.ImageField(null=True, blank=True, upload_to='images/profile')
    pic_3 = models.ImageField(null=True, blank=True, upload_to='images/profile')

    def __str__(self):
        return str(self.user)


'''
Implement of positional encoding
Including:
    Absolute positional encoding
    Learnable positional encoding
    Convolution-based positional encoding
'''

from turtle import forward
import torch
import torch.nn as nn

class AbsolutePosEncoding(nn.Module):
    def __init__(self, max_length:int, embedding_dim:int):
        super().__init__()
        self.max_length = max_length
        self.embedding_dim = embedding_dim
        pe = torch.zeros((max_length, embedding_dim))
        position = torch.arange(0, max_length, dtype=torch.float).unsqueeze(1)
        div_term = torch.exp(
            torch.arange(0, embedding_dim, 2).float() *
            (-torch.log(torch.tensor(10000.0)) / embedding_dim)
        )
        pe[:, 0::2] = torch.sin(position*div_term)
        pe[:, 1::2] = torch.cos(position*div_term)
        pe = pe.unsqueeze(0)
        self.register_buffer('pe', pe)

    def forward(self, x):
        x = x + self.pe
        return x

class LearnablePosEncoding(nn.Module):
    def __init__(self, max_length:int, embedding_dim:int):
        super().__init__()
        self.max_length = max_length
        self.embedding_dim = embedding_dim
        self.pe = nn.Parameter(torch.zeros(size=(1, self.max_length, self.embedding_dim)))

    def forward(self, x):
        return x + self.pe

class Conv3dPosEmbedding(nn.Module):
    '''
    Positinal Encoding Generator using 3d convolution
    Args:
        dim: the input feature dimension
        dropout: the dropout ratio
        emb_kernel: the kernel size of convolution
            padding_size = emb_kernel // 2
    '''
    def __init__(self, dim, dropout:float, emb_kernel:int=3):
        super(Conv3dPosEmbedding, self).__init__()
        self.proj = nn.Conv3d(in_channels=dim, out_channels=dim, kernel_size=emb_kernel,
                              stride=1, padding=emb_kernel//2, groups=dim)

        self.dropout = nn.Dropout3d(p=dropout)

    def forward(self, x):
        """
        Args:
            Input:
                size, [batch, channels, heights, widths, depths]
            Output:
                size is same with Input
        """
        pos_enc = self.proj(x)
        x = x + pos_enc
        return self.dropout(x)

class Conv2dPosEmbedding(nn.Module):
    '''
    Positinal Encoding Generator using convolution
    Args:
        dim: the input feature dimension
        dropout: the dropout ratio
        emb_kernel: the kernel size of convolution
            padding_size = emb_kernel // 2
    '''
    def __init__(self, dim, dropout:float, emb_kernel:int=3):
        super(Conv2dPosEmbedding, self).__init__()
        self.proj = nn.Conv2d(in_channels=dim, out_channels=dim, kernel_size=emb_kernel,
                              stride=1, padding=emb_kernel//2, groups=dim)

        self.dropout = nn.Dropout2d(p=dropout)

    def forward(self, x):
        """
        Args:
            Input:
                size, [batch, channels, heights, widths, depths]
            Output:
                size is same with Input
        """
        pos_enc = self.proj(x)
        x = x + pos_enc
        return self.dropout(x)


class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        k = []
        for i in range(len(nums)):
            if nums[i] == 0:
                k.append(i)

        for j in k[::-1]:
            nums.pop(j)
            nums.append(0)