import numpy as np
import pandas as pd
pd.set_option('display.max_columns',34)
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go

from sklearn.model_selection import train_test_split
from sklearn import neighbors
from sklearn.neighbors import KNeighborsRegressor, KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.cluster import KMeans
from sklearn.metrics import accuracy_score, mean_absolute_error, mean_squared_error, r2_score, confusion_matrix, ConfusionMatrixDisplay
from sklearn.preprocessing import StandardScaler, MinMaxScaler, OneHotEncoder
from sklearn.ensemble import RandomForestRegressor, AdaBoostRegressor
from sklearn.linear_model import LogisticRegression, LinearRegression, Ridge, Lasso
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline, Pipeline

import streamlit as st
from streamlit_option_menu import option_menu

st.set_option('deprecation.showPyplotGlobalUse', False)
st.set_page_config(layout="wide")

data = pd.read_csv('data.csv')
data.pop('Unnamed: 0')
data = data.rename(columns = {'Type of ownership' : 'Type of Ownership',
                      'min_salary' : 'Min. Salary',
                      'max_salary' : 'Max. Salary',
                      'avg_salary' : 'Avg. Salary',
                      'job_state' : 'Job State',
                      'same_state' : 'Same State',
                      'age' : 'Age',
                      'python_yn' : 'Python Exp.',
                      'R_yn' : 'R Exp.',
                      'spark' : 'Spark Exp.',
                      'aws' : 'AWS Exp.',
                      'excel' : 'Excel Exp.',
                      'job_simp' : 'Title Simplified',
                     'Headquarters' : 'HQ',
                     'hourly' : 'Hourly',
                     'desc_len' : 'Description Length',
                     'num_comp' : '# of Competitors',
                     'employer_provided' : 'Employer Provided',
                     'seniority' : 'Seniority',
                     })
data.pop('R Exp.')
data['Title Simplified'] = data['Title Simplified'].str.title()
data['Title Simplified'] = data['Title Simplified'].str.replace('Mle', 'MLE')
data['Min. Salary'] = data['Min. Salary']*1000
data['Max. Salary'] = data['Max. Salary']*1000
data['Avg. Salary'] = data['Avg. Salary']*1000
data.replace({"Na": np.nan, "NaN": np.nan, "-1": np.nan, "Unknown / Non-Applicable": np.nan}, inplace=True)
data = data.drop(['Competitors', '# of Competitors'], axis=1)
data['Type of Ownership'] = data['Type of Ownership'].replace('Unknown', 'Na')
data_orig = data
columns_to_drop = ['Seniority' , 'Job Title', 'Salary Estimate', 'Job Description', 'Company Name', 'Location', 'HQ', 'Industry', 'Hourly', 'Same State', 'Seniority', 'Employer Provided', 'company_txt']

df = data.drop(columns=columns_to_drop)
df_orig = df

#removing data
df = df.loc[(df['Age'] >= 18) & (df['Age'] <= 120)]
df = df[df['Min. Salary'] > 30000]

###############################################################################################################################################

from PIL import Image
image = Image.open('image2.png')

###############################################################################################################################################
###############################################################################################################################################
with st.sidebar:
    selected = option_menu(
        menu_title="Menu",
        options=['Home', 'EDA', 'Salary Analysis', 'Salary Prediction', 'Conclusion','About Me'],
        icons= ['house-door-fill' , 'rocket-takeoff-fill', 'graph-up', 'layers-fill', 'piggy-bank-fill' ]
    )

###############################################################################################################################################
###############################################################################################################################################
if selected == 'Home':
    st.image(image, width=800)
    st.write("""
    We long gone past the time when we once lived where every action required user input and the view on how math subjects can be applicable outside of what is required while in school.  Thanks to every growing field on Artificial Intelligence (AI) and Machine Learning (ML) and the well renowned [article published in Harvard Business Review](https://hbr.org/2012/10/data-scientist-the-sexiest-job-of-the-21st-century) back in 2012, which called Data Science (DS) the sexiest job of the 21st century. As businesses continue to seek ways effectively access trends (from present data, reviewing trends from past data and accurately predicting future trends), satisfy the need of customers while operating in the most efficient way possible.

    It is widely known that pursing a career in Data Science can be rewarding in terms of income, while that may be the case as the great saying goes 'More money, more problems'. This purpose of this app will explore through the job listings from a Glassdoor back in 2016 and observe certain trends in salaries, location, satisfaction of a position, the abundance of positions and see how they all relate to one another.""")

###############################################################################################################################################
###############################################################################################################################################
if selected == 'EDA':
    st.image(image, width=800)
    st.title('Exploratior Data Analysis')

    col1, col2 = st.columns([4, 2])
    with col1:
        with st.expander('**View Data**'):
            st.write(df)
            with col2:
                with st.expander('**View Data**'):
                    st.subheader('Summary Statistics of Job Postings')
                    st.write(df.describe())

    tab1, tab2, tab3, tab4, tab5 = st.tabs(['**Description**', '**Simple Data Review**', '**Map Visualization**', '**Correlations**', '**Conclusion**'])

    with tab2:
        st.title('Dataset Exploratation')
        col1, col2 = st.columns([4, 1])
        with col1:
            pursue_ds_career = st.radio('**How sure are you that you want to pursue a career in Data Science**', ['ehh maybe...', '**Very Certain❕**'], horizontal=True)
            if pursue_ds_career == 'ehh maybe...':
                st.write(df.head())
                st.write('#')
                st.subheader('Summary statistics of a ***few*** job postings')
                st.write(df.head().describe())
            else:
                st.write(df)
                st.write('#')
                st.subheader('Summary statistics of **all** job postings')
                st.write(df.describe())
        with col2:
            select = st.checkbox('Show Description of Features')
            if select:
                st.write('''
                Founded: Year company was founded Job

                State: The state where the job is located

                Same State: An indicator of whether the job is in the same state as the person looking at the job

                Age: The age of the person looking at the job

                Python Exp.: An Indicator of whether the person looking at the job knows Python

                R Exp.: An indicator of whether the person looking at the job knows R

                Spark Exp.: An indicator of whether the person looking at the job knows Spark

                AWS Exp.: An Indicator of whether the person looking at the job knows AWS

                Excel Exp.: An indicator of whether the person looking at the job knows Excel

                Title Simplified: A simplified job title

                HQ: Location of Headquarters

                Hourly: An indicator of whether the person will be paid hourly

                Description Length: A count of total number of characters in the job posting

                **MLE: Machine Learning Engineer''')

        small_vis = st.radio('**Quick visuals**', ['Distributions', 'Coding Skillset'], horizontal=True)
        if small_vis == 'Distributions':
            st.write('''
            The message mentions that there is a bias in job openings, specifically in the context of Data Scientist positions and the type of ownership field being a private company. This suggests that there might be more job openings for Data Scientists in private companies compared to other roles. It can be seen that there are roughly 66% more Data Science job openings than the next option. This indicates that Data Scientist roles are abundant in the dataset.''')
            col1, col2 = st.columns(2)
            with col1:
                lst = ['Rating', 'Size', 'Founded', 'Age', 'Type of Ownership', 'Sector', 'Revenue', 'Title Simplified', 'Job State']
                #color_sel = st.sidebar.selectbox('Sorting Options', df.columns)
                st.sidebar.title('''Fig. 1A: Distribution of Features''')
                sel = st.sidebar.selectbox('Features', sorted(lst), index=7)

                if sel:
                    color_mapping = {
                        value: color
                        for value, color in zip(df[sel].unique(), px.colors.qualitative.Set1)}
                    df['Color'] = df[sel].map(color_mapping)
                    fig = px.histogram(df, x=sel, color=sel)
                    fig.update_layout(xaxis=dict(showgrid=False), yaxis=dict(showgrid=False, title_text='Count'), title_text=f'Distribution for {sel}')
                    st.plotly_chart(fig, use_container_width=True)
                    st.title('Fig. 1A: Distribution of Features')
                    with col2:
                        fig_pie = px.pie(df, names=sel, title=f'Pie chart for {sel}')
                        fig_pie.update_traces(textposition='inside', textinfo='percent+label')
                        st.plotly_chart(fig_pie)
        else:
            code_exp = df.groupby('Title Simplified')[['Python Exp.', 'Spark Exp.', 'AWS Exp.', 'Excel Exp.']].sum().drop_duplicates()
            st.sidebar.title('''Fig. 1B: Distribution of Coding Skillset''')
            code_sel = st.sidebar.selectbox('How does your coding experience help', code_exp.index)
            selected_data = code_exp.loc[code_sel]
            sorted_data = selected_data.sort_values()
            colors = ['crimson' if x == sorted_data.min() else 'green' if x == sorted_data.max() else 'lightslategray' for x in sorted_data]
            st.write('''
            Now you can observe the distributions which coding languages were idenitifed in the job listing for each of the simplified job titles. The data shows the experience in older languages, such as python and excel, are used more. Also, there are certain positions that require a knowledge in multiple skillsets.
            The column which is green identifies the coding language that was identified most often, column which is red/no color identifies the coding language that was identified most often.
            ''')
            fig = go.Figure(data=[go.Bar(
                x=sorted_data.index,
                y=sorted_data,
                marker_color=colors)])
            fig.update_layout(xaxis=dict(showgrid=False, title_text=f'Coding Experience for {code_sel}'), yaxis=dict(showgrid=False, title_text='Count'), title_text=f'Distribution of Coding Skillset Required for {code_sel} Position')
            st.plotly_chart(fig)
            st.title('''Fig. 1B: Distribution of Coding Skillset''')

###############################################################################################################################################
###############################################################################################################################################
if selected == 'Salary Analysis':
    st.image(image, width=800)
    st.title('Salary Analysis')

    col1, col2 = st.columns([4,2])
    with col1:
        num_feats = data.select_dtypes(include=['number']).columns
        num_feats = num_feats.drop(['Hourly', 'Employer Provided', 'Same State', 'Avg. Salary','Python Exp.', 'Spark Exp.', 'AWS Exp.', 'Excel Exp.'])
        number_of_feats = st.slider('Select the # of Features to Reflect Prediction', 1,3,1)
        df_cols = df.columns.drop(['Python Exp.', 'Spark Exp.', 'AWS Exp.', 'Excel Exp.'])
        available_feats =  num_feats.tolist()
        selected_feats = []

        for i in range(number_of_feats):
            if available_feats:
                feature_name = st.selectbox(f'Select Feature #{i + 1}', options=available_feats, key=i)
                selected_feats.append(feature_name)
                available_feats = [feat for feat in available_feats if feat != feature_name]
            else:
                st.warning('No more features available to select.')
                break
    with col2:
        code_skills = st.multiselect(
        'Select all coding languages you are familiar with:',
        ['Python', 'Spark', 'AWS', 'Excel'],
        placeholder="Choose an option")

    y = df['Avg. Salary']
    X = df[selected_feats]
    test_fraction = 0.2
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_fraction)

    model_options = ["Linear Regression", "Ridge", "Lasso"]
    selected_model = st.selectbox("Select a regression model:", model_options)

    model_dict = {
        "Linear Regression": LinearRegression(),
        "Ridge": Ridge(),
        "Lasso": Lasso()
    }

    model = model_dict[selected_model]
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)

    def evaluate_model (true, predicted):
        mae = mean_absolute_error(true, predicted)
        mse = mean_squared_error(true, predicted)
        rmse = np.sqrt (mean_squared_error (true, predicted))
        r2_square = r2_score (true, predicted)
        return mae, mse, rmse, r2_square
    mae, mse, rmse, r2 = evaluate_model(y_test, y_pred)

    fig = go.Figure()
    fig.add_trace(go.Scatter(x=np.arange(len(y_test)), y=y_test, mode='markers', name='Actual'))
    fig.add_trace(go.Scatter(x=np.arange(len(y_pred)), y=y_pred, mode='markers', name='Prediction'))
    fig.update_layout(title="Actual vs Predicted Salary Values", xaxis_title="Index", yaxis_title="Values")
    fig.update_traces(marker=dict(size=8,  symbol="diamond",
                                line=dict(width=2,
                                            color='DarkSlateGrey')),
                    selector=dict(mode='markers'))
    st.plotly_chart(fig)
    st.metric(label="Mean Absolute Error (MAE)", value=f"{mae:.2f}")
    #st.metric(label="Mean Squared Error (MSE)", value=f"{mse:.2f}")
    st.metric(label="Root Mean Squared Error (RMSE)", value=f"{rmse:.2f}")
    st.metric(label="R-Squared (R2)", value=f"{r2:.2f}")

###############################################################################################################################################
###############################################################################################################################################
if selected == 'Salary Prediction':
    st.title('')
    st.header("Let's Predict Your Salary")

    #jt = data['Title Simplified'].tolsit()
    st.subheader('What is simplified job tilie you are seeking?')

    job_title = st.selectbox(
        "What job title best reflects your daily work?",
        ['Data Scientist', 'Analyst', 'Data Engineer', 'Director',
        'Manager', 'Machine Learning Engineer'])

    comp_size = st.selectbox(
        "How many people are you hoping to have within your new company?",
        ['501 to 1000 employees', '10000+ employees',
        '1001 to 5000 employees', '201 to 500 employees',
        '5001 to 10000 employees', '51 to 200 employees',
        '1 to 50 employees'])

    top = st.selectbox(
        "What type of ownership are you interested in?",
    ['Company - Private', 'Other Organization', 'Government',
        'Company - Public', 'Nonprofit Organization',
        'Subsidiary or Business Segment'])

    pick_sector = st.selectbox(
        "Which sector are you interested in being apart of?",
    ['Aerospace & Defense', 'Health Care',
        'Oil, Gas, Energy & Utilities', 'Real Estate', 'Business Services',
        'Retail', 'Insurance', 'Transportation & Logistics', 'Finance',
        'Biotech & Pharmaceuticals', 'Telecommunications',
        'Information Technology', 'Manufacturing', 'Government',
        'Agriculture & Forestry', 'Education',
        'Arts, Entertainment & Recreation', 'Travel & Tourism', 'Media',
        'Non-Profit'])

    work_loc   = st.selectbox(
        "Ideally what state would you work in?",
    ['NM', 'MD', 'WA', 'TX', 'VA', 'CO', 'KY', 'OR', 'MI', 'MA', 'CA',
        'NY', 'IL', 'AL', 'PA', 'GA', 'WI', 'NC', 'IN', 'MN', 'DC', 'OH',
        'TN', 'NJ', 'MO', 'ID', 'IA', 'FL', 'KS', 'UT', 'AZ'])

    predictors = ['Title Simplified', 'Size', 'Type of Ownership', 'Sector', 'Job State']
    target = 'Avg. Salary'
    y = df[target]
    X = df[predictors]
    test_fraction = 0.2
    X_train, X_test, y_train, y_test = train_test_split(df[predictors], df[target], test_size=0.2, random_state=42)

    categorical_features = ['Title Simplified', 'Size', 'Type of Ownership', 'Sector', 'Job State']
    categorical_transformer = OneHotEncoder(handle_unknown='ignore')

    preprocessor = ColumnTransformer(
        transformers=[
            ('cat', categorical_transformer, categorical_features)
        ])

    model = Pipeline(steps=[('preprocessor', preprocessor),
                            ('regressor', LinearRegression())])

    model.fit(X_train, y_train)

    y_pred = model.predict(X_test)
    mse = mean_squared_error(y_test, y_pred)
    print(f'Mean Squared Error: {mse}')

    def predict_salary(job_title, comp_size, ownership_type, sector, job_state):
        input_data = pd.DataFrame({
            'Title Simplified': [job_title],
            'Size': [comp_size],
            'Type of Ownership': [ownership_type],
            'Sector': [sector],
            'Job State': [job_state]})
        predicted_salary = model.predict(input_data)
        return predicted_salary[0]

    image2 = Image.open('IMG_0370.png')
    if st.button("Predict Salary"):
        predicted_salary = predict_salary(job_title, comp_size, top, pick_sector, work_loc)
        st.write(f"Predicted Salary: ${predicted_salary:,.2f}")
        st.image(image2, width=100)

###############################################################################################################################################
###############################################################################################################################################
if selected == 'Conclusion':
    st.title('Conclusion')

###############################################################################################################################################
###############################################################################################################################################
if selected == 'About Me':
    st.title('Sharod Williams')

    col1, col2 = st.columns([3,2])

    with col1:
        image = Image.open('image2.png')
        st.image(image, width=600)
        st.title('Conclusion')
        st.write('''Throughout the exploration of this dataset certain factors have been analyzed to support your journey as you try to find the data science position that is right for you. It was also shown that the more money you make in a position equaites to having a higher level of happiness. This process included reviewal of underlying commonalities in the listings, an evaluation of different visualizations was made to help make this easier, including the comparing and contrasting of the relationship agamous Salary, Employee Satisfaction and their Location. I plan to further investigate this dataset by conducting testing and training of the data, this will allow for better accurate imputation of the missing data.''')
    with col2:
        image2 = Image.open('IMG_0370.png')
        st.image(image2, width=300)
        st.write('Committed to using computational techniques to solve community problems, furthering my professional development through enrolling in courses and completing projects, while working independently and collaboratively as a data scientist/bioinformatician.')

    #

    #st.set_page_config(page_title = 'Analysis of Data Scientist Openings', page_icon = '💰', layout='wide')


'''
Replace all the special characters(!, @, #, $, %, ^, &, *) in a given string with a space.
example : ab!@#cd is the input, the output is ab   cd
Output has three spaces, which are to be replaced with these special characters
'''
def main():
    s = input()
    a = "!@#$%^&*"
    #a_list = list(a)
    s_new = ""
    for letter in s:
        if letter not in a:
            print(letter)
            s_new += letter
        else:
            s_new += " "

    print(s_new)
main()

"""
def main():
    '''
    Read string from the input, store it in variable str_input.
    '''
    s = str(input())
    z = ""
    for n in s:
        if n == "!" or n == "@" or n == "#" or n == "$" or n == "%" or n == "^" or n == "&" or n == "*":
             z+=" "
        else:
            z+=n
    print(z)
main()
"""


import numpy as np
import pandas as pd
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthieu', 'Laura', 'Kevin', 'Jonas'],

'note' : [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],

'tentatives' : [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],

'qualifier' : ['oui', 'non', 'oui', 'non', 'non', 'oui', 'oui', 'non', 'non', 'oui']}

df= pd.DataFrame(data=exam_data , index= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
print(df)

df=df.dropna()
print(df)
print(df.loc[:,['name','note']])

df=df._append({'name': "Suresh", 'note': 15.5, 'tentatives': 1, 'qualifier': "yes"},ignore_index= True)
print(df)
print(df.drop(columns='tentatives'))

df['Success'] = df['note'].apply(lambda x: 1 if x > 10 else 0)
print(df)
df.to_csv('my_data.csv')


# IMPORTS --------------------------------------------------------------

# Standard imports
import pdb
import logging

import sys
from datetime import datetime as dt

# Third party imports
pass

# local application imports
from ccat import config
from ccat.model.database.bucket import Bucket
from ccat.model.exchange.order import Order
from ccat.controller.strategy.reversal import Strategy

# SETTINGS -------------------------------------------------------------

# Get arguments from the cronjob/terminal.
# Default to default settings if sys.argv does not have any arguments
try:
    '''
    Cronjob:
    0 0 * * * python3 bot.py 1 6 500 10

    Run bot.py with the arguments below every day at midnight:
    sys.argv[1] = 1     > market_id = 1
    sys.argv[2] = 6     > timeframe_id = 6
    sys.argv[3] = 500   > count = 500
    sys.argv[4] = 500   > stake_size = 500
    '''
    market_id = int(sys.argv[1])
    timeframe_id = int(sys.argv[2])
    count = int(sys.argv[3])
    stake_size = int(sys.argv[4])

# If no ill-formed arguments are passed, a test order is created
# TODO: Remove this hack and pass args to sys.argv instead
except:
    market_id = 1
    timeframe_id = 6
    count = 500
    stake_size = 1

# Strategy settings
kwargs = {
    'len_ma_top': 24, #24
    'len_ma_bottom': 24,
    'prefix':'reversal'}

# EXECUTE STRATEGY -----------------------------------------------------

# Get a bucket instance with the right market and timeframe id's
bucket = Bucket(market_id = market_id, timeframe_id = timeframe_id)

# Update table with the candles for the timeframe
bucket.update(count=count)

# Get the dataframe with the bucket data for the latest count candles
df_bucket = bucket.read_latest(count=count)

# Create strategy instance using the strategy settings
strategy = Strategy(df_bucket = df_bucket, **kwargs)

# Get signal
signal = strategy.signal()

# LOGGING --------------------------------------------------------------

logging.basicConfig(
    filename=config.lf_bot,
    level=logging.INFO)

def do_log(order):
    info = (
        '{:3} | '
        'utc: {:<20} | '
        'signal: {:2} | '
        'stake: {:6} | '
        'status: {:<9} | '
        'timeframe_id: {:1} | '
        'market_id: {:3} | ').format(
            '',
            dt.strftime(dt.utcnow(), "%Y.%m.%d %H:%M:%S"),
            signal,
            stake_size,
            order.order_status,
            timeframe_id,
            market_id)
    logging.info(info)

# EXECUTE ORDER --------------------------------------------------------

def execute(signal):

    if signal != 0:
        order = Order(market_id = market_id)

        if signal == 1:
            order = order.submit(side = 'buy', amount = stake_size)
            do_log(order)
            return order

        elif signal == -1:
            order = order.submit(side = 'sell', amount = stake_size)
            do_log(order)
            return order

    else:
        order = type('Order', (object,), {})()
        order.order_status = "-"
        do_log(order)
        return order

# MAIN -----------------------------------------------------------------

if __name__ == '__main__':

    execute(signal)


from bs4 import BeautifulSoup
import requests
import re

CAC40_URL = "https://companiesmarketcap.com/cac-40/largest-companies-by-market-cap/"
AMERICAN_LARGEST_COMPANIES = (
    "https://companiesmarketcap.com/usa/largest-companies-in-the-usa-by-market-cap/"
)

def get_cac40_symbols() -> list[str]:
    soup = BeautifulSoup(requests.get(CAC40_URL).content, "lxml")
    raw_codes = soup.find_all("div", class_="company-code")
    return list(map(lambda input: str(re.sub("<[^>]*>", "", str(input))), raw_codes))

def get_largest_symbols_by_country_normal(n: int = 500) -> list[str]:
    n_per_page = 100
    assert (
        n >= n_per_page and n <= 3571
    ), "Error, provide a valid number of symbol, <= 3571 and >= 100."

    raw_codes = []
    pages = (n // n_per_page) + 1

    for i in range(pages):
        soup = BeautifulSoup(
            requests.get(f"{AMERICAN_LARGEST_COMPANIES}?page={i+1}").content, "lxml"
        )
        raw_codes.extend(soup.find_all("div", class_="company-code"))
    return list(map(lambda input: str(re.sub("<[^>]*>", "", str(input))), raw_codes))[
        :n
    ]


import requests
import json, os, re, time
import urllib.request

url = "https://graphql.anilist.co/"

# To get access token please visit: https://anilist.co/api/v2/oauth/authorize?client_id=2861&response_type=token
accessToken = json.loads(open(os.path.join('', 'api.json')).read())
if accessToken in "REPLACE WITH ACCESS TOKEN":
    print("Please add your access token.")
else:
    #user specified page number
    startpage = int(input('Enter start page number -->'))
    stoppage = int(input('Enter page number to stop at -->'))
    print('         ------STARTING------')
    while startpage < stoppage:

        no = startpage

        #POST request data
        payload = "{\"query\":\"query ($p: Int) {\\n  Page(page: $p) {\\n    users(sort: ID_DESC) {\\n      id\\n      avatar{\\n        large\\n      }\\n      \\n    }\\n  }\\n}\",\"variables\":{\"p\":" + str(no) + "}}"
        headers = {
            'content-type': "application/json",
                'authorization': "Bearer " + accessToken
         }

        #POST request parameters.
        response = requests.request("POST", url, data=payload, headers=headers)

        #Output - query data
        qdata = response.text
        #remove backslashes.
        qdata = qdata.replace('\\','')
        #remove square brackets.
        qdata = qdata.replace('[', '')
        qdata = qdata.replace(']', '')
        #Remove default image profiles.
        qdata = qdata.replace('"https://s4.anilist.co/file/anilistcdn/user/avatar/large/default.png"', 'null')
        #remove "large":"
        qdata = qdata.replace('"large":"','')
        #remove quotation marks
        qdata = qdata.replace('"','')

        #DO NOT CASEFOLD. IMAGE URL IS CASE SENSITIVE
        #Turn qdata into an array at each },{
        rdata = qdata.split('},{')

        #print(rdata)

        #loop 50 times with +1 each cycle, because max entries per page is 50.
        nq = 1
        while nq < 49:
            #set image as rdata
            image = rdata[nq]
            idnum = rdata[nq].split(',')
            match = re.search("{(.+?)}", image,flags=re.IGNORECASE)
            result = match.group(1)

            if result == 'large:null':
                nq = nq+1
            else:
                idnum = idnum[0].replace('id:','')
                page = urllib.request.urlopen("http://localhost:5555/api/classify?url=" + result).read().decode('utf-8')
                jsonify = json.loads(page)
                get_predictions = jsonify.get('predictions',{})
                pred1 = get_predictions[0]
                pred2 = get_predictions[1]
                nsfw = ["hentai", "sexy", "porn"]
                possible_class1 = pred1.get('class',{})
                possible_class2 = pred2.get('class',{})
                predict1 = pred1.get('prob',{}) * 100
                predict2 = pred2.get('prob',{}) * 100

                if possible_class1 in nsfw:
                    print('https://anilist.co/user/' + idnum)
                    print("1st guess: " + possible_class1.upper() + ": " + str(predict1) + "%")
                else:
                    pass

                if possible_class2 in 'hentai':
                    if int(predict2) > 10:
                        print('https://anilist.co/user/' + idnum)
                        print("2nd guess: " + possible_class2.upper() + ": " + str(predict2) + "%")
                    else:
                        pass
                else:
                    pass
                nq = nq+1
        time.sleep(1)
        startpage = startpage+1
        if startpage == stoppage:
            print('         ------FINISHED------')
        else:
            print('         Page: ' + str(startpage))


import os
import subprocess

def get_db_ip():
    result = subprocess.run(
        [
            "docker",
            "ps",
            "-f",
            "name=%s" % get_guessed_container_name(),
            "-q",
        ],
        stdout=subprocess.PIPE,
    )
    ids = result.stdout.decode('utf-8').strip().split('\n')
    if len(ids) > 1:
        raise Exception("Can't find the DB, too much match")
    if len(ids) == 0 or len(ids[0]) == 0:
        result = subprocess.run(
            [
                "docker",
                "ps",
                "-f",
                "name=_db",
                "-q",
            ],
            stdout=subprocess.PIPE,
        )
        ids = result.stdout.decode('utf-8').strip().split('\n')
        if len(ids) > 1:
            raise Exception(
                "Can't find the DB, couldn't guess container name (tried '%s'), "
                "and too much match with '_db'" % get_guessed_container_name()
            )
        if len(ids) == 0 or len(ids[0]) == 0:
            raise Exception("Can't find the DB")
    result = subprocess.run(
        [
            "docker",
            "inspect",
            "-f",
            "'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'",
            ids[0],
        ],
        stdout=subprocess.PIPE,
    )
    return result.stdout.decode('utf-8').strip().replace("'", "")

def get_guessed_container_name():
    return str(os.path.dirname(__file__).split(os.path.sep)[-2]).lower().replace('-', '') + "_db"

if __name__ == "__main__":
    print(get_db_ip())


def freqQuery(queries):
    collection = {}
    freq_dict = {}
    output = []
    for i in queries:
        arr = i
        if arr[0] == 1:
            if arr[1] not in collection:
                collection[arr[1]] = 1
            else:
                collection[arr[1]] += 1
        elif arr[0] == 2:
            if collection.get(arr[1]):
                collection[arr[1]] -= 1
        elif arr[0] == 3:
            if arr[1] in collection.values():
                output.append(1)
            else:
                output.append(0)
        else:
            continue

    return output

q = [[1,1],[2,2],[3,2],[1,1],[1,1],[2,1],[3,2]]
# q = [[1,3],[2,3],[3,2],[1,4],[1,5],[1,5],[1,4],[3,2],[2,4],[3,2]]
print(freqQuery(q))


import os
import re
import xml.dom.minidom
import xml.etree.ElementTree as ET
import logging
import posixpath

class JenkinsParameters:
    def __init__(self, config_path):
        if not config_path.endswith('xml'):
            pass
        else:
            self.config_path = config_path
            self.root = ET.parse(config_path)
            self.params = self.root.findall("properties/hudson.model.ParametersDefinitionProperty/parameterDefinitions")

    def add_param(self, parameter):
        for child in self.root.iter('parameterDefinitions'):
            child.append(parameter)

    @staticmethod
    def add_param_to_tree(root, parameter):
        for child in root.iter('parameterDefinitions'):
            child.append(parameter)

    @staticmethod
    def create_parameter(param_type, name, description, param_value):
        """Create new Jenkins parameter

        :param param_type: Type of parameter (String, Boolean, Choice)
        :param name: Name of parameter
        :param description: Parameter description
        :param param_value: For String and Boolean parameter it's defaultValue, for choice parameter it's a list of choices.
        """

        def get_parameter_definition(param):
            return 'hudson.model.' + param + 'ParameterDefinition'

        def create_choice_parameter():
            choices = ET.Element('choices', {'class': 'java.util.Arrays$ArrayList'})
            a = ET.Element('a', {'class': 'string-array'})
            for val in param_value:
                element = ET.Element('string')
                element.text = val
                a.append(element)
            choices.append(a)
            parent_element.append(choices)

        parent_element = ET.Element(get_parameter_definition(param_type))
        name_el = ET.Element('name')
        name_el.text = name
        desc = ET.Element('description')
        desc.text = description
        parent_element.append(name_el)
        parent_element.append(desc)
        if param_type == 'Choice':
            create_choice_parameter()
        else:
            default_val = ET.Element('defaultValue')
            default_val.text = param_value
            parent_element.append(default_val)
        return parent_element

    def set_param_default_value(self, name, new_default_value):
        """Changes parameter default value

        :param name: Parameter name in Jenkins
        :param new_default_value: Default value of Jenkins Parameter. Can be empty string.

        """
        for child in self.root.iter('parameterDefinitions'):
            for child2 in child:
                found_match = False
                for child3 in child2:
                    try:
                        if child3.tag == 'name' and (child3.text == name or child3.attrib['name'] == name):
                            found_match = True
                    except KeyError:
                        continue
                if found_match:
                    for child3 in child2:
                        if child3.tag == 'defaultValue':
                            child3.text = new_default_value

    @staticmethod
    def is_maven_set_property_line(line):
        return not re.match('^mvn.*\\\\$', line) and re.match('^\s*["]?\w*-D.*\w*$', line)

    def add_mvn_property_to_bash_script(self, mvn_property, prop_value):
        """Adds maven property to bash script

        :param mvn_property: str property -Dsome.maven.property=${example}
        :param prop_value: str property value"""

        if self.is_mvn_property_in_bash_script(prop_value):
            logging.debug('{} is already defined in {} config file. Mvn property not added.'
                          .format(prop_value, self.config_path))
            return

        for shell in self.root.iter('hudson.tasks.Shell'):
            for command in shell:
                new_cmd = ''
                for line in command.text.splitlines():
                    if self.is_maven_set_property_line(line) \
                            and not re.match('.*\\\\$', line):
                        new_cmd += line + ' \\\n'
                        new_cmd += mvn_property + '\n'
                    else:
                        new_cmd += line + '\n'
                print('\nShell script after changes \n-------------------\n' + new_cmd)
                command.text = new_cmd

    def read_mvn_property_from_bash_script(self, parameter_name):
        """Traverse jenkins job shell script lines, looking for line
        that contains mvn property assignment to bash variable with 'parameter name'.
        Please note that this method will work only if bash shell contains command:
        'mvn compile \
           -Dsome.property=${EXAMPLE} \
           -Dother.property=${EXAMPLE2}'

        :param parameter_name: Name of jenkins parameter used to assign mvn property
        """

        def remove_escape_line(mvn_property):
            if re.match('.*\\\\$', mvn_property):
                return mvn_property.replace('\\', '')
            return mvn_property

        for shell in self.root.iter('hudson.tasks.Shell'):
            for command in shell:
                for line in command.text.splitlines():
                    if self.is_maven_set_property_line(line) and re.match('.*=\\$\\{' + parameter_name + '\\}.*', line):
                        return remove_escape_line(line)
        raise MavenPropertyNotFoundException("Could not find maven property for {} parameter".format(parameter_name))

    def is_mvn_property_in_bash_script(self, parameter_name):
        for shell in self.root.iter('hudson.tasks.Shell'):
            for command in shell:
                for line in command.text.splitlines():
                    if self.is_maven_set_property_line(line) and re.match('.*=\\$\\{' + parameter_name + '\\}.*', line):
                        return True
        return False

    def write(self, target_path='.', override_config=False):
        if override_config:
            target_path = self.config_path

        self.root.write(target_path)
        content = xml.dom.minidom.parse(open(target_path, 'r')).toprettyxml()
        file = open(target_path, 'w')
        content_no_whitespace = ''
        for elem in filter(lambda x: not re.match(r'^\s*$', x), content.splitlines()):
            content_no_whitespace += elem + '\n'
        file.write(content_no_whitespace)
        file.close()

    @staticmethod
    def write_xml_tree(root, target_path):
        root.write(target_path)
        content = xml.dom.minidom.parse(open(target_path, 'r')).toprettyxml()
        file = open(target_path, 'w')
        content_no_whitespace = ''
        for elem in filter(lambda x: not re.match(r'^\s*$', x), content.splitlines()):
            content_no_whitespace += elem + '\n'
        file.write(content_no_whitespace)
        file.close()

    def read_all_configs(self, root_dir):
        """Returns dictionary of all jenkins jobs in *search_path*

        :param root_dir: Absolute path to the top of job search

        Key is directory name and value is absolute path to this directory.
        """
        tuples = []
        result = {}

        if root_dir is None or root_dir == '':
            return tuples
        else:
            print(root_dir)

        for dirPath, subdirList, fileList in os.walk(root_dir):
            for subdir in subdirList:
                if subdir != 'jobs' and subdir != 'builds' and subdir != 'workflow' and not re.match('\d+', subdir) \
                        and not subdir.startswith(".") and not subdir.endswith('Build'):
                    for file in os.listdir(os.path.join(dirPath, subdir)):
                        if file == 'config.xml':
                            tuples.append((subdir, os.path.join(os.path.join(dirPath, subdir), file)))
        for tup in tuples:
            result[tup[0]] = tup[1]
        return result

    def add_params(self, config_paths, params, mvn_property):
        """
        Modifies config.xml files at given paths list

        :param config_paths:  dictionary of paths to Jenkins job config file (config.xml)
        :param params: List of Parameters to add to each job config
        :param mvn_property: List of mvn_property tuples (key, value) example ('-dsome.dd, 'value')
        """
        for config_path in config_paths.values():
            jenkins_param = JenkinsParameters(config_path)
            for name, param in params.items():
                jenkins_param.add_param(param)
                if mvn_property.__len__() > 0:
                    for prop in mvn_property:
                        jenkins_param.add_mvn_property_to_bash_script('\t' + prop[0] + '=' + prop[1] + '\n', prop[1])
            jenkins_param.write(override_config=True)

    def read_config_file(self, path):
        return open(path).read()

    def copy_configs(self, config_paths, target_dir='./jobs_copy', deep_copy=True):
        """Creates deep copy of given configs paths. Directory structure is
        recreated on local system.

        Returns Dictionary {job_name: pathToConfig}

        :param config_paths: List of all config paths to copy
        :param target_dir: Root directory where structure copy will be created
               system open. It will be used to read local or remote file content
        :param deep_copy: If true complete directory structure will be recreated,
                          otherwise only job name directory will be created
        """
        copy_paths = {}

        def remove_root_path(path):
            """Remove root directory from given path. Returns path (windows/linux)
             depending on the system running app.

            :param path: Linux format path
            """

            def create_path(root_dir):
                result_path = ''
                root_dir_reached = False
                for directory in path.split(posixpath.sep):
                    if directory == root_dir:
                        root_dir_reached = True
                    if root_dir_reached:
                        result_path += directory + os.path.sep
                return result_path, root_dir_reached

            result, root_reached = create_path('.jenkins' if path.__contains__('.jenkins') else 'jenkins')
            if not root_reached:
                return create_path('jobs')[0]
            return result

        for config_path in config_paths:
            dir_path, config_file = os.path.split(config_path)
            if not deep_copy:
                dir_path = os.path.split(dir_path)[1]
            else:
                dir_path = remove_root_path(dir_path)
            copy_dir_path = os.path.join(target_dir, dir_path)
            copy_file_path = os.path.join(copy_dir_path, config_file)
            if not os.path.exists(copy_dir_path):
                # creates directory tree
                os.makedirs(copy_dir_path)
            src_file = self.read_config_file(config_path)
            target_file = open(copy_file_path, 'w', encoding='utf-8')
            target_file.write(src_file)
            copy_paths[os.path.split(dir_path)[1]] = copy_file_path
        return copy_paths

    def read_parameter_by_name(self, name):
        result = {}
        for child in self.root.iter('parameterDefinitions'):
            for child2 in child:
                found_match = False
                for child3 in child2:
                    try:
                        if child3.tag == 'name' and (child3.text == name or child3.attrib['name'] == name):
                            found_match = True
                    except KeyError:
                        continue
                if found_match:
                    for child3 in child2:
                        if child.tag != name:
                            result[child3.tag] = child3.text
        return result

    def read_job_all_parameters(self, config_path):
        """Reads job all parameters and returns tuple containing
        two dictionaries, where key is parameter name.

        First element is
        a dictionary containing all fields of parameter.

        Second element is dictionary of XML elements, to
        be used to add them in different jobs"""
        result = {}
        xml_elements = {}
        root = ET.parse(config_path)
        for child in root.iter('parameterDefinitions'):
            for child2 in child:
                temp = {}
                for child3 in child2:
                    try:
                        temp[child3.tag] = child3.text
                    except KeyError:
                        continue
                result[temp['name']] = temp
                xml_elements[temp['name']] = child2
        return result, xml_elements

    def import_job_parameters(self, new_parameters, config_paths, with_mvn_params=False, src_config_path=''):
        """Adds parameters to parameterDefinitions xml tree.
        If parameter already exists, override existing. Overrides existing
        config.xml files with updated versions.

        :param src_config_path: path to config.xml file of src job
        :param config_paths: List of config.xml file locations
        :param new_parameters: tuple, where second element is
                a dictionary of xml elements (representing jenkins parameters)
        :param with_mvn_params: If true mvn parameters from bash script will be copied if don't
                don't exists in target job
        """

        def get_child_name(child_element):
            for child_2 in child_element:
                if child_2.tag == 'name':
                    return child_2.text

        for config_path in config_paths:
            root = ET.parse(config_path)
            for child in root.iter('parameterDefinitions'):
                # Remove already existing parameters
                elements_to_remove = []
                for child2 in child:
                    try:
                        if new_parameters.__contains__(get_child_name(child2)):
                            elements_to_remove.append(child2)
                    except KeyError:
                        pass
                for param in elements_to_remove:
                    child.remove(param)

            for new_parameter in new_parameters.values():
                JenkinsParameters.add_param_to_tree(root, new_parameter)

            # Adds mvn parameter job bash script
            if with_mvn_params:
                params = JenkinsParameters(config_path)
                params.root = root
                mvn_parameters_to_add = []
                for new_parameter_name in new_parameters.keys():
                    mvn_param = JenkinsParameters(src_config_path). \
                        read_mvn_property_from_bash_script(new_parameter_name)
                    mvn_parameters_to_add.append((new_parameter_name, mvn_param))
                for new_parameter in mvn_parameters_to_add:
                    params.add_mvn_property_to_bash_script(new_parameter[1], new_parameter[0])
            # Save changes
            JenkinsParameters.write_xml_tree(root, config_path)

def read_job_config(config_path):
    tree = ET.parse(config_path)
    params = tree.findall("properties/hudson.model.ParametersDefinitionProperty/parameterDefinitions")

    for param in params:
        for child in param:
            if child.tag == 'hudson.model.ChoiceParameterDefinition':
                strings = child.findall('choices/a/string')
                for string in strings:
                    print(string.text)
            else:
                print(child.find('name').text)
                print(child.find('defaultValue').text)

class MavenPropertyNotFoundException(Exception):
    def __init__(self, message):
        self.message = message


from datetime import datetime

from django.http import HttpResponse
from django.shortcuts import render, redirect

COMPANY_MAIL = "the_boss@company.com"

# The simplest possible view
def show_contact_info(request):
    contact_info = "Sofia, bul. Lomsko Shose, Ivan Ivanov"
    return HttpResponse(contact_info)

class ShirtSizes:
    @staticmethod
    def get_available_shirt_sizes():
        return ["XS", "S", "M", "L", "XL", "XXL"]
        # return ["XS", ]

# View with context
def show_about(request):
    context = {
        "introduction": "We are the best online shop for shirts!\n Men and women.\nTake a look! ",
        "email_address": "ivan.ivanov@company.com",
        "name": {
            "first": "Ivan",
            "last": "Ivanov",
        },
        "shirt_sizes": ShirtSizes(),
    }
    return render(request, "about.html", context=context)

def index(request):
    context = {
        "current_time": datetime.now(),
        # "user": "Gosho",
        "user": "",
    }
    return render(request=request, template_name="index.html", context=context)

def redirect_to_home(request):
    return redirect(to="index")

class Article:
    starting_id = 0

    def __init__(self, headline, tease, full_text, local_path):
        self.headline = headline
        self.tease = tease
        self.full_text = full_text
        self.local_path = local_path
        self.article_id = Article.starting_id
        Article.starting_id += 1

    def get_absolute_url(self):
        return "https://www.shirt_company.com" + self.local_path

articles = [
    Article(
        headline="How to find the size that matches you?",
        tease="It has never been easier to find a perfectly matching shirt. Find how in this article.",
        full_text="{full text of the article}",
        local_path="article/1/",
    ),
    Article(
        headline="The trendiest colors of this summer",
        tease="Can you guess the winner out of purple, blue or orange?",
        full_text="{full text of the article}",
        local_path="article/2/",
    ),
]

def show_articles(request):
    context = {
        "articles": articles,
    }
    return render(request, "articles.html", context)

def show_single_article_by_id(request, article_id):
    article = [a for a in articles if a.article_id == article_id][0]
    context = {
        "article": article,
    }
    return render(request, "article.html", context)

def checkout_basket(request):
    context = {
        "basket": ["milk", "coffee", "bread", ],
        # "basket": [],
        "bill": 134.1214401239,
    }

    return render(request, "checkout.html", context)


#!/usr/bin/env python3

import socket

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = socket.gethostname()
port = 444

server_socket.bind((host, port))
server_socket.listen(3)

while True:
    client_socket, addr = server_socket.accept()
    print(f"received connection from {addr}")

    message = "Hello! Thank you connecting the server!\n"
    client_socket.send(message.encode('utf-8'))

    client_socket.close()


import tensorflow as tf
from typing import List, Optional
from tensorflow.keras.layers import Layer
from .DownConvolution import DownConvolution
from ...mesh.Mesh import Mesh
from ..feature import features_valid

class Encoder(Layer):
    """This combines several DownConvolution layers."""

    down_convolutions: List[DownConvolution]

    def __init__(
        self,
        out_channels: List[int],
        convolutions_per_sequence: int,
        num_sequences_per_down_convolution: int,
        leaky_relu_alpha: float,
        pool_targets: List[Optional[int]],
    ):
        Layer.__init__(self)

        assert len(out_channels) == len(pool_targets)
        for i in range(1, len(pool_targets)):
            assert pool_targets[i] is None or pool_targets[i] < pool_targets[i - 1]

        self.down_convolutions = [
            DownConvolution(
                out_channels[i],
                convolutions_per_sequence,
                num_sequences_per_down_convolution,
                leaky_relu_alpha,
                pool_targets[i],
            )
            for i in range(len(pool_targets))
        ]

    def call(self, mesh: Mesh, features: tf.Tensor) -> tf.Tensor:
        assert features_valid(mesh, features)

        snapshots = []
        for down_convolution in self.down_convolutions:
            features, snapshot = down_convolution(mesh, features)
            snapshots.append(snapshot)

        return features, snapshots


#!/usr/bin/python3

#flask stuff
from flask import Blueprint, render_template

#return output in json
import json

#custom-form class
from forms import RemoteJobsForm

#custom config definitions
from config import Config

import localenv

from localutils import getUsername

from jobs_scraper import show_job_list, get_job_list, search_job_list

#ui-form route
remotejobsui = Blueprint('remotejobsui', __name__)

#variables
httpRCok=200
httpRCbad=400
httpRCsvr=500

devopsContact=localenv.CONTACT

try:
  buyMeCoffee = str(localenv.BUY_ME_COFFEE_URL).split("/")[-1]
except:
  buyMeCoffee=None

@remotejobsui.route('/', methods=['GET', 'POST'])
def uiform():
    username = getUsername()

    form = RemoteJobsForm()
    formOutput = {}

    job_details = localenv.JOB_DETAILS
    if form.validate_on_submit():
        keyword_sub = form.searchkeyword.data
        #formOutput = get_job_list(keyword_sub, job_details) #redo job-scraping
        formOutput = search_job_list(keyword_sub) #search from sqlite-db

        return render_template('remotejobs.html', title='Remote Jobs', user=username, form=form, output=formOutput, appcontact=devopsContact, buyMeCoffee=buyMeCoffee)
    else:
        #formOutput = get_job_list(None, job_details)
        formOutput = show_job_list() #search from sqlite-db

        return render_template('remotejobs.html', title='Remote Jobs', user=username, form=form, output=formOutput, appcontact=devopsContact, buyMeCoffee=buyMeCoffee)

    return render_template('remotejobs.html', title='Remote Jobs', user=username, form=form, output=formOutput, appcontact=devopsContact, buyMeCoffee=buyMeCoffee)


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

# In[53]:

import pandas as pd
import numpy as np

# In[54]:

data = pd.read_excel('C:\\Users\\Gaddam Srinivas\\Load Data in kW.xlsx')

# In[55]:

data.isnull().sum()

# In[56]:

data.head()

# In[57]:

X=data[data.columns[:3]]
X.head()

# In[58]:

y=data[['Load (kW)']]
y.head()

# In[75]:

from sklearn.model_selection import train_test_split
#train test split
X_train, X_test, y_train,y_test = train_test_split(x, y, test_size=0.2,random_state=121)

# # ANN implementation using Adagrad Optimizer

# In[76]:

def define_structure(X, Y):
    input_unit = X.shape[0] # size of input layer
    hidden_unit = 4 #hidden layer of size 4
    output_unit = Y.shape[0] # size of output layer
    return (input_unit, hidden_unit, output_unit)
(input_unit, hidden_unit, output_unit) = define_structure(X_train, y_train)
print("The size of the input layer is:  = " + str(input_unit))
print("The size of the hidden layer is:  = " + str(hidden_unit))
print("The size of the output layer is:  = " + str(output_unit))

# In[77]:

def parameters_initialization(input_unit, hidden_unit, output_unit):
    np.random.seed(2)
    W1 = np.random.randn(hidden_unit, input_unit)*0.01
    b1 = np.zeros((hidden_unit, 1))
    W2 = np.random.randn(output_unit, hidden_unit)*0.01
    b2 = np.zeros((output_unit, 1))
    parameters = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}

    return parameters

# In[78]:

def sigmoid(z):
    return 1/(1+np.exp(-z))

# In[79]:

def forward_propagation(X, parameters):
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']

    Z1 = np.dot(W1, X) + b1
    A1 = np.tanh(Z1)
    Z2 = np.dot(W2, A1) + b2
    A2 = sigmoid(Z2)
    cache = {"Z1": Z1,"A1": A1,"Z2": Z2,"A2": A2}

    return A2, cache

# In[80]:

def cross_entropy_cost(A2, Y, parameters):
    # number of training example
    m = Y.shape[1]
    # Compute the cross-entropy cost
    logprobs = np.multiply(np.log(A2), Y) + np.multiply((1-Y), np.log(1 - A2))
    cost = - np.sum(logprobs) / m
    cost = float(np.squeeze(cost))

    return cost

# In[81]:

def backward_propagation(parameters, cache, X, Y):
    #number of training example
    m = X.shape[1]

    W1 = parameters['W1']
    W2 = parameters['W2']
    A1 = cache['A1']
    A2 = cache['A2']

    dZ2 = A2-Y
    dW2 = (1/m) * np.dot(dZ2, A1.T)
    db2 = (1/m) * np.sum(dZ2, axis=1, keepdims=True)
    dZ1 = np.multiply(np.dot(W2.T, dZ2), 1 - np.power(A1, 2))
    dW1 = (1/m) * np.dot(dZ1, X.T)
    db1 = (1/m)*np.sum(dZ1, axis=1, keepdims=True)

    grads = {"dW1": dW1, "db1": db1, "dW2": dW2,"db2": db2}

    return grads

# In[89]:

def do_adagrad(parameters,grads,learning_rate = 0.01):
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']

    dW1 = grads['dW1']
    db1 = grads['db1']
    dW2 = grads['dW2']
    db2 = grads['db2']
    v_w1, v_b1, v_w2, v_b2, eps = 0, 0, 0, 0, 1e-8
    for i in range(max_epochs):
        dw1, db1, dw2, db2 = 0, 0, 0, 0
        for x,y in zip(X,Y):
            dw1 += grad_w(W1, b1, x, y)
            db1 += grad_b(W1, b1, x, y)
            dw2 += grad_w(W2, b2, x, y)
            db2 += grad_b(W2, b2, x, y)

        v_w1 = v_w1 + dw1**2
        v_b1 = v_b1 + db1**2
        v_w2 = v_w2 + dw2**2
        v_b2 = v_b2 + db2**2

        W1 = W1 - (eta/np.sqrt(v_w1 + eps)) * dw1
        b1 = b1 - (eta/np.sqrt(v_b1 + eps)) * db1
        W2 = W2 - (eta/np.sqrt(v_w2 + eps)) * dw2
        b2 = b2 - (eta/np.sqrt(v_b2 + eps)) * db2

    parameters = {"W1": W1, "b1": b1,"W2": W2,"b2": b2}
    return parameters

# In[ ]:

def neural_network_model(X, Y, hidden_unit, num_iterations = 1000):
    np.random.seed(3)
    input_unit = define_structure(X, Y)[0]
    output_unit = define_structure(X, Y)[2]

    parameters = parameters_initialization(input_unit, hidden_unit, output_unit)

    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']

    for i in range(0, num_iterations):
        A2, cache = forward_propagation(X, parameters)
        cost = cross_entropy_cost(A2, Y, parameters)
        grads = backward_propagation(parameters, cache, X, Y)
        parameters = do_adagrad(parameters, grads)
        if i % 5 == 0:
            print ("Cost after iteration %i: %f" %(i, cost))
    return parameters
parameters = neural_network_model(X_train, y_train, 4, num_iterations=1000)

# In[ ]:

def prediction(parameters, X):
    A2, cache = forward_propagation(X, parameters)
    predictions = np.round(A2)

    return predictions

# In[ ]:

predictions = prediction(parameters, X_train)
print ('Accuracy Train: %d' % float((np.dot(y_train, predictions.T) + np.dot(1 - y_train, 1 - predictions.T))/float(y_train.size)*100) + '%')
predictions = prediction(parameters, X_test)
print ('Accuracy Test: %d' % float((np.dot(y_test, predictions.T) + np.dot(1 - y_test, 1 - predictions.T))/float(y_test.size)*100) + '%')


import time

class dynamicProgramming:
    def __init__(self, size1, size2):
        self.__result = []
        self.__time_cost = 0.0  # in sec
        self.__size1 = size1
        self.__size2 = size2
        self.__accumulated_table = []  # to keep trace on the length of the subsequence
        self.__solution_table = []  # to keep trace on the solution subsequence

    def get_Result(self):
        return self.__result

    def get_Time(self):
        return self.__time_cost

    def get_accumulated_table(self):
        return self.__accumulated_table

    def get_solution_table(self):
        return self.__solution_table

    def fill_tables(self, seq1, seq2):
        """
        Fills up two tables, accumulated length table and the solution construction table
        :param seq1: a list, the first sequence
        :param seq2: a list, the second sequence
        :return: return the filled accumulated length table and the solution construction table
        """
        for i in range(self.__size1 + 1):
            accumulated_row = []
            solution_row = []
            for j in range(self.__size2 + 1):
                accumulated_row.append(0)
                solution_row.append('X')
            self.__accumulated_table.append(accumulated_row)
            self.__solution_table.append(solution_row)

        for i in range(1, self.__size1 + 1):
            for j in range(1, self.__size2 + 1):
                if seq1[i - 1] == seq2[j - 1]:
                    self.__accumulated_table[i][j] = self.__accumulated_table[i - 1][j - 1] + 1
                    self.__solution_table[i][j] = 'Down'
                else:
                    if self.__accumulated_table[i - 1][j] >= self.__accumulated_table[i][j - 1]:
                        self.__accumulated_table[i][j] = self.__accumulated_table[i - 1][j]
                        self.__solution_table[i][j] = 'Up'
                    else:
                        self.__accumulated_table[i][j] = self.__accumulated_table[i][j - 1]
                        self.__solution_table[i][j] = 'Left'

        return self.__accumulated_table, self.__solution_table

    def __construct_Result(self, seq1, i, j):
        """
        Constructs the solution of the longest subsequence
        :param seq1: a list, the first sequence
        :param i: int, length of the first sequence
        :param j: int, length of the second sequence
        :return:
        """
        if i == 0 or j == 0:
            return
        if self.__solution_table[i][j] == 'Down':
            self.__construct_Result(seq1, i - 1, j - 1)
            self.__result.append(seq1[i - 1])
        elif self.__solution_table[i][j] == 'Up':
            self.__construct_Result(seq1, i - 1, j)
        else:
            self.__construct_Result(seq1, i, j - 1)

    def run(self, seq1, seq2):
        """
        Runs the algorithm
        :param seq1: a list
        :param seq2: a list
        :return:
        """
        start_time = time.time()
        self.fill_tables(seq1, seq2)
        self.__construct_Result(seq1, self.__size1, self.__size2)
        end_time = time.time()
        self.__time_cost = end_time - start_time


import numpy as np
import matplotlib.pyplot as plt

# 目标函数:y=x^2
def func(x):
    return np.square(x)

# 目标函数一阶导数:dy/dx=2*x
def dfunc(x):
    return 2 * x

# Gradient Descent
def GD(x_start, df, epochs, lr):
    """
    梯度下降法。给定起始点与目标函数的一阶导函数，求在epochs次迭代中x的更新值
    :param x_start: x的起始点
    :param df: 目标函数的一阶导函数
    :param epochs: 迭代周期
    :param lr: 学习率
    :return: x在每次迭代后的位置（包括起始点），长度为epochs+1
    """
    xs = np.zeros(epochs+1)
    x = x_start
    xs[0] = x
    for i in range(epochs):
        dx = df(x)
        # v表示x要改变的幅度
        v = - dx * lr
        x += v
        xs[i+1] = x
    return xs

def demo1_GD_lr():
    # 函数图像
    line_x = np.linspace(-5, 5, 100)
    line_y = func(line_x)
    plt.figure('Gradient Desent: Learning Rate')

    x_start = -5
    epochs = 5

    lr = [0.1, 0.3, 0.9]

    color = ['r', 'g', 'y']
    size = np.ones(epochs+1) * 10
    size[-1] = 70
    for i in range(len(lr)):
        x = GD(x_start, dfunc, epochs, lr=lr[i])
        plt.subplot(1, 3, i+1)
        plt.plot(line_x, line_y, c='b')
        plt.plot(x, func(x), c=color[i], label='lr={}'.format(lr[i]))
        plt.scatter(x, func(x), c=color[i])
        plt.legend()
    plt.show()
demo1_GD_lr()

2022-2022大二第二学期/最优化/梯度下降法/附录1代码.py


from sqlalchemy import MetaData, Table, String, Integer, Column, Boolean, DateTime
from sqlalchemy import create_engine, insert, update, delete, select, exc

from ..Telnet.Alarm import Alarm

class AlarmDatabase:
    def __init__(self):
        self.engine = create_engine('sqlite:///alarms.db')
        self.create_alarm_table()
        self.clear_tables()

    def clear_tables(self):
        conn = self.engine.connect()
        conn.execute(delete(self.alarms))
        conn.execute(delete(self.nodes))

    def create_alarm_table(self):
        metadata = MetaData()
        self.alarms = Table('alarmwindow_alarms', metadata,
                            Column('id', Integer(), nullable=False),
                            Column('type', String(7), nullable=False),
                            Column('raising_time', DateTime, nullable=False),
                            Column('ceasing_time', DateTime, nullable=True),
                            Column('managed_object', String(30)),
                            Column('object_name', String(30)),
                            Column('slogan', String(30)),
                            Column('descr', String(60)),
                            Column('text', String(300), nullable=False),
                            Column('is_active', Boolean(), nullable=False),
                            Column('node_id', Integer()),
                            Column('node_update_id', Integer())
                            )

        self.nodes = Table('alarmwindow_nodes', metadata,
                           Column('id', Integer(), autoincrement=True, primary_key=True),
                           Column('name', String(20), nullable=False, unique=True),
                           Column('update_id', Integer(), default=0)
                           )
        metadata.create_all(self.engine)

    def get_current_update_id(self, node_id) -> int:
        conn = self.engine.connect()
        query = select(self.nodes.c.update_id).where(
            self.nodes.c.id == node_id
        )
        result = conn.execute(query).fetchone()
        return result[0]

    def insert_new_alarms(self, alarm_objects: list[Alarm]):
        if not len(alarm_objects):
            return
        target_node = None
        update_id = self.get_current_update_id(alarm_objects[0].node_id)
        query = insert(self.alarms).values(
            [
                {
                    'id': alarm.id,
                    'type': alarm.type,
                    'raising_time': alarm.raising_time,
                    'managed_object': alarm.managed_object,
                    'object_name': alarm.object_name,
                    'slogan': alarm.slogan,
                    'descr': alarm.descr,
                    'text': alarm.text,
                    'is_active': alarm.is_active,
                    'node_id': alarm.node_id,
                    'node_update_id': update_id if alarm.node_id == target_node
                    else self.get_current_update_id(alarm.node_id)
                } for alarm in alarm_objects
            ]
        )

        conn = self.engine.connect()
        conn.execute(query)

    def update_ceased_alarms(self, alarm_objects: list[Alarm]):
        if not len(alarm_objects):
            return
        conn = self.engine.connect()
        transaction = conn.begin()
        last_node = None
        update_id = self.get_current_update_id(alarm_objects[0].node_id)
        for alarm in alarm_objects:
            if last_node != alarm.node_id:
                update_id = self.get_current_update_id(alarm.node_id)
                last_node = alarm.node_id
            upd = update(self.alarms).where(
                self.alarms.c.id == alarm.id
            ).values({
                'is_active': False,
                'ceasing_time': alarm.ceasing_time,
                'node_update_id': update_id
            })
            conn.execute(upd)
        transaction.commit()

    def increase_update_id(self, controller_id):
        conn = self.engine.connect()
        upd = update(self.nodes).where(
            self.nodes.c.id == controller_id
        ).values(
            {'update_id': self.nodes.c.update_id + 1}
        )
        conn.execute(upd)

    def add_node(self, node_name):
        conn = self.engine.connect()
        query = insert(self.nodes).values(
            {
                'name': node_name,
                'update_id': 0
            }
        )
        try:
            conn.execute(query)
        except exc.IntegrityError:
            pass

    def get_node_id(self, name) -> int:
        conn = self.engine.connect()
        query = select(self.nodes.c.id).where(
            self.nodes.c.name == name
        )
        result = conn.execute(query).fetchone()
        return result[0]


from datetime import date, datetime, timedelta

from django.test import TestCase
from django.utils.formats import localize
from model_bakery import baker

from evap.evaluation.models import Evaluation, Course, Contribution, Questionnaire
from evap.evaluation.models_logging import FieldAction

class TestLoggedModel(TestCase):
    def setUp(self):
        self.old_start_date = datetime.now()
        self.new_start_date = self.old_start_date + timedelta(days=10)

        self.course = baker.make(Course)

        self.evaluation = baker.make(
            Evaluation,
            course=self.course,
            state="prepared",
            vote_start_datetime=self.old_start_date,
            vote_end_date=date.today() + timedelta(days=20),
        )
        self.evaluation.save()  # first logentry

        self.evaluation.vote_start_datetime = self.new_start_date
        self.evaluation.save()  # second logentry

        self.logentry = self.evaluation.related_logentries()[1]

    def test_voters_not_in_evaluation_data(self):
        self.assertFalse(any("voters" in entry.data for entry in self.evaluation.related_logentries()))

    def test_datetime_change(self):
        self.assertEqual(
            self.logentry.data["vote_start_datetime"],
            {"change": [localize(self.old_start_date), localize(self.new_start_date)]},
        )

    def test_data_attribute_is_correctly_parsed_to_fieldactions(self):
        self.assertEqual(
            self.logentry.field_context_data["vote_start_datetime"],
            [
                FieldAction(
                    "Start of evaluation",
                    "change",
                    [localize(self.old_start_date), localize(self.new_start_date)],
                )
            ],
        )

    def test_deletion_data(self):
        self.assertEqual(self.evaluation._get_change_data(action_type="delete")['course']['delete'][0], self.course.id)
        self.evaluation.delete()
        self.assertEqual(self.evaluation.related_logentries().count(), 0)

    def test_creation(self):
        course = baker.make(Course)
        self.assertEqual(course.related_logentries().count(), 1)

    def test_related_logged_model_creation(self):
        self.assertEqual(self.evaluation.related_logentries().count(), 2)
        contribution = baker.make(Contribution, evaluation=self.evaluation)
        self.assertFalse(contribution.related_logentries().exists())
        self.assertEqual(self.evaluation.related_logentries().count(), 3)

    def test_m2m_creation(self):
        self.assertEqual(self.evaluation.related_logentries().count(), 2)
        questionnaire = baker.make(Questionnaire)
        contribution = self.evaluation.contributions.get(contributor__isnull=True)
        contribution.questionnaires.add(questionnaire)
        self.assertEqual(self.evaluation.related_logentries().count(), 3)
        self.assertEqual(self.evaluation.related_logentries().order_by("id").last().data['questionnaires']['add'], [questionnaire.id])

    def test_none_value_not_included(self):
        baker.make(Contribution, evaluation=self.evaluation, label="testlabel")
        self.assertIn("label", self.evaluation.related_logentries().order_by("id").last().data)

        baker.make(Contribution, evaluation=self.evaluation, label=None)
        self.assertNotIn("label", self.evaluation.related_logentries().order_by("id").last().data)


#!/usr/bin/env python3
#https://leetcode.com/problems/sqrtx/#/description

# mySqrt(9)  = 3
# mySqrt(36)  = 6
# mySqrt(144)  = 12
# mySqrt(900)  = 30

def mySqrt(x):
    """
    :type x: int
    :rtype: int
    """
    i = 0
    for i in range(1, x):
        if (i * i) == x:
            break
    return i


__author__ = 'florian-strub'
import os
import pickle
import collections
import tensorflow as tf

from GarnetNetwork import GarnetNetwork
from GarnetDataset import GarnetDataset
from Garnet import Garnet
from tools import *
from argparse import Namespace

import matplotlib.pyplot as plt

Dataset = collections.namedtuple('Dataset', ['train', 'test', 'eval'])

params = Namespace(

    Ns=100,
    Na=5,
    Nb=1,

    noPlayer=2,

    gamma=0.9,

    garnet="S_linear_T2_zero",
    sparsity=0.5,

    coefSample=5,

    Qlayers=[80],
    Pilayers=[80],

    Qregu=1e-7,
    Pregu=1e-7,
    dropout=1,

    Q_lrt=0.001,
    Pi_lrt=5e-05,

    epoch=5000,
    batchSize=20,

    gpu=1,
    outDir=".",
    seed=-1,
)

def start(params, garnet=None, dataset=None):
    print (params)

    no_samples = params.Ns * params.Na * params.coefSample

    if params.seed > -1:
        np.random.seed(params.seed)
        rd.seed(params.seed)
        tf.set_random_seed(params.seed)

    ##############################################
    # Build training dataset

    if garnet is None:
        garnet = Garnet(params.Ns, params.Na, params.Nb, params.noPlayer, params.sparsity, params.garnet)

        if dataset is None:
            dataset = Dataset(
                train=GarnetDataset(Ns=params.Ns, Na=params.Na, samples=garnet.uniform_batch_data(no_samples)),
                test=GarnetDataset(Ns=params.Ns, Na=params.Na, samples=garnet.uniform_batch_data(params.Ns * params.Na)),
                eval=GarnetDataset(Ns=params.Ns, Na=params.Na, samples=garnet.eval_batch_data()),
            )

        print ("Starting points")
        print (garnet.start)

    #############################################
    # Compute random policy

    policy_random = (1.0 * np.ones((garnet.Ns, garnet.Na))) / (1.0 * garnet.Na)
    err_random = garnet.l2errorDiffQstarQpi(policy_random, params.gamma)
    print ("Error random policy Bellman")
    print (err_random)
    print ("")

    #############################################
    # build network

    Qlayers = [params.Ns] + params.Qlayers + [params.Na]
    Pilayers = [params.Ns] + params.Pilayers + [params.Na]

    print (["Qlayers  : ", Qlayers])
    print (["Pilayers : ", Pilayers])

    fApp = GarnetNetwork(qLayerSize=Qlayers, policyLayerSize=Pilayers, params=params)
    plots = fApp.execute(dataset=dataset, nEpoch=params.epoch, nMiniBatch=params.batchSize, garnet=garnet, gamma=params.gamma)

    #############################################
    # plot results

    print("Final Error random policy Bellman")
    print (err_random)

    res = {}
    res["garnet"] = garnet
    res["params"] = params
    res["plots"] = plots

    with open(os.path.join(params.outDir, "data.p"), "wb") as file:
        pickle.dump(res, file=file)

    # plot with various axes scales

    x = range(0, len(plots["errResidual"]["train"]))

    # Two subplots, the axes array is 1-d
    fig, axarr = plt.subplots(3, sharex=True)

    axarr[0].set_title(str(params))

    axarr[0].plot(x, np.log10(plots["errResidual"]["train"]), color='b', linestyle='-')
    axarr[0].plot(x, np.log10(plots["errResidual"]["test"]), color='r', linestyle='-')
    axarr[0].legend(["Train residual Error", "Test residual Error"])

    colors = ["b", "k", "r", "g", "y", "m"]
    legends = []
    for player in range(params.noPlayer):
        axarr[1].plot(x, plots["errPolicy"][player], color=colors[player], linestyle='-')
        axarr[1].plot(x, [err_random[player]] * len(x), color=colors[player], linestyle='--')

        legends += ["NN policy " + str(player), "RND policy " + str(player)]

    axarr[1].legend(legends)
    axarr[1].set_ylim([0.0, 1.0])

    actions = np.array(plots["histoAction"])
    legend = []
    for i in range(params.Na):
        axarr[2].plot(x, actions[:, i])
        legend.append('action ' + str(i))
    axarr[2].legend(legend)
    axarr[2].set_ylim([0.0, 1.0])

    fig.savefig(params.outDir + "/training.eps")
    plt.close(fig)

    return plots

if __name__ == '__main__':
    start(params)


#!/home/einfochips/Desktop/YOLO/evn/bin/python3

import torch
import numpy as np
import cv2
import rospy
from sensor_msgs.msg import Image
from geometry_msgs.msg import Point
from std_msgs.msg import Int16
from cv_bridge import CvBridge,CvBridgeError
import time
import math
from tesingm.srv import servicetest
from tesingm.srv import servicetestRequest
from tesingm.srv import servicetestResponse
import threading

model = torch.hub.load('ultralytics/yolov5','custom',path='/home/einfochips/Desktop/YOLO/yolov5/runs/train/exp4/weights/last.pt')
# model.conf = 0.65
model.classes = (0,1,2,3,5,6,7)
a = model.classes
steps_name = {
   0: "ScreenOff",
   1: "ScreenOn",
   2: "Zero",
   3: "Settings",
   5: "Bluetooth",
   6: "BtnOff",
   7: "BtnOn",
}

steps_count = (0,1,2,3,5,6,7)
steps = 0
nn_new_x= None
new_y = None
old_coord = [0.0, -163.0]
new_steps = 0
c_x = None
c_y = None

def camera_to_world(cam_mtx, r, t, img_points):
    inv_k = np.asmatrix(cam_mtx).I
    r_mat = np.zeros((3, 3), dtype=np.float64)
    cv2.Rodrigues(r, r_mat)
    # invR * T
    inv_r = np.asmatrix(r_mat).I  # 3*3
    transPlaneToCam = np.dot(inv_r, np.asmatrix(t))  # 3*3 dot 3*1 = 3*1
    world_pt = []
    coords = np.zeros((3, 1), dtype=np.float64)
    for img_pt in img_points:
        coords[0][0] = img_pt[0][0]
        coords[1][0] = img_pt[0][1]
        coords[2][0] = 1.0
        worldPtCam = np.dot(inv_k, coords)  # 3*3 dot 3*1 = 3*1
        # [x,y,1] * invR
        worldPtPlane = np.dot(inv_r, worldPtCam)  # 3*3 dot 3*1 = 3*1
        # zc
        scale = transPlaneToCam[2][0] / worldPtPlane[2][0]
        # zc * [x,y,1] * invR
        scale_worldPtPlane = np.multiply(scale, worldPtPlane)
        # [X,Y,Z]=zc*[x,y,1]*invR - invR*T213596990823
        worldPtPlaneReproject = np.asmatrix(scale_worldPtPlane) - np.asmatrix(transPlaneToCam)  # 3*1 dot 1*3 = 3*3
        pt = np.zeros((3, 1), dtype=np.float64)
        pt[0][0] = worldPtPlaneReproject[0][0]
        pt[1][0] = worldPtPlaneReproject[1][0]
        pt[2][0] = 0
        world_pt.append(pt.T.tolist())
    return world_pt

def detect(image):

    global steps_count, steps, nn_new_x, new_y, results

    bridge = CvBridge()
    open=bridge.imgmsg_to_cv2(image)
    col=cv2.cvtColor(open,cv2.COLOR_BGR2RGB)
    results = model(col)

    cv2.imshow('YOLO',np.squeeze(results.render()))
    cv2.waitKey(1)
    # a = torch.tensor(results.xyxy[0])[0][0].item()
    # print(a[0][0].item())
    # print(int(torch.tensor(results.xyxy[0])[0][5].item()))

    try:
        if int(torch.tensor(results.xyxy[0])[0][5].item()) == steps_count[steps] and len(torch.tensor(results.xyxy[0])) == 1:
            if a[steps] == steps_count[steps]:
                # cv2.circle(img=col,center=(((int(torch.tensor(results.xyxy[0])[0][0].item())+int(torch.tensor(results.xyxy[0])[0][2].item()))//2),((int(torch.tensor(results.xyxy[0])[0][1].item())+int(torch.tensor(results.xyxy[0])[0][3].item()))//2)),radius=3,color=(0,255,0),thickness=-1)
                #cv2.imshow('YOLO',np.squeeze(results.render()))
                # rate = rospy.Rate(0.5)
                c_x = ((int(torch.tensor(results.xyxy[0])[0][0].item())+int(torch.tensor(results.xyxy[0])[0][2].item()))//2)
                c_y = ((int(torch.tensor(results.xyxy[0])[0][1].item())+int(torch.tensor(results.xyxy[0])[0][3].item()))//2)
                # print("center points of object == :", c_x,c_y)
                #zp = int(torch.tensor(results.xyxy[0])[0][5].item())
                cur_x, cur_y, cur_z = 0, -163,212
                K=np.array([[434.2196960449219, 0.0, 266.6663412990292],[0.0, 491.9661865234375,238.71625361069528],[0.0, 0.0, 1.0]],dtype=np.float64).reshape(3,3)
                R=np.array([[-0.050112674474709636], [-3.0323416167631896],[0.012589481083534122]],dtype=np.float64).reshape(3,1)
                T=np.array([[16.512429040858382], [51.675548988183714], [202.46864073880207]],dtype=np.float64).reshape(3,1)

                x, y, _ = camera_to_world(K,R,T, np.array([c_x, c_y]).reshape((1, 1, 2)))[0][0]

                t = math.sqrt(x * x + y * y + 120 * 120) / 120

                new_x, new_y = cur_x + x, cur_y + y + 15
                nn_new_x = new_x - 25
                # print("continuous xandy for:",steps_count[steps]  ,nn_new_x, new_y)
                    #jetmax.set_position((nn_new_x, new_y, 80), t)
                    #rospy.sleep(t)
                    #jetmax.set_position((new_x , new_y, 70), 0.3)
                    #rospy.sleep(t + 0.6)

            else:
                pass
        elif int(torch.tensor(results.xyxy[0])[0][5].item()) == steps_count[steps] and len(torch.tensor(results.xyxy[0])) > 1:
                c_x = ((int(torch.tensor(results.xyxy[0])[0][0].item())+int(torch.tensor(results.xyxy[0])[0][2].item()))//2)
                c_y = ((int(torch.tensor(results.xyxy[0])[0][1].item())+int(torch.tensor(results.xyxy[0])[0][3].item()))//2)
                # print("center points of object 0>1 :", c_x,c_y)
                #zp = int(torch.tensor(results.xyxy[0])[0][5].item())
                cur_x, cur_y, cur_z = 0, -163,212
                K=np.array([[434.2196960449219, 0.0, 266.6663412990292],[0.0, 491.9661865234375,238.71625361069528],[0.0, 0.0, 1.0]],dtype=np.float64).reshape(3,3)
                R=np.array([[-0.050112674474709636], [-3.0323416167631896],[0.012589481083534122]],dtype=np.float64).reshape(3,1)
                T=np.array([[16.512429040858382], [51.675548988183714], [202.46864073880207]],dtype=np.float64).reshape(3,1)

                x, y, _ = camera_to_world(K,R,T, np.array([c_x, c_y]).reshape((1, 1, 2)))[0][0]

                t = math.sqrt(x * x + y * y + 120 * 120) / 120

                new_x, new_y = cur_x + x, cur_y + y + 15
                nn_new_x = new_x - 25
                # print("continuous xandy for:",steps_count[steps]  ,nn_new_x, new_y)

        elif int(torch.tensor(results.xyxy[0])[1][5].item()) == steps_count[steps] and len(torch.tensor(results.xyxy[0])) > 1:
                c_x = ((int(torch.tensor(results.xyxy[0])[1][0].item())+int(torch.tensor(results.xyxy[0])[1][2].item()))//2)
                c_y = ((int(torch.tensor(results.xyxy[0])[1][1].item())+int(torch.tensor(results.xyxy[0])[1][3].item()))//2)
                # print("center points of object 1>1 :", c_x,c_y)
                #zp = int(torch.tensor(results.xyxy[0])[0][5].item())
                cur_x, cur_y, cur_z = 0, -163,212
                K=np.array([[434.2196960449219, 0.0, 266.6663412990292],[0.0, 491.9661865234375,238.71625361069528],[0.0, 0.0, 1.0]],dtype=np.float64).reshape(3,3)
                R=np.array([[-0.050112674474709636], [-3.0323416167631896],[0.012589481083534122]],dtype=np.float64).reshape(3,1)
                T=np.array([[16.512429040858382], [51.675548988183714], [202.46864073880207]],dtype=np.float64).reshape(3,1)

                x, y, _ = camera_to_world(K,R,T, np.array([c_x, c_y]).reshape((1, 1, 2)))[0][0]

                t = math.sqrt(x * x + y * y + 120 * 120) / 120

                new_x, new_y = cur_x + x, cur_y + y + 15
                nn_new_x = new_x - 25
                # print("continuous xandy for:",steps_count[steps]  ,nn_new_x, new_y)

        else:
            # nn_new_x= None
            # new_y = None
            # c_x = None
            # c_y = None
            pass
    except IndexError:
            cv2.imshow('YOLO',col)
            cv2.waitKey(1)
    #cv2.imshow('YOLO',np.squeeze(results.render()))
    cv2.imshow('YOLO',col)

    cv2.waitKey(1)

def requesting(msg):
    global old_coord, new_steps, results

    # if nn_new_x == old_coord[0] and new_y == old_coord[1] and new_steps == steps and len(torch.tensor(results.xyxy[0])) >= 1:
    #     print("old coord:", old_coord)
    #     time.sleep(0.5)
    #     client = client_obj((str(nn_new_x) +" "+ str(new_y)))
    #     return client.reply
    # else:
    #     new_steps = steps
    #     old_coord = [nn_new_x,new_y]
    #     new_steps = steps
    #     print("changing coord:", old_coord)

    try:
        time.sleep(0.5)
        print(str(nn_new_x) +" "+ str(new_y))
        client = client_obj((str(nn_new_x) +" "+ str(new_y)))
        return client.reply
    except:
        print("Service call failed")

def main():

    cam_sub = rospy.Subscriber("/usb_cam/image_rect_color",Image,detect)
    try:
        rospy.spin()
    except KeyboardInterrupt:
        cv2.destroyAllWindows()

if __name__=="__main__":
    rospy.init_node("opencv")
    client_obj = rospy.ServiceProxy('test_service', servicetest)
    lock = threading.RLock()
    t1 = threading.Thread(target = main)
    t1.start()
    #print("moving")
    #time.sleep(2)
    while True:
        time.sleep(3)
        # print("Global Steps:-", steps)
        # print("global x&y:",nn_new_x, new_y)
        # print("midpoints x&y:",c_x,c_y)
        rospy.wait_for_service('test_service')
        # if nn_new_x == None:
        #     print("pass")
        # else:
        #     #time.sleep(1)
            # print("latest coord:", nn_new_x, new_y)

        with lock:
            c = str(nn_new_x)+ str(new_y)
            print("c",c)
            s = requesting(c)
            # print("Reply:",s)
            if s == "Done":
                try:
                    while True:
                        if int(torch.tensor(results.xyxy[0])[0][5].item()) == steps_count[steps]:
                            if steps>6:
                                break
                            steps += 1
                            # print("Global Steps after incrementing:-", steps)
                            # print("next to detect:" , steps_name[steps])
                            break
                        else:
                            # print("Global Steps remained same:-", steps)
                            break
                except:
                    pass
            elif s == "None":
                pass


import discord
from datetime import datetime
from discord.ext import commands
from discord.utils import get
import json
from discord.ext import commands
import manageDB#DB 관리하기, 테트리오 API를 사용하여 사용자 정보를 받아오기
import GetLB
import requests

token=open("token.txt","r").readline()
game=discord.Game(' )도움 입력')
bot = commands.Bot(command_prefix=')',intents=discord.Intents.all(),activity=game)

def lbSend(username):#리더보드를 임베드로 반환
    _dict = GetLB.Get(username)
    if(_dict=='no user'):
        embed= discord.Embed(title='유저를 찾을 수 없습니다')
    else:
        embed = discord.Embed(title='**'+_dict['name'].upper()+'**님의 리더보드입니다',color=discord.Color.blue())
        img_url='https://tetr.io/user-content/avatars/'+_dict['_id']+'.jpg'
        if('<Response [404]>'==str(requests.get(img_url))):
            img_url='https://media.discordapp.net/attachments/1096103445024485488/1096103529506164867/logo.png'
        embed.set_thumbnail(url=img_url)
        embed.add_field(name='계정 생성일', value=_dict['ts'], inline=False)
        embed.add_field(name='40LINE 최고 기록', value=str(_dict['40L']//60)+'분 '+str(_dict['40L']%60)+'초', inline=True)
        embed.add_field(name='BLITZ 최고 기록', value=str(_dict['blitz']), inline=False)
        embed.add_field(name='LEAGUE 기록',value='', inline=False)
        embed.add_field(name='플레이 횟수', value=str(_dict['league']['playeds'])+'회', inline=True)
        embed.add_field(name='이긴 횟수', value=str(_dict['league']['wins'])+'회', inline=True)
        embed.add_field(name='RANK', value=str(_dict['league']['rank'])+' rank', inline=True)

    return(embed)

def helpSend():
    embed = discord.Embed(title='**SunrinTETRIO**에서 사용할 수 있는 명령어들 입니다',color=discord.Color.blue())
    embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/1096103445024485488/1096103529506164867/logo.png")
    embed.add_field(name='`)추가 [사용자 이름] [게임 닉네임]`', value='DB에 사용자 정보를 업로드 합니다', inline=False)
    embed.add_field(name='`)리더보드 [게임 닉네임]`', value='게임 리더보드를 간략화하여 출력합니다', inline=False)
    embed.add_field(name='`)순위 [페이지 번호(기본:1)]`', value='선린고의 학생들의 40L기록을 순위로 나열하여 출력 합니다', inline=False)

    return(embed)

def rankSend(pageNum):
    userDB=open(file='./DB/userData.json',mode='r',encoding="UTF-8").read()
    _dict = json.loads(userDB)
    rankLi=manageDB.updateRank()
    embed = discord.Embed(title='선린인고 학생들의 40L 순위입니다.',color=discord.Color.blue())

    for i in range(pageNum*5,pageNum*5+5):
        userCode=rankLi[i][0]
        userRecord=rankLi[i][1]
        if(userRecord==9999):
            break
        embed.add_field(name=str(i+1)+'위', value=_dict[userCode]['name']+'('+_dict[userCode]['nick']+') '+str(userRecord//60)+'분'+str(userRecord%60)+'초', inline=False)

    return(embed)

@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user}')

@bot.command()#리더보드 leaderboard
async def 리더보드(ctx, arg):
    print(datetime.now().strftime,ctx.author,'가',ctx.guild,'에서 리더보드를 사용 ')
    await ctx.send(embed=lbSend(arg))

@bot.command()#DB 업로드 register
async def 추가(ctx,arg,arg2):
    print(datetime.now(),ctx.author,'가',ctx.guild,'에서 추가를 사용 ')
    check=manageDB.AddUser((arg,arg2.lower()))
    if(check=='no user'):
        await ctx.send('유저가 없습니다.')
    elif(check=='overlap user'):
        await ctx.send('이미 추가된 유저입니다')
    else:
        await ctx.send('사용자 정보가 DB에 업로드 되었습니다')

@bot.command()
async def 순위(ctx,arg=0):
    print(datetime.now(),ctx.author,'가',ctx.guild,'에서 순위를 사용 ')
    try:
        if(int(arg)<=0):
            pageNum=0
        else:
            pageNum=int(arg)-1
    except:
        pageNum=0

    await ctx.send(embed=rankSend(pageNum))

@bot.command()
async def 도움(ctx):
    print(datetime.now(),ctx.author,'가',ctx.guild,'에서 도움을 사용 ')
    await ctx.send(embed=helpSend())

bot.run(token)


#!/usr/bin/python3

import re

# data for the Italian Delight restaurant
restaurant_data = """
Restaurant: Italian Delight - Italian
Ingredients: pasta, tomatoes, cheese, basil
Color: rgb(255, 0, 0)
Social Media: @italiandelight123
Product Code: ABC123
News Headline: Food Critic's Review: Italian Delight Impresses Again
Event: Grand Opening - Sep 15, 2023 - 06:00 PM
"""

# Regular expression patterns
restaurant_name_pattern = r'Restaurant: (.*)'
ingredients_pattern = r'Ingredients: (.*)'
color_pattern = r'Color: rgb\((\d+), (\d+), (\d+)\)'
social_media_pattern = r'Social Media: (@[\w\d]+)'
product_code_pattern = r'Product Code: ([A-Z]+\d+)'
news_headline_pattern = r'News Headline: (.*)'
event_pattern = r'Event: (.*) - (\w{3} \d{2}, \d{4} - \d{2}:\d{2} [APM]+)'

# Extract data for the restaurant
restaurant_name_match = re.search(restaurant_name_pattern, restaurant_data)
ingredients_match = re.search(ingredients_pattern, restaurant_data)
color_match = re.search(color_pattern, restaurant_data)
social_media_match = re.search(social_media_pattern, restaurant_data)
product_code_match = re.search(product_code_pattern, restaurant_data)
news_headline_match = re.search(news_headline_pattern, restaurant_data)
event_match = re.search(event_pattern, restaurant_data)

if restaurant_name_match:
    restaurant_name = restaurant_name_match.group(1)
    print(f"Restaurant Name: {restaurant_name}")

if ingredients_match:
    ingredients = ingredients_match.group(1).split(', ')
    print("Ingredients:")
    for ingredient in ingredients:
        print(f"- {ingredient}")

if color_match:
    red = color_match.group(1)
    green = color_match.group(2)
    blue = color_match.group(3)
    print(f"Color: RGB({red}, {green}, {blue})")

if social_media_match:
    social_media_username = social_media_match.group(1)
    print(f"Social Media Username: {social_media_username}")

if product_code_match:
    product_code = product_code_match.group(1)
    print(f"Product Code: {product_code}")

if news_headline_match:
    news_headline = news_headline_match.group(1)
    print(f"News Headline: {news_headline}")

if event_match:
    event_details = event_match.group(1)
    event_datetime = event_match.group(2)
    print(f"Event Details: {event_details}")
    print(f"Event Date and Time: {event_datetime}")


import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

def basic_lstm(n_timesteps, n_features, name="Basic_LSTM"):
    inputs = tf.keras.Input(shape=(n_timesteps, n_features))
    x = layers.LSTM(units=64,
                    return_sequences=True,
                    name='lstm1')(inputs)
    x = layers.LSTM(units=64,
                    return_sequences=True,
                    name='lstm2')(x)
    outputs = layers.LSTM(units=n_features,
                          return_sequences=True,
                          activation=None)(x)

    return tf.keras.Model(inputs, outputs, name=name)


import cs50
import sys

def main():

    a = sys.argv[1]
    b = sys.argv[2]
    n = int(sys.argv[3])

     # Read files
    try:
        with open(a, "r") as file:
            file1 = file.read()
    except IOError:
        sys.exit(f"Could not read {a}")
    try:
        with open(b, "r") as file:
            file2 = file.read()
    except IOError:
        sys.exit(f"Could not read {b}")

    same_substrings = substrings(file1, file2, n)

    # print("Same lines: ")
    # for line in same_lines:
    #     print(line)

    sys.exit(0)

def substrings(a, b, n):

    """Return substrings of length n in both a and b"""

    print("a:")
    print(a)
    print("b:")
    print(b)

    # Splits sentences from a and b into two lists
    a_substrings = get_substrings(a, n)
    b_substrings = get_substrings(b, n)

    print("a_substrings:")
    print(a_substrings)
    print("b_substrings:")
    print(b_substrings)

    # Convert lists into sets to eliminate duplicates
    a_substrings = set(a_substrings)
    b_substrings = set(b_substrings)

    print("a_substrings:")
    print(a_substrings)
    print("b_substrings:")
    print(b_substrings)

    # Finds the intersection between the two sets (sentences in both a and b)
    intersect = a_substrings.intersection(b_substrings)

    print("Intersect:")
    print(intersect)

    # Creates list for common sentences and appends all elements from intersection
    common_substrings = []

    for element in intersect:
        common_substrings.append(element)

    for substring in common_substrings:
        print(substring)

    return common_substrings

def get_substrings(string, n):

    substring_list = []

    for i in range(len(string) - n):
        substring_list.append(string[i:i+n])

    return substring_list

if __name__ == '__main__':
    main()