# Naming convention
# Component ID = 'component-name-example'

import dash
import plotly
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
import dash_table
from dash.exceptions import PreventUpdate
from dash_extensions import Download
import pandas as pd
from dash.dependencies import Input, Output, State
from app import app
from layouts import plots, cards, filters, tabs
from database import transforms as tf
import plotly.graph_objects as go

import io # Used for downloading csv file

from app import app

white_btn_style = {
    'background-color': '#e6e6e6'
    ,'color': 'black'
    ,'border-radius':'4px'
    ,'border': '1px solid #bbb'
    ,'height': '38px'
    ,'padding': '0 30px'
    #,'font-size': '11px'
}
blue_btn_style = {
    'background-color': '#007ace'
    ,'color': 'white'
    ,'border-radius':'4px'
    ,'border': '1px solid white'
    ,'height': '38px'
    ,'padding': '0 30px'
    #,'font-size': '11px'
}

#####################################################################################################################
####################################### Gauge App - Main Layout #####################################################
#####################################################################################################################

layout = html.Div([

    #######################################
    ########## Navigation Bar #############
    #######################################

    dbc.Row(
            [
            dbc.Col(
                [
                    filters.common_filters
                    ,filters.gauge_filters
                ]
                , width=2
                , style={
                    'margin-top':165
                    ,'margin-bottom':2200
                    #,'backgroundColor':'gray'
                }
            )
            #######################################
            ########## Current App Header #########
            #######################################
            ,dbc.Col(
                dbc.Row(
                    dbc.Col([
                        html.Div(
                            [html.H1('Food NA - Gauge Tracking System')]
                            ,style={'text-align':'center','margin-bottom':80}
                        )
                        #############################
                        ####### Gauges TABS ################
                        ,dbc.Row(html.Div(
                            [
                                dbc.Tabs(
                                    id='tabs-gauge'
                                    ,active_tab='tab-gauge-records'
                                    ,children=[
                                        dbc.Tab(label='Gauge Records', tab_id='tab-gauge-records')
                                        #,dbc.Tab(label='Add Gauge', tab_id='tab-add-gauge')
                                        ,dbc.Tab(label='Gauge Studies', tab_id='tab-gauge-studies')
                                        ,dbc.Tab(label='Gauge Capabilities', tab_id='tab-gauge-capabilities')
                                    ]
                                )
                                ,html.Div(
                                    id='tabs-gauge-content'
                                    ,style={'padding': '5px 5px 0px 5px'} # padding: Top,Right,Bottom,Left
                                )
                                ,html.Br()
                                # ,html.Div(
                                #     id='gauge-save-button-div'
                                #     ,children = html.Button(
                                #         'Save'
                                #         ,id='gauge-save-button'
                                #         #, style={'display', 'none'}
                                #         )
                                # )

                            ] )
                        ,style={
                            'margin-right':-5
                            ,'margin-left':10
                        }
                        )
                    ]
                    )
                )#, style={'backgroundColor':'gray'}
            )
    ], style={'margin-right':10,'margin-left':10, 'margin-top':0})
]
)

#####################################################################################################################
####################################### Callbacks ###################################################################
#####################################################################################################################

# Tabs callback
@app.callback(
    [
        Output('tabs-gauge-content', 'children')
        ,Output('tb-download-btn', 'children')
    ]
    ,[
        Input('tabs-gauge', 'active_tab') # dcc.Tabs
    ]
)
def tab_gauge_content(active_tab):
    if active_tab=='tab-gauge-records':
        download_btn = html.Div([html.Button("Download records", id="download-btn", style=white_btn_style), Download(id="download")])
        #print('tab_gauge_content')
        return tabs.gauge_table, download_btn
    # elif active_tab=='tab-add-gauge':
    #     return tabs.add_gauge_layout, html.Br()
    elif active_tab=='tab-gauge-studies':
        return html.H2('Gauge studies placeholder'), html.Br()
    elif active_tab=='tab-gauge-capabilities':
        return html.H2('Gauge capabilities placeholder'), html.Br()

# #Make table editable
# @app.callback(
#     [Output('gauge-table', 'editable')
#     ,Output('gauge-save-button', 'style')]
#     ,[Input('make-gauge-table-editable', 'value')]

# )
# def make_gauge_tbl_editable(value):
#     #print('make table editable')
#     if value == []:
#         style = {'display':'none'}
#         return False, style
#     elif value == ['Y']:
#         style = blue_btn_style
#         return True, style

# Gauge records filters
@app.callback(Output('gauge-table', 'data')
    ,[
        Input('plant', 'value')
        ,Input('gauge-manufacturer', 'value')
        ,Input('gauge-type', 'value')
        ,Input('gauge-due-soon', 'value')
    ]
)

def tb_gauge_filters(plant, mfg, g_type, due):
    filters_input = {'Plant': plant, 'Manufacturer':mfg, 'Type':g_type, 'Due Soon': due}
    filtered_dict = {k:v for k,v in filters_input.items() if len(v)!=0}
    #print('tb gauge filters')
    if len(filtered_dict)==0:
        return tf.df.to_dict('rows')
    else:
        filtered_table = tf.df.copy()
        for k,v in filtered_dict.items():
            filtered_table = filtered_table.loc[filtered_table[k].isin(v)]
        return filtered_table.to_dict('rows')

# Dynamic dropdown list (gauge filters)
@app.callback(
    [
        Output('gauge-manufacturer', 'options')
        ,Output('gauge-type', 'options')
    ]
    ,[
        Input('plant', 'value')
        ,Input('gauge-type', 'value')
    ]
)

def set_gauge_mfg_options(plant, gauge_type):
    #print('set gauge mfg options')
    if len(plant) == 0:

        mfg_options = tf.drop_options(tf.gauge_manufacturer)
        type_options = tf.drop_options(tf.gauge_types)
        return mfg_options, type_options
    else:
        # Gauge manufacturer dropdown options
        df_ = tf.df.loc[tf.df['Plant'].isin(plant)==True, ['Manufacturer', 'Type']]
        df_mfg = df_['Manufacturer'].dropna().unique()
        mfg_options = tf.drop_options(df_mfg)

        # Gauge type dropdown options
        df_type = df_['Type'].dropna().unique()
        type_options = tf.drop_options(df_type)

        return  mfg_options, type_options

# Download table (csv)
@app.callback(
    Output('download', 'data'),
    [
        Input('download-btn', 'n_clicks')
        ,Input('gauge-table', 'data')
    ]
)
def generate_csv(n_clicks, data):
    #print('generate csv')
    if n_clicks is not None and tf.current_clicks != n_clicks:
        s = io.StringIO()
        pd.DataFrame.from_dict(data).to_csv(s, index=False)
        content=s.getvalue()
        tf.current_clicks = n_clicks
        return dict(filename='data.csv', content=content, type='text/csv')

# #Update gauge records table with user edits
# @app.callback(
#     [
#         Output('table-updated', 'children')
#     ]
#     ,[

#         Input('gauge-table', 'active_cell')
#         ,Input('gauge-table', 'data')
#         ,Input('gauge-table', 'data_previous')
#         ,Input('make-gauge-table-editable', 'value')
#         ,Input('gauge-save-button', 'n_clicks')

#     ]
# )
# def update_gauge_table(active_cell, data, data_previous, editable, clicks):
#     #print('update gauge table') # - using this to troubleshoot missing output id warnings
#     if editable == ['Y']:
#         if active_cell is not None:
#             tf.changed_cells['row_id'].append(active_cell['row_id'])
#             tf.changed_cells['column_id'].append(active_cell['column_id'])
#             print(tf.changed_cells)
#         return ['tracking']
#     else:
#         return ['nothing tracked']

    # if clicks is None:
    #     updated_table = ['Not updated yet']
    #     return updated_table
    # else:


from django.shortcuts import render
from enroll.models import Student,Teacher,Contractor

# Create your views here.

def home(request):
	student_data = Student.objects.all()
	teacher_data = Teacher.objects.all()
	contractor = Contractor.objects.all()

	context = {'student':student_data,'teacher':teacher_data,'contractor':contractor}

	return render(request,'enroll/home.html',context = context)


import cv2
import numpy as np
#img1 = cv2.imread('vlcsnap-2014-01-20-18h51m28s254.PNG')
cap = cv2.VideoCapture(0)

ret, frame = cap.read()

cv2.imshow('image',frame)

cap.release()

cv2.waitKey(0)
cv2.destroyAllWindows()


import os
import openai

from langchain.schema import SystemMessage, HumanMessage
from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain, SequentialChain

from traceloop.sdk import Traceloop

openai.api_key = os.getenv("OPENAI_API_KEY")
Traceloop.init(app_name="langchain_example")

def langchain_app():
    chat = ChatOpenAI(temperature=0)

    first_prompt_messages = [
        SystemMessage(content="You are a funny sarcastic nerd."),
        HumanMessage(content="Tell me a joke about OpenTelemetry."),
    ]
    first_prompt_template = ChatPromptTemplate.from_messages(first_prompt_messages)
    first_chain = LLMChain(llm=chat, prompt=first_prompt_template, output_key="joke")

    second_prompt_messages = [
        SystemMessage(content="You are an Elf."),
        HumanMessagePromptTemplate.from_template(
            "Translate the joke below into Sindarin language:\n {joke}"
        ),
    ]
    second_prompt_template = ChatPromptTemplate.from_messages(second_prompt_messages)
    second_chain = LLMChain(llm=chat, prompt=second_prompt_template)

    workflow = SequentialChain(chains=[first_chain, second_chain], input_variables=[])
    workflow({})

langchain_app()


import streamlit as st
import pandas as pd
import numpy as np
import plotly_express as px
from PIL import Image
import seaborn as sns
st.set_option('deprecation.showPyplotGlobalUse', False)
# load model
import joblib

st.set_page_config(layout="wide")
st.title('ALONA: Allocation - Outcome - Anomali')
# pages = ['Home','Anomali','Allocation','Outcome']
# jump = st.sidebar.selectbox('Select Pages', pages)

menu = ['Anomali','Allocation & Outcome']
alona= Image.open('logo_alona.png')
st.sidebar.image(alona)
choice = st.sidebar.selectbox('Select Menu', menu)

if choice == "Allocation & Outcome":
    st.subheader("Budget Allocations and Prediction of Indexes")
    df = pd.read_excel('belanja_apbd_full.xlsx')
    new = df["nama_pemda"].str.split(" ", n = 1, expand = True)
    df["jenispemda"]= new[0]
    df["namapemda"]= new[1]
    # df.drop(columns =["nama_pemda"], inplace = True)
    df['wilayah'] = np.where(df['jenispemda'].str.contains("Provinsi"), "Provinsi", "Kab_Kota")
    p1, p2, p3 = st.beta_columns((4,0.5,4))
    with p1:
        wilayah = st.selectbox('Choose Region',['Provinsi','Kab_Kota'])
        if wilayah == 'Provinsi':
            df = df[df['wilayah'].isin(['Provinsi'])]
        else:
            df = df[df['wilayah'].isin(['Kab_Kota'])]
        lokasi = df['nama_pemda'].unique()
        kota = st.selectbox('Choose Local Government',lokasi)
        df = df[df['nama_pemda'].isin([kota])]
        df = df[df['Tahun'].isin([2019])]
        lybudget = df.sum(axis=1)
        pop = df['Populasi'].sum()
        base_ipm = df['Base_IPM'].sum()
        base_ahh = df['Base_AHH'].sum()
        base_hls = df['Base_HLS'].sum()
        base_rls = df['Base_RLS'].sum()
        base_ppk = df['Base_PPK'].sum()
        df['Total_Anggaran'] = df.sum(axis=1)
        ekvalue=df['Ekonomi']/df["Total_Anggaran"]
        ksvalue=df['Kesehatan']/df["Total_Anggaran"]
        ktvalue=df['Ketertiban']/df["Total_Anggaran"]
        lkvalue=df['Lingkungan']/df["Total_Anggaran"]
        pbvalue=df['ParBud']/df["Total_Anggaran"]
        plvalue=df['Pelayanan']/df["Total_Anggaran"]
        pdvalue=df['Pendidikan']/df["Total_Anggaran"]
        sovalue=df['Sosial']/df["Total_Anggaran"]
        fsvalue=df['Rumah_Fasum']/df["Total_Anggaran"]
    with p2:
        st.write("")
    with p3:
        # st.write(f"Total Populasi: {pop:.0f} (2019)")
        lytotal = st.text_input(label="Last Year Budget",value="Rp {:,.0f}".format(int(lybudget.values)))
        lypkp = st.text_input(label="Per Capita Budget",value="Rp {:,.0f}".format(int(lybudget.values/pop)))
        # perkapita = st.write(f"Anggaran Perkapita Akan Dialokasikan: {base/pop:.2f}")
    # with p3:
    #     st.write(" ")
    t1, t2 ,t3,t4= st.beta_columns((4,0.5,2,2))
    with t1:
        st.text_input(label="Total Population",value=int(pop))
        # st.write('(Populasi Penduduk Tahun 2019)')
    with t2:
        st.write(" ")
    with t3:
        # st.text_input(label="% Perubahan Anggaran: ",value=str(int(100*((base-lybudget.values)/lybudget.values)))+" %")
        # st.text_input(label="% Perubahan Perkapita: ",value=str(int(100*((base/pop-lybudget.values/pop)/lybudget.values/pop)))+" %")
        basechange = st.number_input(label="% Budget Adjustment",value=0,min_value=-100, max_value=100, step=5)
        allbase = int((basechange/100)*int(lybudget.values)+int(lybudget.values))
    with t4:
        # allbase = lybudget.values
        # alcpkp = lybudget.values/pop
        # base = st.number_input(label="Total Anggaran Akan Dialokasikan",value=allbase,min_value=0, max_value=1000000000000000)
#         base = st.number_input(label="Total Anggaran Akan Dialokasikan",value=allbase,min_value=0, max_value=1000000000000000,step=10000000000)
        base = int(allbase)
        st.text_input(label="Planned Budget",value="Rp {:,.0f}".format(base))
        # pkp = st.text_input(label="Anggaran Perkapita Akan Dialokasikan",value=alpkp)
    # perkapita = st.write(f"Anggaran Perkapita Akan Dialokasikan: {base/pop:.2f}")

    k1,k2,k3= st.beta_columns((4,0.5,4))
    with k1:
        st.write('(Population in 2019)')
    with k2:
        st.write(" ")
    with k3:
        perkapita = st.write("Planned Per Capita Budget: Rp {:,.0f}".format(int(base/pop)))

    c1, c2 ,c3, c4, c5= st.beta_columns((1,1,1,0.5,2))
    with c1:
        st.subheader("Last Year Budget Allocation")
        st.text_input(label="Ekonomi",value="Rp {:,.0f}".format(df['Ekonomi'].sum()))
        st.text_input(label="Kesehatan",value="Rp {:,.0f}".format(df['Kesehatan'].sum()))
        st.text_input(label="Ketertiban",value="Rp {:,.0f}".format(df['Ketertiban'].sum()))
        st.text_input(label="Lingkungan",value="Rp {:,.0f}".format(df['Lingkungan'].sum()))
        st.text_input(label="Pariwisata Budaya",value="Rp {:,.0f}".format(df['ParBud'].sum()))
        st.text_input(label="Pelayanan",value="Rp {:,.0f}".format(df['Pelayanan'].sum()))
        st.text_input(label="Pendidikan",value="Rp {:,.0f}".format(df['Pendidikan'].sum()))
        st.text_input(label="Sosial",value="Rp {:,.0f}".format(df['Sosial'].sum()))
        st.text_input(label="Rumah_Fasum",value="Rp {:,.0f}".format(df['Rumah_Fasum'].sum()))
    with c2:
        st.subheader("Allocated Budget Distribution (%)")
        a = st.number_input(label="Ekonomi",value=100*float(ekvalue.values),min_value=0.0, max_value=100.0, step=1.0)
        b = st.number_input(label="Kesehatan",value=100*float(ksvalue.values),min_value=0.0, max_value=100.0, step=1.0)
        c = st.number_input(label="Ketertiban",value=100*float(ktvalue.values),min_value=0.0, max_value=100.0, step=1.0)
        d = st.number_input(label="Lingkungan",value=100*float(lkvalue.values),min_value=0.0, max_value=100.0, step=1.0)
        e = st.number_input(label="Pariwisata Budaya",value=100*float(pbvalue.values),min_value=0.0, max_value=100.0, step=1.0)
        f = st.number_input(label="Pelayanan",value=100*float(plvalue.values),min_value=0.0, max_value=100.0, step=1.0)
        g = st.number_input(label="Pendidikan",value=100*float(pdvalue.values),min_value=0.0, max_value=100.0, step=1.0)
        h = st.number_input(label="Sosial",value=100*float(sovalue.values),min_value=0.0, max_value=100.0, step=1.0)
        i = st.number_input(label="Rumah_Fasum",value=100*float(fsvalue.values),min_value=0.0, max_value=100.0, step=1.0)

    with c3:
        st.subheader("Budget Allocated for Simulation")
        st.text_input(label="Ekonomi",value="Rp {:,.0f}".format(int(base*a/100)))
        st.text_input(label="Kesehatan",value="Rp {:,.0f}".format(int(base*b/100)))
        st.text_input(label="Ketertiban",value="Rp {:,.0f}".format(int(base*c/100)))
        st.text_input(label="Lingkungan",value="Rp {:,.0f}".format(int(base*d/100)))
        st.text_input(label="Pariwisata_Budaya",value="Rp {:,.0f}".format(int(base*e/100)))
        st.text_input(label="Pelayanan",value="Rp {:,.0f}".format(int(base*f/100)))
        st.text_input(label="Pendidikan",value="Rp {:,.0f}".format(int(base*g/100)))
        st.text_input(label="Sosial",value="Rp {:,.0f}".format(int(base*h/100)))
        st.text_input(label="Rumah_Fasum",value="Rp {:,.0f}".format(int(base*i/100)))

    with c4:
        st.write("")
    with c5:
        st.subheader("Index Change Prediction  \nBased on Machine-Learning Models")
        index = st.selectbox('Pilih Index',['IPM','AHH','HLS','RLS','PPK'])
        if index == "IPM":
            st.subheader("Base IPM Index")
            j = st.number_input(label="Base_IPM",value=base_ipm,min_value=0.0, max_value=100.0, step=1.0)
        elif index == "AHH":
            st.subheader("Base AHH Index")
            j = st.number_input(label="Base_AHH",value=base_ahh,min_value=0.0, max_value=100.0, step=1.0)
        elif index == "HLS":
            st.subheader("Base HLS Index")
            j = st.number_input(label="Base_HLS",value=base_hls,min_value=0.0, max_value=100.0, step=1.0)
        elif index == "RLS":
            st.subheader("Base RLS Index")
            j = st.number_input(label="Base_RLS",value=base_rls,min_value=0.0, max_value=100.0, step=1.0)
        elif index == "PPK":
            st.subheader("Base PPK Index")
            j = st.number_input(label="Base_PPK",value=base_ppk,min_value=0, max_value=30000, step=100)

        total = a+b+c+d+e+f+g+h+i
        # st.subheader("Total Alokasi Anggaran: "+str(total)+"%")
        st.subheader(f"Total Distributed Budget: {total:.2f} %")

        if index == "IPM":
            model= open("ipm_gb.pkl", "rb")
        elif index == "AHH":
            model= open("ahh_stack.pkl", "rb")
        elif index == "HLS":
            model= open("hls_knn.pkl", "rb")
        elif index == "RLS":
            model= open("rls_ridge.pkl", "rb")
        elif index == "PPK":
            model= open("ppk_stack.pkl", "rb")

        pkl=joblib.load(model)
        if st.button("Click to run Prediction"):
            a = (base/pop)*(a/100)
            b = (base/pop)*(b/100)
            c = (base/pop)*(c/100)
            d = (base/pop)*(d/100)
            e = (base/pop)*(e/100)
            f = (base/pop)*(f/100)
            g = (base/pop)*(g/100)
            h = (base/pop)*(h/100)
            i = (base/pop)*(i/100)
            dfvalues = pd.DataFrame(list(zip([a],[b],[c],[d],[e],[f],[g],[h],[i],[j])),columns =[
                    'Ekonomip',
                    'Kesehatanp',
                    'Ketertibanp',
                    'Lingkunganp',
                    'ParBudp',
                    'Pelayananp',
                    'Pendidikanp',
                    'Sosialp',
                    'Rumah_Fasump',
                    'Base_PPK',])
            input_variables = np.array(dfvalues[[
                    'Ekonomip',
                    'Kesehatanp',
                    'Ketertibanp',
                    'Lingkunganp',
                    'ParBudp',
                    'Pelayananp',
                    'Pendidikanp',
                    'Sosialp',
                    'Rumah_Fasump',
                    'Base_PPK']])
            prediction = pkl.predict(input_variables)
            nilai = j+float(prediction)
            st.title(f"Index Prediction: {nilai:.2f}")
            st.subheader(f"Index Change: {(nilai-j):.2f}")

elif choice == 'Anomali':
    st.subheader('Anomali Analysis in Budget Allocations')
    if st.checkbox("Exploratory Data Analysis"):
        # df = pd.read_csv('belanja_apbd.csv',sep=";",usecols=["nama_pemda","Tahun","Ekonomi","Kesehatan","Ketertiban","Lingkungan","ParBud","Pelayanan","Pendidikan","Sosial","Rumah_Fasum","Populasi"])
        # st.subheader('Automated Exploratory Data Analysis')
        df = pd.read_excel('belanja_apbd_full.xlsx')
        df.loc[:,'Total'] = df.sum(numeric_only=True, axis=1)
#         df = df.style.format("{:,.0f}")
        st.dataframe(df.style.format(subset=["Total","Ekonomi","Kesehatan","Ketertiban","Lingkungan","ParBud","Pelayanan","Pendidikan","Sosial","Rumah_Fasum","Populasi"],formatter="Rp {:,.0f}"))
        if st.checkbox('Show Shape'):
            st.write(df.shape)
        if st.checkbox('Show Summary'):
            st.write(df.describe())
        if st.checkbox('Correlation Matrix'):
            dfcor2 = df[['Base_IPM','Base_AHH','Base_HLS','Base_RLS','Base_PPK']]
            dfcor1 = df[["Ekonomi","Kesehatan","Ketertiban","Lingkungan","ParBud","Pelayanan","Pendidikan","Sosial","Rumah_Fasum"]]
            dfall = df[['Base_IPM','Base_AHH','Base_HLS','Base_RLS','Base_PPK',"Ekonomi","Kesehatan","Ketertiban","Lingkungan","ParBud","Pelayanan","Pendidikan","Sosial","Rumah_Fasum"]]
            z0, z1, z2 = st.beta_columns((1,1,1))
            with z0:
                st.write("Correlation Matrix of All Variables")
                st.write(sns.heatmap(dfall.corr(),annot=True,annot_kws={"size": 6}))
                st.pyplot()
            with z1:
                st.write("Correlation Matrix of Budget Functions")
                st.write(sns.heatmap(dfcor1.corr(),annot=True,annot_kws={"size": 10}))
                st.pyplot()
            with z2:
                st.write("Correlation Matrix of Index")
                st.write(sns.heatmap(dfcor2.corr(),annot=True,annot_kws={"size": 12}))
                st.pyplot()
        plot_type = st.selectbox('Select Type of Plot',["bar","line","area","hist","box"])
        all_columns = ["Total","Ekonomi","Kesehatan","Ketertiban","Lingkungan","ParBud","Pelayanan","Pendidikan","Sosial","Rumah_Fasum","Populasi"]
        pemda = st.multiselect('Choose Local Government',df['nama_pemda'])
        df["Tahun"] = df["Tahun"].astype(str)
        # tahun = st.selectbox('Pilih Tahun',[2019,2018])
        # df = df[df['Tahun'].isin([tahun])]
        selected_columns = st.selectbox('Select Column To Visualize', all_columns)
        # dimension = st.selectbox('Add Dimension', all_columns)
        if pemda == None or pemda ==[]:
            dfp = df
            color_value = dfp['nama_pemda']
            mode = 'stack'
            xbox = None
        else:
            dfp = df[df['nama_pemda'].isin(pemda)]
            color_value = dfp['nama_pemda']
            mode = 'group'
            xbox = dfp['nama_pemda']

        if plot_type == "bar":
            fig = px.bar(dfp,x='Tahun',y=selected_columns,color=color_value,barmode=mode)
            st.plotly_chart(fig)
        elif plot_type == "line":
            fig = px.line(dfp,x='Tahun',y=selected_columns,color=color_value)
            st.plotly_chart(fig)
        elif plot_type == "area":
            fig = px.area(dfp,x='Tahun',y=selected_columns,color=color_value)
        elif plot_type == "hist":
            fig = px.histogram(dfp, x=selected_columns)
            st.plotly_chart(fig)
        elif plot_type == "box":
            fig = px.box(dfp, y=selected_columns, x=xbox)
            st.plotly_chart(fig)
        elif plot_type:
            cust_plot = dfp[selected_columns].plot(kind=plot_type)
            st.write(cust_plot)
            st.pyplot()

    if st.checkbox("Budget Allocation vs Index Change Analysis"):
        # df = pd.read_csv('belanja_apbd.csv',sep=";")
        df = pd.read_excel('belanja_apbd_full.xlsx')
        df.loc[:,'Total_anggaran'] = df.sum(numeric_only=True, axis=1)
        st.subheader('Anomali Detection from Trends and Outliers Analysis ')
        index = st.selectbox('Choose Index',['IPM','AHH','HLS','RLS','PPK'])
        anggaran = st.selectbox('Choose Budget Function',["All","Ekonomi","Kesehatan","Ketertiban","Lingkungan","ParBud","Pelayanan","Pendidikan","Sosial","Rumah_Fasum"])
        pemda = st.multiselect('Choose Local Government',df['nama_pemda'].unique())
        df['TotalPerkapita'] = df['Total_anggaran']/df['Populasi']
        df['ek'] = df['Ekonomi']/df['Populasi']
        df['ks'] = df['Kesehatan']/df['Populasi']
        df['kt'] = df['Ketertiban']/df['Populasi']
        df['lk'] = df['Lingkungan']/df['Populasi']
        df['pb'] = df['ParBud']/df['Populasi']
        df['pl'] = df['Pelayanan']/df['Populasi']
        df['pd'] = df['Pendidikan']/df['Populasi']
        df['so'] = df['Sosial']/df['Populasi']
        df['fs'] = df['Rumah_Fasum']/df['Populasi']

        c1, c2 = st.beta_columns((1, 1))
        df["Tahun"] = df["Tahun"].astype(str)
        # st.write(pemda)
        with c1:
            if pemda == None or pemda ==[]:
                dfp = df
                msize=10
            else:
                dfp = df[df['nama_pemda'].isin(pemda)]
                msize=25

            if anggaran == "All":
                fig1 = px.scatter(dfp, x='nama_pemda', y='TotalPerkapita',color='Tahun')
            elif anggaran == "Ekonomi":
                fig1 = px.scatter(dfp, x='nama_pemda', y="ek",color='Tahun')
            elif anggaran == "Kesehatan":
                fig1 = px.scatter(dfp, x='nama_pemda', y="ks",color='Tahun')
            elif anggaran == "Ketertiban":
                fig1 = px.scatter(dfp, x='nama_pemda', y="kt",color='Tahun')
            elif anggaran == "Lingkungan":
                fig1 = px.scatter(dfp, x='nama_pemda', y="lk",color='Tahun')
            elif anggaran == "ParBud":
                fig1 = px.scatter(dfp, x='nama_pemda', y="pb",color='Tahun')
            elif anggaran == "Pelayanan":
                fig1 = px.scatter(dfp, x='nama_pemda', y='pl',color='Tahun')
            elif anggaran == "Pendidikan":
                fig1 = px.scatter(dfp, x='nama_pemda', y="pd",color='Tahun')
            elif anggaran == "Sosial":
                fig1 = px.scatter(dfp, x='nama_pemda', y="so",color='Tahun')
            elif anggaran == "Rumah_Fasum":
                fig1 = px.scatter(dfp, x='nama_pemda', y="fs",color='Tahun')

            fig1.update_traces(marker_size=msize, selector=dict(type='scatter'))
            st.plotly_chart(fig1)

        with c2:
            if pemda == None or pemda ==[]:
                dfp = df
                msize=10
            else:
                dfp = df[df['nama_pemda'].isin(pemda)]
                msize=25

            if index == "IPM":
                fig2 = px.scatter(dfp, x='nama_pemda', y='Base_IPM',color='Tahun')
            elif index == "AHH":
                fig2 = px.scatter(dfp, x='nama_pemda', y='Base_AHH',color='Tahun')
            elif index == "HLS":
                fig2 = px.scatter(dfp, x='nama_pemda', y='Base_HLS',color='Tahun')
            elif index == "RLS":
                fig2 = px.scatter(dfp, x='nama_pemda', y='Base_RLS',color='Tahun')
            elif index == "PPK":
                fig2 = px.scatter(dfp, x='nama_pemda', y='Base_PPK',color='Tahun')
            # fig2.update_layout(yaxis={'categoryorder':'total descending'})
            fig2.update_traces(marker_size=msize, selector=dict(type='scatter'))
            st.plotly_chart(fig2)


#!/usr/bin/env python3

"""Radio scheduling program.

Usage:
  album_times.py [--host=HOST] PORT

Options:
  --host=HOST  Hostname of MPD [default: localhost]
  -h --help    Show this text

Prints out the last scheduling time of every album.
"""

from datetime import datetime
from docopt import docopt
from mpd import MPDClient

def album_sticker_get(client, album, sticker):
    """Gets a sticker associated with an album."""

    # I am pretty sure that MPD only implements stickers for songs, so
    # the sticker gets attached to the first song in the album.
    tracks = client.find("album", album)
    if len(tracks) == 0:
        return

    return client.sticker_get("song", tracks[0]["file"], "album_" + sticker)

def list_albums(client):
    """Lists albums sorted by last play timestamp."""

    # Get all albums
    albums = client.list("album")
    all_albums = list(
        filter(lambda a: a not in ["", "Lainchan Radio Transitions"], albums)
    )

    # Group albums by when they were last scheduled
    albums_by_last_scheduled = {}
    last_scheduled_times = []
    for album in all_albums:
        # Get the last scheduled time, defaulting to 0
        try:
            last_scheduled = int(album_sticker_get(client, album, "last_scheduled"))
        except ValueError:
            last_scheduled = 0

        # Put the album into the appropriate bucket
        if last_scheduled in albums_by_last_scheduled:
            albums_by_last_scheduled[last_scheduled].append(album)
        else:
            albums_by_last_scheduled[last_scheduled] = [album]
            last_scheduled_times.append(last_scheduled)

    # Pick the 10 oldest times
    last_scheduled_times.sort()
    for last_scheduled in last_scheduled_times:
        dt = datetime.utcfromtimestamp(last_scheduled)
        albums = albums_by_last_scheduled[last_scheduled]
        print("{}: {}".format(dt.strftime("%Y-%m-%d %H:%M:%S"), albums))

if __name__ == "__main__":
    args = docopt(__doc__)

    try:
        args["PORT"] = int(args["PORT"])
    except ValueError:
        print("PORT must be an integer")
        exit(1)

    try:
        client = MPDClient()
        client.connect(args["--host"], args["PORT"])
    except Exception as e:
        print(f"could not connect to MPD: {e.args[0]}")
        exit(2)

    list_albums(client)


import xmlrpc.client
import config
import tkinter as tk
from tkinter.filedialog import askopenfilename
import subprocess
import numpy as np
import time
proxy_cluster = xmlrpc.client.ServerProxy('http://localhost:'+str(config.CLUSTER))

# esto lo metemos en un for y vamos tratando uno a uno + tratamiento de errores
# proxy_server1 = xmlrpc.client.ServerProxy('http://localhost:9001')
# proxy_server2 = xmlrpc.client.ServerProxy('http://localhost:9002')
import os

m_list=[]
filename =None
price=None
#---in_csv: path del archivo
#---rowsize: numero de linias por archivo ó numero de nodos
def UploadAction(event=None):
    global filename
    filename = askopenfilename(initialdir='.')
    # Cut path to the file off
    filename = filename.split('/')[len(filename.split('/'))-1]
    print('Selected:', filename)
    label1['text'] = filename

def UploadAction2(min_max):

      global filename
      number_lines = sum(1 for row in (open(filename)))
      list_servers=proxy_cluster.get_servers()
      chunksize = round(number_lines/len(list_servers))
      since=0
      totaltime=0
      time_worker=0
      start_time = time.time()
      if min_max == "min":
            for server in list_servers:
                  minn,time_worker=xmlrpc.client.ServerProxy('http://localhost:'+str(server)).get_min(filename, since, chunksize, price)
                  totaltime=totaltime+float(time_worker)
                  m_list.append(minn)
                  since=since+chunksize
            x = np.array(m_list)
            y = x.astype(np.float64)
            min_value=min(y)
            label2['text'] = min_value
      elif min_max == "max":
            for server in list_servers:
                  maxx,time_worker=xmlrpc.client.ServerProxy('http://localhost:'+str(server)).get_max(filename, since, chunksize, price)
                  totaltime=totaltime+float(time_worker)
                  m_list.append(maxx)
                  since=since+chunksize
            x = np.array(m_list)
            y = x.astype(np.float64)
            max_value=max(y)
            label2['text'] = max_value
######elif --> more options"
      else:
            label2['text'] = "ERROR: Please, select min or max price"
      print("Total Time : "+str(time.time() - start_time)+" seconds in "+str(len(list_servers))+" worker/s")
      print("Time without connexions: "+str(totaltime)+" seconds in "+str(len(list_servers))+" worker/s")

def UploadAction3(price_input):
      global price
      price=price_input

      # Cut path to the file off
def UploadAction4(event=None):
     subprocess.call('start python xmlrpc_server.py', shell=True)
     label4['text'] = str(int(label4['text'])+1)

      # Cut path to the file off

subprocess.call('start python xmlrpc_cluster.py', shell=True)
root= tk.Tk(className='Tick prices')
root.geometry("500x500")

button1 = tk.Button(text='Select File', command=UploadAction, bg='blue', fg='white')
button1.pack(padx=2, pady=5)
label1 = tk.Label(text='Please choose a file')
label1.pack(padx=2, pady=2)
label2 = tk.Label()

button7 = tk.Button(text='Add worker', command=UploadAction4, bg='blue', fg='white')
button7.pack(padx=2, pady=5)
label4 = tk.Label( text='0')
label4.pack(padx=2, pady=2)

label6 = tk.Label(text='----------------Select price type-----------------')
label6.pack(padx=2, pady=2)
button3 = tk.Button(text='Ask', command=lambda:UploadAction3('Ask'), bg='brown', fg='white')
button3.pack(padx=2, pady=5)
button4 = tk.Button(text='Bid', command=lambda:UploadAction3('Bid'), bg='brown', fg='white')
button4.pack(padx=2, pady=5)
button5 = tk.Button(text='AskVolume', command=lambda:UploadAction3('AskVolume'), bg='brown', fg='white')
button5.pack(padx=2, pady=5)
button6 = tk.Button(text='BidVolume', command=lambda:UploadAction3('BidVolume'), bg='brown', fg='white')
button6.pack(padx=2, pady=5)

label3 = tk.Label(text='---------------------------------------------------------')
label3.pack(padx=2, pady=2)
button2 = tk.Button(text='Compute Low price', command=lambda:UploadAction2("min"), bg='red', fg='white')
button2.pack(padx=2, pady=5)
button8 = tk.Button(text='Compute High price', command=lambda:UploadAction2("max"), bg='green', fg='white')
button8.pack(padx=2, pady=5)
label2.pack(padx=2, pady=2)

root.mainloop()


import os
import tempfile
from typing import Generator, List

import pytest

from runexpy.utils import DefaultParamsT

@pytest.fixture()
def script() -> List[str]:
    command = """import sys
print("stdout")
print("stderr", file=sys.stderr)
with open("out.txt", "w") as f:
    f.write("file\\n")
"""
    script = ["python3", "-c", f"{command}"]
    return script

@pytest.fixture()
def default_params() -> DefaultParamsT:
    return {
        "p1": "x",
        "p2": 3,
        "p3": None,
    }

@pytest.fixture()
def campaign_dir() -> Generator[str, None, None]:
    with tempfile.TemporaryDirectory() as tempdir:
        yield os.path.join(tempdir, "temp_campaign")


from typing import Optional, Tuple
from retry import retry

import apimoex
import requests

class MoexAPI:
    @staticmethod
    @retry(tries=3, delay=2, backoff=2)
    def get_ticker_info(session, ticker):
        url = f'https://iss.moex.com/iss/securities/{ticker}.json'
        table = 'boards'
        query = apimoex.requests._make_query(table=table, columns=None)
        data = apimoex.requests._get_short_data(session, url, table, query)
        for info in data:
            if info['secid'] == ticker:
                return info

        raise Exception("No such ticker on MOEX!")

    @staticmethod
    @retry(tries=3, delay=2, backoff=2)
    def get_board_history(
        session: requests.Session,
        security: str,
        start: Optional[str] = None,
        end: Optional[str] = None,
        columns: Optional[Tuple[str, ...]] = (
            "BOARDID",
            "TRADEDATE",
            "CLOSE",
            "VOLUME",
            "VALUE",
        ),
        board: str = "TQBR",
        market: str = "shares",
        engine: str = "stock",
    ):
        return apimoex.get_board_history(session, security, start, end, columns, board, market, engine)


import scipy.io
import numpy as np
from pylgmath.so3.operations import hat
import os
import pickle
from thesis.experiments.utils import StereoLocalizationProblem
import thesis.solvers.local_solver as local_solver
from thesis.solvers.iterative_sdp import iterative_sdp_solution
from thesis.simulation.sim import Camera
import thesis.visualization.plotting as plotting
import thesis.simulation.sim as sim
from typing import List
from datetime import datetime
from copy import deepcopy

RECORD_HISTORY = False

def read_dataset(path: str):
    dataset3 = scipy.io.loadmat(path)

    # F_i: inertial frame
    # F_v: vehicle frame
    # F_c: camera frame

    theta_vk_i = dataset3["theta_vk_i"] # a 3xK matrix
    r_i_vk_i = dataset3["r_i_vk_i"] # a 3xK matrix where the kth column is the groundtruth position of the camera at timestep k

    y_k_j = dataset3["y_k_j"] # 4 x K x 20 array of observations. All components of y_k_j(:, k, j) will be -1 if the observation is invalid
    y_var = dataset3["y_var"] # 4 x 1 matrix of computed variances based on ground truth stereo measurements
    rho_i_pj_i = dataset3["rho_i_pj_i"] # a 3x20 matrix where the jth column is the poisition of feature j

    # camera to vehicle
    C_c_v = dataset3["C_c_v"] # 3 x 3 matrix giving rotation from vehicle frame to camera frame
    rho_v_c_v = dataset3["rho_v_c_v"]

    # intrinsics
    fu = dataset3["fu"]
    fv = dataset3["fv"]
    cu = dataset3["cu"]
    cv = dataset3["cv"]
    b = dataset3["b"]
    print(fu, fv, cu, cv, b)

    cam = Camera(fu, fv, cu, cv, b, R = 0 * np.eye(4), fov_phi_range=(0, 0), fov_depth_range=(0, 0)) # is this noise correct?
    M = np.array(cam.M(), dtype=np.float64)

    T_c_v = np.eye(4)
    T_c_v[:3, :3] = C_c_v
    T_c_v[:3, -1:] = -C_c_v @ rho_v_c_v

    problems = []

    p_max = -np.inf * np.ones((3, 1))
    W = np.array(np.linalg.inv(np.diag(y_var.reshape(-1))), dtype=np.float64)
    #W = np.ones((4, 4), dtype=np.float64)

    for k, psi in enumerate(theta_vk_i.T):

        p_w = rho_i_pj_i.T[:, :, None]
        p_w = np.concatenate((p_w, np.ones_like(p_w[:, 0:1, :])), axis = 1)
        ys_with_invalid = y_k_j[:, k, :].T[:, :, None]
        mask = ~((ys_with_invalid.squeeze(-1) == -1).all(axis = 1))
        p_w = np.array(p_w[mask] , dtype=np.float64)

        if p_w.shape[0] < 3: # reject problems with less than 3 landmark points in the image
            continue

        y = np.array(ys_with_invalid[mask], dtype=np.float64)

        psi = psi.reshape(3, 1)
        psi_mag = np.linalg.norm(psi)
        C_vk_i = np.cos(psi_mag) * np.eye(3) + ( 1 - np.cos(psi_mag) ) * (psi / psi_mag) @ (psi / psi_mag).T - np.sin(psi_mag) * hat(psi / psi_mag)
        T_vk_i = np.eye(4)
        T_vk_i[:3, :3] = C_vk_i
        T_vk_i[:3, -1] = - C_vk_i @ r_i_vk_i[:, k]
        T_ck_i = T_c_v @ T_vk_i

        T_w_ck = np.array(np.linalg.inv(T_ck_i), dtype = np.float64)
        problems.append(
            StereoLocalizationProblem(
                T_wc = T_w_ck,
                p_w = p_w,
                M = M,
                y = y,
                W = W,
            )
        )
        p_max = np.maximum(p_max, T_w_ck[:3, -1:])

    p_wc_extent = p_max

    return problems, p_wc_extent, np.diag(y_var)

def metrics_fcn(problem, num_tries = 100):
    mosek_params = {}
    datum = {}
    local_solution = local_solver.stereo_localization_gauss_newton(problem, log = False, max_iters = 100, num_tries = num_tries, record_history=RECORD_HISTORY)
    iter_sdp_soln = iterative_sdp_solution(problem, problem.T_init, max_iters = 10, min_update_norm = 0.02, return_X = False, mosek_params=mosek_params, max_num_tries = num_tries, record_history=RECORD_HISTORY)
    datum["local_solution"] = local_solution
    datum["iterative_sdp_solution"] = iter_sdp_soln

    return datum

def run_experiment(problems: List[StereoLocalizationProblem], num_local_solve_tries: int, p_wc_extent: np.array):

    exp_time = datetime.today().strftime('%Y-%m-%d-%H:%M:%S')
    dir_path = os.path.dirname(os.path.realpath(__file__))
    exp_dir = os.path.join(dir_path, f"outputs/{exp_time}")
    if not os.path.isdir(exp_dir):
        os.mkdir(exp_dir)

    metrics = []

    for ind, problem in enumerate(problems):
        print(f"Problem {ind+1}/{len(problems)}")
        for _ in range(num_local_solve_tries):
            T_op = sim.generate_random_T(p_wc_extent)
            problem.T_init = T_op
            metrics.append(metrics_fcn(problem))
            metrics[-1]["problem"] = deepcopy(problem)
            metrics[-1]['scene_ind'] = ind
            metrics[-1]['noise_var'] = 'natural'

    with open(os.path.join(exp_dir, "metrics.pkl"), "wb") as f:
        pickle.dump(metrics, f)
    return metrics, exp_dir

def main():
    num_local_solve_tries = 20

    problems, p_wc_extent, y_var = read_dataset('dataset3/dataset3.mat')
    metrics, exp_dir = run_experiment(problems, num_local_solve_tries, p_wc_extent)
    plotting.plot_percent_succ_vs_noise(metrics, os.path.join(exp_dir, "local_vs_iterative_bar.png"))

if __name__ == "__main__":
    main()


import cherrypy
import urllib
import json
import tumblpy

class OAuthApp:

    def read_config( self ):
        fh = open( "tumblr_config.json")
        tumblr_config = json.load( fh )
        fh.close()
        return tumblr_config

    @cherrypy.expose
    def index( self ):

        callback_url = cherrypy.url("oauth_callback")

        tumblr_config = self.read_config()

        t = tumblpy.Tumblpy( app_key = tumblr_config[ "consumer_key" ],
            app_secret = tumblr_config[ "consumer_secret" ],
            callback_url = callback_url )

        auth_props = t.get_authentication_tokens()
        auth_url = auth_props['auth_url']

        # get token for this session from auth_props
        oauth_token = auth_props['oauth_token']
        oauth_token_secret = auth_props['oauth_token_secret']

        # write token for this session to temp file
        fh = open( "tumblr_authsession_token", "w")
        out = "%s\n%s" % (oauth_token,oauth_token_secret)
        fh.write( out )
        fh.close()

        raise cherrypy.HTTPRedirect( auth_url )

    @cherrypy.expose
    def oauth_callback( self, oauth_token=None, oauth_verifier=None ):
        if oauth_verifier is not None:

            # read session token from temp file
            fh = open( "tumblr_authsession_token")
            token_lines = fh.readlines()
            # oauth_token = token_lines[0]
            oauth_token_secret = token_lines[1]
            fh.close()

            tumblr_config = self.read_config()

            t = tumblpy.Tumblpy( app_key = tumblr_config[ "consumer_key" ],
                app_secret = tumblr_config[ "consumer_secret" ],
                oauth_token=oauth_token,
                oauth_token_secret=oauth_token_secret )

            # get oauth token
            authorized_tokens = t.get_authorized_tokens(oauth_verifier)
            final_oauth_token = authorized_tokens['oauth_token']
            final_oauth_token_secret = authorized_tokens['oauth_token_secret']

            # read exisitng config json
            fh = open( "tumblr_config.json")
            tumblr_config = json.load( fh )
            fh.close()

            # add oauth token
            tumblr_config[ "oauth_token" ] = final_oauth_token
            tumblr_config[ "oauth_secret" ] = final_oauth_token_secret

            # write back out again
            fh = open( "tumblr_config.json", "w" )
            json.dump( tumblr_config, fh )
            fh.close()

        else:
            return "Auth callback didn't get a verifier :("

if __name__ == "__main__":
    cherrypy.config.update( {'server.socket_host':"0.0.0.0", 'server.socket_port':1413 } )
    cherrypy.quickstart( OAuthApp() )


#!/bin/bash python
import argparse
import os
import json
import time
import logging
import fitz

st = time.time()

def process_text(filename, filepath):
    # time.sleep(180)

    output_folder = "/home/flask/app/output_files"
    all_paths = []

    file_at = f"{output_folder}/{filename}"
    if not os.path.exists(file_at):
        return []

    doc = fitz.open(file_at)
    for i in range(len(doc)):
        for img in doc.getPageImageList(i):
            xref = img[0]
            pix = fitz.Pixmap(doc, xref)

            file_out = f"{output_folder}/{filename}_p%s-%s.png" % (i, xref)
            all_paths.append(f"output_files/{filename}_p%s-%s.png" % (i, xref))

            if pix.n < 5:       # this is GRAY or RGB
                pix.writePNG(file_out)
            else:               # CMYK: convert to RGB first
                pix1 = fitz.Pixmap(fitz.csRGB, pix)
                pix1.writePNG(file_out)
                pix1 = None
            pix = None

    return all_paths

def main(id="", uuid="", filename="", filepath=""):
    processed_body = process_text(filename, filepath)
    ajson = {}
    return processed_body

usage = """
    Minimal app

	Usage:

    # Use text buffer
    python3 main.py -i id_of_item -t "text buffer" -l
	"""

if __name__ == "__main__":
    # Handle arguments
    parser = argparse.ArgumentParser(
        description="Minimal app",
        epilog=usage,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("-i", help="Item id", type=str, default="1")
    parser.add_argument("-t", help="Text to be processed", type=str, default="")
    args = parser.parse_args()

    uuid = args.i
    body = args.t

    result = main(
        id="", uuid=uuid, filename=body, filepath=""
    )

    print(result)
    print("~~ finished in {0:.3f} sec ~~".format(time.time() - st))


import numpy as np
import pandas as pd
import tensorflow as tf
import os
from scipy.io import arff
from sklearn.preprocessing import MinMaxScaler

def load_data(path='/content/microarray-data/data/', shuffle=False):
  """
  Funkcja działa analogicznie do load_data zbioru MNIST.
  Zwraca (X_train, y_train), (X_test, y_test) o wymiarach:
  (900, 32, 32)
  (900,)
  (157, 32, 32)
  (157,)

  Dla potrzeb wizualizacji każdy gen przyjmuje wymiary kwadratu 32 x 32.
  """
  microarrays = []

  # Załadowanie danych
  for filename in os.listdir(path):
    full_path = os.path.join(path, filename)

    if os.path.isfile(full_path):
      if filename[-5:] == '.arff':
        microarray, _ = arff.loadarff(full_path)
        microarrays.append(microarray)

  # Konkatenacja danych z plików do jednej listy o długości 1057
  data = [*microarrays[0], *microarrays[1], *microarrays[2], *microarrays[3],
          *microarrays[4], *microarrays[5], *microarrays[6], *microarrays[7]]

  # 1057 pacjentów
  patients = len(data)
  # 1000 genów i 1 etykieta na pacjenta
  genes = len(data[0]) - 1

  # Zamiana listy data na macierze numpy
  X_flatten = np.zeros((patients, genes))
  X_reshaped = np.zeros((patients, 32, 32))
  y = np.zeros(patients)

  for i in range(patients):
    for j in range(genes):
      X_flatten[i][j] = data[i][j]

    # Normalizacja w przedziale <0, 1> w obrębie pojedynczego genu
    df = pd.DataFrame(X_flatten[i])
    x = df.select_dtypes(include=[np.number])
    scaler = MinMaxScaler(feature_range=(0, 1))
    x = scaler.fit_transform(x)

    # Dostawienie zer, aby długość genomu była równa 1024
    # I zmiana wymiaru x z (1000, 1) na 1000
    x = np.concatenate((np.reshape(x, 1000), np.zeros(24)))

    # Zmiana kształtu genu na 32 x 32
    X_reshaped[i] = np.reshape(x, (32, 32))

    # Zastąpienie etykiet wartościami logicznymi
    if str(data[i][1000]) == "b'yes'":
      y[i] = 1
    elif str(data[i][1000]) == "b'no'":
      y[i] = 0

  # Przetasowanie elementów
  if shuffle:
    indices = np.arange(patients)
    X = tf.gather(X_reshaped, indices)
    y = tf.gather(y, indices)
  else:
    X = X_reshaped

  # Podział na zbiór treningowy i testowy
  X_train = X[:900]
  X_test = X[-157:]
  y_train = y[:900]
  y_test = y[-157:]

  return (X_train, y_train), (X_test, y_test)

# Do użycia podczas trenowania GANów i sieci niewymagających etykiet
def load_data_by_label(label, path='/content/microarray-data/data/', shuffle=False):
  """
  Zwraca (X_train, X_test) o wymiarach:
  (n, 32, 32)
  (n, 32, 32)
  Gdzie n jest liczbą przypadków oznaczonych daną etykietą.
  """
  (X_train, y_train), (X_test, y_test) = load_data(path=path, shuffle=shuffle)

  assert X_train.shape[0] == y_train.shape[0]
  assert X_test.shape[0] == y_test.shape[0]
  assert label == 0 or label == 1

  X_pos_train = []
  for i in range(X_train.shape[0]):
    if y_train[i] == label:
      X_pos_train.append(X_train[i])

  X_pos_test = []
  for i in range(X_test.shape[0]):
    if y_test[i] == label:
      X_pos_test.append(X_test[i])

  X_pos_train = np.array(X_pos_train)
  X_pos_test = np.array(X_pos_test)

  return X_pos_train, X_pos_test

def convert_to_arff(images, labels, filename_r, filename_w, precision=3):
  """
  Konwersja obrazów z powrotem na plik .arff
  Funkcja przyjmuje argumenty:
  images o wymiarach (n, 32, 32)
  labels o wymiarach (n,)
  filename_r - nazwa pliku .arff, z którego pochodzą dane obrazów i etykiet
  filename_w - nazwa pliku .arff, do którego zostaną zapisane dane z obrazów np. po augmentacji
  precision - liczba cyfr po przecinku w danych, domyślnie 3

  n jest liczbą pacjentów (wierszy) we wczytanym pliku .arff !!
  """
  assert images.shape[1] == images.shape[2] == 32
  assert images.shape[0] == labels.shape[0]

  data_length = images.shape[0]
  genes = 1000

  file_r = open(filename_r, 'r')
  file_w = open(filename_w, 'w')

  # Przepisanie pierwszej linijki do nowego pliku
  lines = file_r.readlines()
  file_w.write(lines[0] + '\n')

  # Przepisanie artybutów
  for line in lines:
    if '@attribute' in line:
      file_w.write(line)

  file_w.write('\n@data \n')

  for i in range(data_length):
    img = np.array(images[i]).reshape(1024)[:1000]

    for j in range(genes):
      file_w.write(str(round(img[j], precision)) + ',')

    if labels[i] == 1:
      file_w.write("'yes' \n")
    elif labels[i] == 0:
      file_w.write("'no' \n")

  file_r.close()
  file_w.close()
  return file_w


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json

# ============CRNN OCR模型配置===========#
ocrType = 'chinese'
ocrCfgPath = 'weights/ocr/{}/ocr.cfg'.format(ocrType)
#ocrPath = 'weights/ocr/{}/ocr.weights'.format(ocrType)
ocrPath = 'weights/ocr/{}/ocr_best.pth'.format(ocrType)
charactersPath = 'weights/ocr/chinese/ocr.json'

def get_characters():
    with open(charactersPath, encoding='utf-8') as f:
        characters = json.loads(f.read())
        characters = ' '+characters+'-｜'

        return characters, len(characters)

# ============CTPN TEXT 模型配置===========#
textCfgPath = 'weights/text/text.cfg'
textPath = 'weights/text/text.pth'

TEXT_LINE_SCORE = 0.7  # text line prob
scale = 600
maxScale = 900

# ============CTPN TEXT训练参数===========#
IOU_NEGATIVE = 0.3
IOU_POSITIVE = 0.7
IOU_SELECT = 0.7

OHEM = True
RPN_POSITIVE_NUM = 150
RPN_TOTAL_NUM = 300


import argparse
import os
import sys
from datetime import datetime

import numpy as np
import torch
import yaml
from tqdm import tqdm

from models.mdrnn import MDRNN
from models.vae import VAE

# checking if the saved_models directory exists or create it
if not os.path.isdir("../saved_models"):
    os.makedirs("../saved_models")

# we want to use GPU if available
device = "cuda" if torch.cuda.is_available() else "cpu"
print("device used:", device)

# import data from the moving mnist dataset
data = np.load("../data/bouncing_mnist_test.npy")
data = data / 255  # normalize the data to be between 0 and 1

def count_parameters(net):
    # return the number of parameters of the model
    return sum(p.numel() for p in net.parameters() if p.requires_grad)

def main(params_path, model_path, model_vae_path):
    # we do not allow the training if there is not a training VAE model
    if model_vae_path is None:
        raise ValueError("no VAE model path given, please train a VAE model first.")
    else:
        print("loading VAE model saved in", model_vae_path)

        vae_params_path_recovery = model_vae_path[:-3] + ".params.yml"
        with open(vae_params_path_recovery) as file:
            vae_parameters = yaml.load(file, Loader=yaml.FullLoader)
        print("using VAE parameters from " + vae_params_path_recovery)

        vae_model = VAE(
            img_channels=vae_parameters["img_channels_vae"],
            latent_dim=vae_parameters["latent_dim_vae"],
            beta=vae_parameters["beta_vae"],
        )
        vae_model.load_state_dict(torch.load(model_vae_path))
        vae_model.to(device)
        print("number of parameters in vae :", count_parameters(vae_model))

    # now we create the MDRNN model
    if model_path is None:
        print("no model path given, training from scratch using default parameters.")
        with open(params_path) as file:
            parameters = yaml.load(file, Loader=yaml.FullLoader)
        model = MDRNN(
            latent_dim=parameters["latent_dim_mdrnn"],
            action_dim=parameters["action_dim_mdrnn"],
            hidden_dim=parameters["hidden_dim_mdrnn"],
            gaussians_nb=parameters["gaussians_nb_mdrnn"],
        )
    else:
        print("loading model saved in", model_path)
        try:
            params_path_recovery = model_path[:-3] + ".params.yml"
            with open(params_path_recovery) as file:
                parameters = yaml.load(file, Loader=yaml.FullLoader)
            print("using parameters from " + params_path_recovery)
        except:
            print("model parameters not found, trying to use default parameters.")
            with open(params_path) as file:
                parameters = yaml.load(file, Loader=yaml.FullLoader)
        model = MDRNN(
            latent_dim=parameters["latent_dim_mdrnn"],
            action_dim=parameters["action_dim_mdrnn"],
            hidden_dim=parameters["hidden_dim_mdrnn"],
            gaussians_nb=parameters["gaussians_nb_mdrnn"],
        )
        model.load_state_dict(torch.load(model_path))

    # even if we load a model, we are going to save a new version of it to not risk any overwriting
    model_name = "model_mdrnn_" + datetime.today().strftime("%Y-%m-%d")

    # we save the corresponding parameters
    with open(
        "../saved_models/vae-mdrnn/" + model_name + ".params.yml", "w"
    ) as outfile:
        yaml.dump(parameters, outfile, default_flow_style=False)

    model = model.to(device)
    batch_size = parameters["batch_size_mdrnn"]
    train_loader = torch.utils.data.DataLoader(
        dataset=data, batch_size=batch_size, shuffle=True
    )

    optimizer = torch.optim.Adam(
        model.parameters(), lr=parameters["learning_rate_mdrnn"]
    )
    print("number of parameters :", count_parameters(model))

    nb_epochs = parameters["nb_epochs_mdrnn"]
    for epoch in range(nb_epochs):
        pbar = tqdm(train_loader)
        pbar.set_description("epoch %s/ loss=? " % str(epoch + 1))
        for x in pbar:
            x = torch.reshape(x, (-1, 1, 64, 64)).float().to(device)
            latents = vae_model.encoder(x)[0]
            # x = torch.transpose(torch.reshape(x,(-1,20,64,64)),0,1).float().to(device)
            latents = torch.transpose(torch.reshape(latents, (-1, 20, 32)), 0, 1)
            seq_len, batch_size, _ = latents.shape

            if model.memory == "rnn":
                hidden = torch.zeros(batch_size, model.hidden_dim).to(device)
            else:
                hidden = (
                    torch.zeros(batch_size, model.hidden_dim).to(device),
                    torch.zeros(batch_size, model.hidden_dim).to(device),
                )
            action = torch.zeros(batch_size, model.action_dim).to(device)

            optimizer.zero_grad()
            episode_loss = []
            for t in range(seq_len - 1):
                latent = latents[t, :, :]
                latent_next_obs = latents[t + 1, :, :]

                if model.memory == "rnn":
                    mus, sigmas, logpi, next_hidden = model(action, latent, hidden)
                    hidden = next_hidden[1].squeeze(0)
                else:
                    mus, sigmas, logpi, next_hidden = model(action, latent, hidden)
                    hidden = next_hidden

                loss = model.loss_function(mus, sigmas, logpi, latent_next_obs)
                episode_loss.append(loss)

            episode_loss = torch.stack(episode_loss).sum()
            episode_loss.backward()
            optimizer.step()

            pbar.set_description(
                "epoch "
                + str(epoch + 1)
                + "/"
                + str(nb_epochs)
                + "  loss= "
                + str(loss.cpu().detach().numpy())
            )

        if (epoch + 1) % 10 == 0:
            torch.save(
                model.state_dict(), "../saved_models/vae-mdrnn/" + model_name + ".pt"
            )

    torch.save(model.state_dict(), "../saved_models/vae-mdrnn/" + model_name + ".pt")
    print(
        "end of training, model saved at",
        "saved_models/vae-mdrnn/" + model_name + ".pt",
    )

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Command line options")
    parser.add_argument(
        "--params_path", type=str, dest="params_path", default="default_parameters.yaml"
    )
    parser.add_argument("--model_path", type=str, dest="model_path", default=None)
    parser.add_argument(
        "--model_vae_path", type=str, dest="model_vae_path", default=None
    )
    args = parser.parse_args(sys.argv[1:])
    main(**{k: v for (k, v) in vars(args).items()})


# https://www.youtube.com/watch?v=-_A_AAxqzCg

import numpy as np
import scipy as sc

import matplotlib.pyplot as plt

# Declare function
func = lambda th: np.sin(1 / 2 *th[0] ** 2 - 1 / 4 * th[1] ** 2 + 3) * \
    np.cos(2 *th[0] + 1 - np.e ** th[1])

# Test function
val1 = func([5, 3])
#print(val1)

res = 20       # resolution of function

# Generate values
_X = np.linspace(-2, 2, res)
_Y = np.linspace(-2, 2, res)

# Empty array to hold result values
_Z = np.zeros((res, res))

# Calculate function and populate _Z
for ix, x in enumerate(_X):
    for iy, y in enumerate(_Y):
        _Z[iy, ix] = func([x, y])

#print(_Z)

plt.contourf(_X, _Y, _Z, res)
plt.colorbar()

# Random point on surface
theta = np.random.rand(2) * 4 - 2
plt.plot(theta[0], theta[1], "o", c="white")

_T = np.copy(theta)
h = .001        # diferential for derivative
lr = .01         # learning rate

grad = np.zeros(2)

for i in range(10000):
    for it, th in enumerate(theta):
        _T = np.copy(theta)
        _T[it] = _T[it] + h
        deriv = (func(_T) - func(theta)) / h
        grad[it] = deriv
    theta = theta - grad * lr
    if (i % 10) == 0:
        plt.plot(theta[0], theta[1], "o", c="red")

    print(func(theta))

plt.plot(theta[0], theta[1], "o", c="green")
plt.show()


# Monitor and update the price and triggers for open orders
import time
from libs.orderapi import Orderapi
from libs.configs import getConfig
import datetime
from zoneinfo import ZoneInfo

orderapi = Orderapi()

def action(data):
    oorders = orderapi.get_open_orders()
    obuyorders = [x for x in oorders if x['transaction_type']=='BUY']
    if(len(obuyorders)>0):
        print('There are open buy orders', len(obuyorders))
        now = datetime.datetime.now(tz=ZoneInfo('Asia/Kolkata'))
        expiry_min = getConfig('OPEN_ORDER_EXPIRY_MIN')
        candle_min = getConfig('OHLC_MIN')
        for order in obuyorders:
            order_time = order['order_timestamp']
            timepast = now.replace(tzinfo=None) - order_time
            if(timepast > datetime.timedelta(minutes=expiry_min)):
                print('Canceling expired open buy orders', expiry_min)
                orderapi.cancel_open_buy_orders([order])
            # elif(timepast > datetime.timedelta(minutes=round(expiry_min/2-1))):
            elif(timepast > datetime.timedelta(minutes=candle_min)):
                print('Changing open buy order triggers')
                price, trigger = orderapi.get_buy_sl_prices(order['tradingsymbol'], order['exchange'])
                print(order['tradingsymbol'], 'new price', f'{price}/{trigger}')
                orderapi.modify_sl_order(order['order_id'], price, trigger)
if(__name__ == '__main__'):
    print('Monitor order started')
    while(True):
        action(None)
        wait_min = getConfig('OHLC_MIN')
        time.sleep(round(wait_min*1)*60)


import gr1py.cli
from gr1py.solve import check_realizable
import pytest

ARBITER1_SPC_GR1C = """
ENV: r1;
SYS: g1;

ENVINIT: !r1;
ENVTRANS:
[](((r1 & !g1) | (!r1 & g1)) -> ((r1' & r1) | (!r1' & !r1)));
ENVGOAL:
[]<>!(r1 & g1);

SYSINIT: !g1;
SYSTRANS:
[](((r1 & g1) | (!r1 & !g1)) -> ((g1 & g1') | (!g1 & !g1')));
SYSGOAL:
[]<>((r1 & g1) | (!r1 & !g1));
"""

@pytest.mark.parametrize(
    'spcstr,expected',
    [(ARBITER1_SPC_GR1C, True),
     ('SYS:x;', True),
     ('SYS: x;\nSYSGOAL: []<>False;', False)])
def test_check_realizable(spcstr, expected):
    tsys, exprtab = gr1py.cli.loads(spcstr)
    r = check_realizable(tsys, exprtab)
    assert r == expected, (spcstr, r, expected)


from typing import List
import heapq

def solution(n: int, edge: List[int]) -> int:
    answer = 0
    arr = [[] for _ in range(n+1)]
    distance = [float('inf')]*(n+1)
    for e in edge:
        arr[e[0]].append((e[1], 1))
        arr[e[1]].append((e[0], 1))

    q = []
    heapq.heappush(q, (0, 1))
    distance[1] = 0
    while q:
        dist, now = heapq.heappop(q)

        if distance[now] < dist:
            continue

        for i in arr[now]:
            cost = dist + i[1]
            if cost < distance[i[0]]:
                distance[i[0]] = cost
                heapq.heappush(q, (cost, i[0]))

    max_num = 0
    for num in distance[1:]:
        if num > max_num:
            max_num = num
            answer = 1
        elif num == max_num:
            answer += 1
    return answer


from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("<str:page>", views.wiki_page, name="page"),
    path("create_page", views.create_page, name="newpage"),
    path("random", views.random_page, name="random"),
    path("search", views.search_page, name="search"),
    path("<str:page>/edit", views.edit_page, name="edit")
]


################################################################################
#
#   triangle.py
#   Code by: Casey Walker
#
################################################################################

import math
import line
import polygon

class Iso_triangle(polygon.Simple_polygon):
    #pos is the center of rotation, if left (0, 0) rotation is about center of the triangle
    #rotation describes the angle to rotate the triangle by on initialization (if left zero --> triangle's base is horizontal)
    #offset is the distance from pos to the symmetry axis and base of the triangle (as a tuple, (dx, dy))
    def __init__(self, pos, base, height, offset = (0, 0), radians = True, rotation = 0):
        self.pos = pos
        self.base = base
        self.height = height
        self.offset = offset
        self.radians = radians
        self.rotation = rotation if radians else math.radians(rotation)
        self.generate_angles()
        self.generate_widths()
        super().__init__(self.pos, self.angles, self.widths, self.radians, self.rotation)

    def generate_angles(self):
        self.angles = list()
        dx = self.offset[0] + self.base / 2
        dy = self.offset[1] - self.height / 2
        angle0 = math.atan2(dy, dx)
        dx = self.offset[0]
        dy = self.offset[1] + self.height / 2
        angle1 = math.atan2(dy, dx)
        dx = self.offset[0] - self.base / 2
        dy = self.offset[1] - self.height / 2
        angle2 = math.atan2(dy, dx)
        self.angles.extend([angle0, angle1, angle2])

    def generate_widths(self):
        self.widths = list()
        dx = self.offset[0] + self.base / 2 + self.pos[0]
        dy = self.offset[1] - self.height / 2 + self.pos[1]
        point = (dx, dy)
        self.widths.append(line.Line.point_to_point(self.pos, point))
        dx = self.offset[0] + self.pos[0]
        dy = self.offset[1] + self.height / 2 + self.pos[1]
        point = (dx, dy)
        self.widths.append(line.Line.point_to_point(self.pos, point))
        dx = self.offset[0] - self.base / 2 + self.pos[0]
        dy = self.offset[1] - self.height / 2 + self.pos[1]
        point = (dx, dy)
        self.widths.append(line.Line.point_to_point(self.pos, point))


import discord
from discord.ext import commands
import random
import sys

class Minesweeper:

	def __init__(self, bot):
		self.bot = bot
		self.settings = {'gridsize' : 8}

	def create_grid(self):
		self.grid = [[self.Tile() for x in range(self.settings['gridsize'])] for x in range(self.settings['gridsize'])]

	def populate_grid(self):
		for row in range(self.settings['gridsize']):
			for column in range(self.settings['gridsize']):
				has_mine = random.randint(1, self.settings['gridsize']*10)
				self.grid[row][column].row = row
				self.grid[row][column].column = column
				self.grid[row][column].nearby_mines = self.find_nearby_mines(row, column)
				if has_mine == 1:
					self.grid[row][column].state = 1
					self.minecount += 1
				else:
					self.grid[row][column].state = 0

	def calculate_numbers(self):
		pass

	@commands.command(brief='')
	async def ms(self, ctx):
		self.create_grid()
		self.populate_grid()
		await ctx.send(self.grid)

	class Tile:

		def __init__(self):

			self.row = None
			self.column = None

			self.state = None
			self.nearby_mines = None

def setup(bot):
	bot.add_cog(Minesweeper(bot))


"""A setuptools based setup module.
"""
import setuptools
# To use a consistent encoding
import codecs
import os

__author__ = 'Tomasz J. Kotarba <tomasz@kotarba.net>'
__copyright__ = 'Copyright (c) 2016, Tomasz J. Kotarba. All rights reserved.'
__maintainer__ = 'Tomasz J. Kotarba'
__email__ = 'tomasz@kotarba.net'

here = os.path.abspath(os.path.dirname(__file__))

install_requires = [
    'rapidpro-python>=2.1.6',
    'rapidsms>=0.18.0',
    'django>=1.7,<1.9'  # rapidsms incompatible with 1.9 at the time
]

tests_require = [
    'mock',
]

# Get the long description from the README.rst file
with codecs.open(os.path.join(here, 'README.rst'), encoding='utf-8') as f:
    long_description = f.read()

setuptools.setup(
    name='rapidpro-for-rapidsms',

    version='1.0.3',

    description='A RapidPro backend for RapidSMS.',
    long_description=long_description,

    # The project's main homepage.
    url='https://github.com/system7ltd/rapidpro-for-rapidsms',

    # Author details
    author='Tomasz J. Kotarba',
    author_email='tomasz@kotarba.net',

    license='BSD',

    classifiers=[
        'Development Status :: 5 - Production/Stable',

        'Intended Audience :: Developers',
        'Intended Audience :: System Administrators',
        'Intended Audience :: Telecommunications Industry',
        'Intended Audience :: Information Technology',

        'Topic :: Communications',
        'Topic :: Internet',
        'Topic :: Software Development :: Libraries',
        'Topic :: Software Development :: Libraries :: Application Frameworks',

        'Framework :: Django',

        'Operating System :: OS Independent',

        'License :: OSI Approved :: BSD License',

        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
    ],

    keywords='rapidsms rapidpro sms django textit',

    # packages=setuptools.find_packages(exclude=['tests']),
    packages=setuptools.find_packages(),

    install_requires=install_requires,

    tests_require=tests_require,

    test_suite='run_tests.main',

    extras_require={'development': tests_require},
)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Самостоятельно изучите работу со стандартным пакетом Python timeit . Оцените с помощью этого модуля скорость
# работы итеративной и рекурсивной версий функций factorial и fib . Во сколько раз измениться скорость работы
# рекурсивных версий функций factorial и fib при использовании декоратора lru_cache ? Приведите в отчет и обоснуйте
# полученные результаты

from functools import lru_cache
import timeit

def fib():
    def iterative(n):
        a, b = 0, 1

        while n > 0:
            a, b = b, a + b
            n -= 1

        return a

    @lru_cache
    def recursive(n):
        if n == 0 or n == 1:
            return n

        else:
            return recursive(n - 2) + recursive(n - 1)

    return {'iterative': iterative, 'recursive': recursive}

def factorial():
    def iterative(n):
        product = 1
        while n > 1:
            product *= n
            n -= 1

        return product

    @lru_cache
    def recursive(n):
        if n == 0:
            return 1

        elif n == 1:
            return 1

        else:
            return n * recursive(n - 1)

    return {'iterative': iterative, 'recursive': recursive}

def test(fun, end, step, start=0):
    arr = []
    for i in range(start, end, step):
        start_timer = timeit.default_timer()
        fun(i)
        arr.append(timeit.default_timer() - start_timer)

    return arr

def filter_float(list_timer, timer=1000000):
    s = []
    for item_timer in list_timer:
        s.append(float(f"{(item_timer * timer):0.2f}"))

    return s

if __name__ == '__main__':
    x = 10000
    lim_x = 20

    print("\nfib[iterative]: (microsecond)")
    print(filter_float(test(fib()['iterative'], x, int(x/lim_x))))

    print("\nfib[recursive]: (microsecond)")
    print(filter_float(test(fib()['recursive'], x, int(x/lim_x))))

    print("\nfactorial[iterative]: (microsecond)")
    print(filter_float(test(factorial()['iterative'], x, int(x/lim_x))))

    print("\nfactorial[recursive]: (microsecond)")
    print(filter_float(test(factorial()['recursive'], x, int(x/lim_x))))


import numpy as np
import matplotlib.pyplot as plt
import os
import utils
import seaborn as sns
import pickle

abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)

###--- change folder name accroding to experiment specications ---###
folder_name = 'gaussian_penalty0_maxlag_10'

#----- Check these parameters are in sync with main_non_modularized.py -----#
test = False

if test:
    sigma_range = np.arange(0.1, 2.0, 0.5)  # std of random gaussian noise
    K_range = [2]
else:
    sigma_range = np.arange(0.1,2.1,0.1) # std of random gaussian noise
    K_range = [2,3,4]

for round in range(1, 5):
    folder_name = f'pvCLCLreturns_maxshifts2_assumedmaxlag5_iter4_penalty0_{round}'
    # read saved results
    with open(f'../results/performance/{round}.pkl', 'rb') as f:
            performance = pickle.load(f)

    with open(f'../results/signal_estimates/{round}.pkl', 'rb') as f:
        estimates = pickle.load(f)

    #=================== plot the results ==================#
    # labels and formats
    labels = {'pairwise': 'SPC-Pairwise',
              'sync': 'SPC-Sync',
                'spc-homo': 'SPC-IVF',
                'spc': 'SPC',
                'het': 'IVF',
                'het reassigned': 'IVF (regroup)',
                'true': 'True'}
    color_labels = labels.keys()
    col_values = sns.color_palette('Set2')
    color_map = dict(zip(color_labels, col_values))

    lty_map = {'sync': 'dotted',
                'spc-homo': 'dashdot',
                'het': 'dashed',
                'true': 'solid'}

    #--- plots of performance of different methods for lag estimatiopn ---#
    results_save_dir = utils.save_to_folder('../plots/SPC_cluster', folder_name)

    for k in K_range:
        fig, axes = plt.subplots(4,1, figsize = (15,24), squeeze=False)
        ax = axes.flatten()
        plot_list = ['ARI',  'accuracy','error_sign']

        for i in range(len(plot_list)):
            for key, result_list in performance[f'K={k}'][plot_list[i]].items():
                ax[i].plot(sigma_range, result_list, label = labels[key], color = color_map[key])
            ax[i].grid()
            ax[i].legend()
            ax[i].set_xlabel('std of added noise')

        quantile = (0.05,0.95)
        for key, errors_quantile in performance[f'K={k}']['errors_quantile'].items():
            mean = [x[int(0.5/0.05)] for x in errors_quantile]

            quantile_l = [x[int(quantile[0]/0.05)] for x in errors_quantile]
            quantile_u = [x[int(quantile[1]/0.05)] for x in errors_quantile]
            ax[3].plot(sigma_range, mean, label = labels[key], color = color_map[key])
            ax[3].fill_between(sigma_range,quantile_u,quantile_l,color = color_map[key], alpha=0.5, label = f'{labels[key]}: {quantile[0]:.0%} to {quantile[1]:.0%}')
            ax[3].grid(visible = True)
            ax[3].legend()
            ax[3].set_xlabel('std of added noise')

        ax[0].set_title(f'Ajusted Rand Index of Clustering, K = {k}')
        ax[1].set_title(f'Lag Prediction Accuracy %')
        ax[2].set_title(f'Average Direction Prediction Error')
        ax[3].set_title(f'Average Lag Prediction Error (shaded area is the {quantile[0]:.0%} to {quantile[1]:.0%} percentile of errors)')

        plt.savefig(results_save_dir + f'/acc_err_ARI_K={k}')

    #--- plots of signal estimates of different methods ---#
    results_save_dir_2 = results_save_dir + '/signal_estimates'
    os.makedirs(results_save_dir_2)

    # align the estimated signals to ground truth signals
    for k in K_range:
        os.makedirs(results_save_dir_2 + f'/K={k}')
        i = 0
        for sigma in sigma_range:
            # # calculate the estimated mixing probabilities

            # for key, classes in classes_estimates[f'K={k}'][f'sigma={sigma:.2g}'].items():
            #     p_est[key] = np.zeros(k)
            #     for c in classes.unique():
            #         p_est[key][c] = np.mean(classes==c)
            estimates_i = estimates[f'K={k}'][f'sigma={sigma:.2g}']
            fig, ax = plt.subplots(k, 1, figsize = (10,5*k))
            X_true =estimates_i['signals']['true']
            # for key, X_estimates in estimates_i['signals'].items():
            #     if key != 'true':
            #         X_estimates, perm = utils.align_to_ref_het(X_estimates, X_true)
            #         signal_estimates[f'K={k}'][f'sigma={sigma:.2g}'][key] = X_estimates
            #         rel_errors = {}
            #         rel_errors_str = ''
            #         p_est_str = ''
            for j in range(k):
                rel_errors_str = []
                p_est_str = []
                for key, X_estimates in estimates_i['signals'].items():
                    ax[j].plot(X_estimates[:,j],
                                label = labels[key],
                                color = color_map[key],
                                linestyle = lty_map[key])
                    if key != 'true':
                        rel_err = np.linalg.norm(X_estimates[:,j]-X_true[:,j])/np.linalg.norm(X_true[:,j])
                        rel_errors_str.append( f'{labels[key]} {rel_err:.3f}')

                for key_p, value_p in estimates_i['probabilities'].items():
                    p_est_str.append(f'{labels[key_p]} {value_p[j]:.3g}')

                title = 'rel. err.: ' + ';'.join(rel_errors_str) + '\nmix. prob.: ' + '; '.join(p_est_str)
                ax[j].set_title(title)
                ax[j].grid()
                ax[j].legend()

            fig.suptitle(f'Comparison of the True and Estimated Signals, K = {k}, noise = {sigma:.2g}')
            plt.savefig(results_save_dir_2 + f'/K={k}/{int(i)}')
            plt.close()
            i+=1

def extract_quantile(q,values):
    """
    Extract quantile from a list of values of percentiles at 0, 5, 10, ..., 100.
    Args:
        q: quantiles to extract, valid inputs are [0, 0.05, 0.1,..., 1]
        values: ndarray of length 21. quantiles values at 0.05 interval

    Returns:

    """
    ind = int(q/0.05)
    return values[ind]


import logging
from typing import Tuple
import torch

EPS = 1e-16
METHODS = ("component_max", "overall_max", "jet_E")

def normalize_p4(
    p4: torch.Tensor, method: str = "overall_max"
) -> Tuple[torch.Tensor, torch.Tensor]:
    if method.lower().replace(" ", "_").replace("-", "_") == "component_max":
        return normalize_p4_component_max(p4)
    elif method.lower().replace(" ", "_").replace("-", "_") == "overall_max":
        return normalize_p4_overall_max(p4)
    elif method.lower().replace(" ", "_").replace("-", "_") == "jet_E".lower():
        return normalize_p4_jet_E(p4)
    else:
        logging.warning(
            f"Normalization method {method} not recognized. Using component_max."
        )
        return normalize_p4_overall_max(p4)

def normalize_p4_component_max(p4: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
    """Normalization using :math:`p^\mu \to p^\mu \max_{p \in J} (|p^\mu|)`.
    That is, we normalize each component by the component-wise maximum of the 4-momenta within the jet.

    :param p4: 4-momentum tensor of shape (num_jets, num_particles, 4).
    :type p4: torch.Tensor
    :return: (p4_norm, factor)
    :rtype: Tuple[torch.Tensor, torch.Tensor]
    """
    normalize_p4_factor = torch.abs(p4).amax(dim=-2, keepdim=True) + EPS
    p4 = p4 / normalize_p4_factor
    return p4, normalize_p4_factor

def normalize_p4_overall_max(p4: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
    """Normalization using :math:`p \to p / \max_{\mu \in \{0,1,2,3\}} \max_{p \in J} (|p^\mu|)`.
    That is, we divide each entry of the 4-momentum by the overall maximum of the 4-momenta within the jet.

    :param p4: 4-momentum tensor of shape (num_jets, num_particles, 4).
    :type p4: torch.Tensor
    :return: (p4_norm, factor)
    :rtype: Tuple[torch.Tensor, torch.Tensor]
    """
    normalize_p4_factor = (
        torch.abs(p4).amax(dim=-1, keepdim=True).amax(dim=-2, keepdim=True) + EPS
    )
    p4 = p4 / normalize_p4_factor
    return p4, normalize_p4_factor

def normalize_p4_jet_E(p4: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
    """Normalization using the jet energy: :math:`p \to p / (\sum_{p \in J} p^0)`.

    :param p4: 4-momentum tensor of shape (num_jets, num_particles, 4).
    :type p4: torch.Tensor
    :return: (p4_norm, factor)
    :rtype: Tuple[torch.Tensor, torch.Tensor]
    """
    jet_p = p4.sum(dim=-2, keepdim=True)
    normalize_p4_factor = jet_p[..., 0].unsqueeze(-1).unsqueeze(-1) + EPS
    p4 = p4 / normalize_p4_factor
    return p4, normalize_p4_factor


import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as mtick
import pandas

data = pandas.read_csv('eff_test1.csv', sep=',')

# Data for plotting
Vin = data["Vin"]
Iin = data["Iin"]
Vout = data["Vout"]
Iout = data["Iout"]

Eff = (Vout*Iout)/(Vin*Iin)

fig, ax = plt.subplots()
ax.plot(Iout[1:], Eff[1:], linestyle='-', color="tab:blue", alpha=0.9, linewidth=3.0)
#ax.tight_layout()
ax.yaxis.set_major_formatter(mtick.PercentFormatter(1.0, 1))
#ax.set_ylabel('$\eta$',rotation=0, fontsize=18, labelpad=10)
ax.set_xlabel(r'Output Current (A)', fontsize=18)
plt.title('Open PMIC\nEfficiency vs Output Current (3.3V in, 1.8V out)', fontsize=14)
ax.grid(alpha=0.15)
plt.ylim([0.6, 0.95])
plt.xlim([0.0, 0.5])
fig.align_ylabels()
fig.tight_layout()

fig.savefig("eff1.png", dpi=300)
plt.show()


from django.shortcuts import render
from django.db.models import Count,Sum,Min, Max
from django.shortcuts import redirect
from . import models
from . import forms
import random

# get question with least answers from given user
def question_queue(batch,user_name):
    print(batch)
    maximum_annotations_per_user_batch = 0.7
    maximum_annotations_per_question = 3
    # find first and last id of question
    q = models.Question.objects.filter(batch_id=batch.id).aggregate(min_id=Min("id"),max_id=Max("id"))
    print(q)
    max_id = q["max_id"]
    min_id = q["min_id"]

    num_annotations = 0
    r = models.Annotation.objects.filter(username=user_name,question__batch=batch).annotate(count_id=Count("id"))
    if len(r) > 0:
        num_annotations = r[0].count_id

    # Batch exhausted for user, no more annotations
    if ((max_id - min_id) * maximum_annotations_per_user_batch) <= num_annotations:
        print(max_id,min_id)
        print(num_annotations)
        print("BATCH FILLLE")
        return None

    # find id of last annotated question
    qid = None
    last_annotations = models.Annotation.objects.filter(username=user_name,question__batch=batch).order_by("-created_at")
    if len(last_annotations) == 0:
        # start with random question
        qid = random.randint(min_id,max_id + 1)
    else:
        # get next_question
        last_question = last_annotations[0].question
        qid = last_question.id
    next_question = None
    for i in range(max_id -min_id - num_annotations):
        qid += 1
        if qid > max_id:
            qid = min_id
        # verify qid
        next_question = models.Question.objects.filter(id=qid)
        if len(next_question) == 0:
            continue
        next_question = next_question[0]
        anots = next_question.annotation_set.all()
        if len(anots) > maximum_annotations_per_question:
            next_question = None
            continue
        for a in anots:
             if user_name == a.username:
                next_question = None
                continue

    return next_question

def get_database(batch):
    questions = models.Question.objects.filter(batch_id=batch.id).annotate(num_annotation=Count("annotation")).order_by("num_annotation")
    for q in questions:
        doc = {
            "id": q.id,
            "text": q.data,
            "meta": json.loads(q.meta),
            "batch": batch,
        }
        answers = []
        for a in q.annotation_set.all():
            an = {
                "id": a.id,
                "hate_level": a.hate_level,
                "hate_cathegory": a.hate_cathegory,
                "user_name": a.username,
                "answer": a.answer,
                "created_at": a.created_at
            }
            answers.append(an)
        doc["annotations"] = answers
        json.dumps(doc)

def question(request):
    userid = "anonymous"
    if "userid" in request.GET:
        userid = request.GET["userid"]
    # get answer from form
    form = forms.AnswerForm(request.POST)
    answer = None
    if "accept" in request.POST:
        answer = models.Answers.ACCEPT
    elif "reject" in request.POST:
        answer = models.Answers.REJECT
    if answer is not None and form.is_valid():
        hate_level = form.cleaned_data.get("hate_level")
        hate_cathegory = form.cleaned_data.get("hate_cathegory")
        question_id = request.POST["question_id"]
        if answer is not None:
            annotation = models.Annotation(username=userid,question_id=question_id,answer=answer,hate_level=hate_level,hate_cathegory=hate_cathegory)
            annotation.save()
    else:
        print("form invalid")
    form = forms.AnswerForm()
    # get active batch
    batches = models.Batch.objects.filter(status=models.Status.ANNOTATION)
    batch = None
    question = None
    if len(batches) == 0:
        pass
    # TODO no active batch
    else:
        # TODO select batch, batch priority
        batch = batches[0]
        question = question_queue(batch,userid)
        if question is None:
            # batch empty, nothing to do
            pass
    # previous questions

    # annotations in all batches
    prev_questions = models.Batch.objects.raw("SELECT harvest_batch.*,harvest_question.batch_id , count(harvest_annotation.id) as annotation_count FROM harvest_question LEFT JOIN harvest_annotation LEFT JOIN harvest_batch WHERE harvest_annotation.question_id = harvest_question.id AND harvest_annotation.username=%s AND harvest_batch.id = harvest_question.batch_id",[userid])
    #prev_questions = models.Batch.objects.annotate(Count("question__annotation")).filter(question__annotation__username=userid)
    #prev_questions = models.Question.objects.filter(annotation__username=userid).count()
    out = {
        "batch":batch,
        "question":question,
        "form":form,
        "userid": userid,
        "prev_questions": prev_questions,
    }
    return render(request,"harvest/question.html",out)

def start(request):
    form = forms.StartForm(request.POST)
    if form.is_valid():
        userid = form.cleaned_data.get("username")
        return redirect("/harvest/question?userid=" + userid)
    out = {
        "form":form
    }
    return render(request,"harvest/start.html",out)

def users(request):
    users = models.Annotation.objects.raw("SELECT DISTINCT username,1 as id FROM harvest_annotation")
    res = []
    for u in users:
        print(u)
        userid = u.username
        print(userid)
        prev_questions = models.Batch.objects.raw("SELECT harvest_batch.name,harvest_question.batch_id as id, count(harvest_annotation.id) as annotation_count FROM harvest_question LEFT JOIN harvest_annotation LEFT JOIN harvest_batch WHERE harvest_annotation.question_id = harvest_question.id AND harvest_annotation.username=%s AND harvest_batch.id = harvest_question.batch_id",[userid])
        res.append({"username":userid,"items":prev_questions})
    out = {
        "users":res
    }
    return render(request,"harvest/users.html",out)

def batch(request):
    batch_id = request.GET["batchid"]
    batch = models.Batch.objects.get(id=batch_id)
    users = models.Annotation.objects.raw("SELECT DISTINCT harvest_annotation.username,1 as id FROM harvest_annotation LEFT JOIN harvest_question WHERE harvest_annotation.question_id = harvest_question.id AND harvest_question.batch_id=%s",[batch_id])
    res = []
    for user in users:
        userid = user.username
        prev_questions = models.Question.objects.raw("SELECT count(harvest_annotation.id) as annotation_count FROM harvest_annotation LEFT JOIN harvest_question WHERE harvest_annotation.username=%s and harvest_question.batch_id=%s",[userid,batch_id])
        # TODO - are there too many rejects?
        # count only accepts...
        res.append({
            "username":userid,
            "count":prev_questions[0].annotation_count
        })
    out = {
        "batch":batch,
        "users": res
    }

def index(request):

    batches = models.Batch.objects.raw("SELECT harvest_batch.name,harvest_batch.status,harvest_batch.description,harvest_question.batch_id as id, count(harvest_annotation.id) as annotation_count FROM harvest_question LEFT JOIN harvest_annotation LEFT JOIN harvest_batch WHERE harvest_question.id = harvest_annotation.question_id AND harvest_question.batch_id = harvest_batch.id")
       #batches = models.Batch.objects.select_related("question").aggregate(Count("question__annotation"))
    for b in batches:
        print(b)
    out = {
        "batches": batches
    }

    return render(request,"harvest/index.html",out)
# Create your views here.


import requests
from bs4 import BeautifulSoup
import inspect
from urllib.request import urlopen
from urllib.request import Request
import SendingEmail
# import schedule
import time
import traceback
import datetime

class Information:
    filter_text = []

    def __init__(self, hypertitle, title, organization, date):
        self.hypertitle = hypertitle
        self.title = title
        self.organization = organization
        self.date = date

    # def load(self, hypertitle, title):

class Crawler:
    text = ''

    def __init__(self):
        pass

    def getData1(self): #데이터 가져오는것.
        print ("한양대학교 컴퓨터소프트웨어 학부")
        req = Request("http://cs.hanyang.ac.kr/board/info_board.php?ptype=&page=1&code=notice")# urllib.request 데이터를 보낼 때 인코딩하여 바이너리 형태로 보낸다 없는 페이지를 요청해도 에러를 띄운다
        req.add_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
        req.add_header("Accept-Language", "ko-KR,ko;")

        data = ''
        try:
            response = urlopen(req, None, 60) # url : 열고자 하는 URL 문자열, request 클래스의 인스턴스가 포함. 아마 html을 return함.

            html = response.read()
            soup = BeautifulSoup(html, 'html.parser')
            data_date = soup.select('#content_box > div > table > tbody > tr > td:nth-child(5)')
            num = soup.select('#content_box > div > table > tbody > tr > td:nth-child(2)')
            # print(num[0].get_text().strip())
            data = soup.select('.left')
            #head태그의 link에 접근해 한양대학교 컴퓨터소프트웨어학부의 처음 url을 가져옴
            headInlink = soup.find('head').find('link')
            link = headInlink['href']

            start_idx = 0
            for idx,i in enumerate(num):
                # print(i.get_text().strip(), idx)
                if i.get_text().strip() != '[공지]':
                    start_idx = idx
                    break

            title = []
            for i in range(start_idx, len(data) ):
                title.append(data[i].get_text().strip() )

            # 하이퍼링크 저장
            hyperTitle = []
            for i in range(start_idx, len(data)):
                if data[i].find('a')['href'][:4] == "http":
                    hyperTitle.append(data[i].find('a')['href'].strip())
                else:
                    hyperTitle.append( (link + data[i].find('a')['href']).strip() )

            #date 저장
            date = []
            for i in range(start_idx, len(data_date)):
                list = (data_date[i].get_text().strip()).split('.')
                d = datetime.datetime( int( '20'+list[0]) , int(list[1]) , int(list[2]) )

                date.append( d )
            print(title)
            info = Information(hyperTitle, title, 1, date)

        except Exception as e:
            print('Exception!', e)
        #   raise CrawlerException(e)

        return info #string형식으로 반환.

    def getData2(self): #데이터 가져오는것.
        print ("한양대학교 소프트웨어중심대학")
        req = Request("http://hysoft.hanyang.ac.kr/modules/board/bd_list.html?id=wt_notice&or=bd_order&al=asc&p=1")# urllib.request 데이터를 보낼 때 인코딩하여 바이너리 형태로 보낸다 없는 페이지를 요청해도 에러를 띄운다
        req.add_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
        req.add_header("Accept-Language", "ko-KR,ko;")

        data = ''

        try:
            response = urlopen(req, None, 60) # url : 열고자 하는 URL 문자열, request 클래스의 인스턴스가 포함. 아마 html을 return함.

            html = response.read()
            # print(html)
            soup = BeautifulSoup(html, 'html.parser')
            data = soup.select('.alLeft')
            data_date = soup.select('#subContainer > div.m00.m11 > div > div.con > form:nth-child(2) > table > tbody > tr > td:nth-child(4)')

            #head태그의 link에 접근해 한양대학교 컴퓨터소프트웨어학부의 처음 url을 가져옴
            headInlink = soup.find('head').find('link')
            link = headInlink['href'] + 'modules/board/'
            print(link)

            title = []
            for i in range(0, len(data) ):
                title.append(data[i].get_text().strip())

            # print(data[i].find('a')['href'])

            hyperTitle = []
            for i in range(0, len(data)):
                hyperTitle.append((link + data[i].find('a')['href']).strip())

            date = []
            for i in range(0, len(data_date)):
                list = (data_date[i].get_text().strip()).split('.')

                d = datetime.datetime( int( list[0]) , int(list[1]) , int(list[2]) )
                date.append( d )
            print(title)
            info = Information(hyperTitle, title, 2, date)

        except Exception as e:
            print('Exception!', e)

        return info #string형식으로 반환.

    def getData3(self): #데이터 가져오는것.
        print ("한양대학교 공과대학교")
        req = Request("http://eng.hanyang.ac.kr/people/notice.php?ptype=&page=1&code=notice")# urllib.request 데이터를 보낼 때 인코딩하여 바이너리 형태로 보낸다 없는 페이지를 요청해도 에러를 띄운다
        req.add_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
        req.add_header('Accept-Language', 'ko;q=0.8')
        req.add_header('Accept-Charset', 'utf-8;q=0.7,*;q=0.3')
        # req.add_header("Accept-Language", "ko-KR,ko;")
        data = ''
        try:
            response = urlopen(req, None, 60) # url : 열고자 하는 URL 문자열, request 클래스의 인스턴스가 포함. 아마 html을 return함.
            html = response.read()
            soup = BeautifulSoup(html, 'html.parser') #여기서 오류발생.

            headInlink = soup.find('head').find('link')
            link = headInlink['href']

            title = []
            hyperTitle = []
            date = []
            data = soup.select('tbody > tr')
            for tr in data:
                tds = tr.select('td')
                notidate = tds[3].text.split('.')
                hyperTitle.append((link + tds[1].select('a')[0]['href']).strip())
                title.append(tds[1].select('a')[0].text)
                date.append( datetime.datetime(int(notidate[0]), int(notidate[1]), int(notidate[2])) )
                # print(tds[1].select('a')[0]['href'])
                # print(tds[1].select('a')[0].text)
                # print(tds[3].text)
            print(title)
            info = Information(hyperTitle, title, 3, date)

        except Exception as e:
            print('Exception!', e)
        #   raise CrawlerException(e)

        return info #string형식으로 반환.

# Information class에 keyword를 저장해놓기
# save_str = ''
# for item in response['Items']:
#     for j in item['name']:
#         save_str += j
#     # print(save_str)
#     Information.filter_text.append(save_str)
#     save_str = ''

#filtering한것
# for i in range(0, len(info)):
#     for j in range(0, len(info[i].title)):
#         for text in Information.filter_text:
#             if text in info[i].title[j]:
#                 name = 'Bob'
#                 title =  info[i].title[j]
#                 res = f'{text} : {title}'
#                 print(res)
#                 print()

# e = SendingEmail.Email()


import numpy as np
import pandas as pd

import matplotlib.pyplot as pyt

STRATEGIES = [
     'LCE-LFU',             # Leave Copy Everywhere
     'LCE-LRU',             # Leave Copy Everywhere
     # # 'NO_CACHE',        # No caching, shorest-path routing
     # # 'HR_SYMM',         # Symmetric hash-routing
     # # 'HR_ASYMM',        # Asymmetric hash-routing
     # # 'HR_MULTICAST',    # Multicast hash-routing
     # # 'HR_HYBRID_AM',    # Hybrid Asymm-Multicast hash-routing
     # # 'HR_HYBRID_SM',    # Hybrid Symm-Multicast hash-routing
     # # 'CL4M',            # Cache less for more
     # 'PROB_CACHE',      # ProbCache
     'WLRU',
     'WLFU',
     'GRD',
	 # 'PD',
             ]

SCEN = {
	'alpha': 'Zipf Exponent Alpha',
	'cache_ratio': 'Cache to Population Ratio (%)',
	# 'std': 'Change Stardard Derivation',
}

STYLE = {
		 'WLRU':         'b-o',
		 'WLFU':        'g-D',
		 'GRD':    'm--^',
		 'PD':    'c-s',
		 'LCE-LRU':    'r--v',
		 'LCE-LFU':  'k--*',
		 'PROB_CACHE':  'y--X',
}

STRATEGY_LABEL = {
		 'WLRU':         'U-LRU',
		 'WLFU':        'U-LFU',
		 'GRD':    'GRD',
		 'PD':    'Offline',
		 'LCE-LRU':    'LCE-LRU',
		 'LCE-LFU':  'LCE-LFU',
		 'PROB_CACHE':  'ProbCache',
}

TOPO = [
	'GEANT',
	'TISCALI',
]

# print results
data = pd.read_csv('multi_data.csv')

fig = pyt.figure(figsize=(16, 3))
pyt.subplots_adjust(wspace=0.3)

for j, metric in enumerate(SCEN.keys()):
	fixed_metric = SCEN.keys()[1 - j]
	fixed_value = data.groupby(fixed_metric).size().argmax()
	for i, topo in enumerate(TOPO):
		ax = pyt.subplot(1,len(SCEN)+len(TOPO),i*len(TOPO)+j+1)
		res = data.loc[(data[fixed_metric] == fixed_value) & (data['topology']==topo)].sort_values(by=metric)
		# print res
		x = res[metric]
		for method in STRATEGIES:
			y = []
			y_up = []
			y_down = []
			for x_value in x:
				this_strategy = res.loc[(res[metric]==x_value) & (res['strategy']==method)][['round', 'weight']]
				nocache = res.loc[(res[metric]==x_value) & (res['strategy']=='NOCACHE')][['round', 'weight']]\
					.rename(columns={'weight':'nocache'})
				screen_out = pd.merge(this_strategy, nocache,on='round')
				screen_out = screen_out['nocache']-screen_out['weight']
				# print np.mean(screen_out)
				y.append(np.mean(screen_out))
				# y_up.append(np.percentile(screen_out,95)-y[-1])
				# y_down.append(y[-1]-np.percentile(screen_out,5))
			pyt.plot(x*100 if metric == 'cache_ratio' else x, y, STYLE[method], label=STRATEGY_LABEL[method])
			# pyt.errorbar(x, y, yerr=[y_down,y_up], fmt=STYLE[method], label=STRATEGY_LABEL[method])
		pyt.xlabel(SCEN[metric])
		pyt.ylabel("Caching Gain")
		pyt.title("(%c) Topology=%s" % (chr(ord('a')+i*len(TOPO)+j), topo))
		if j + i == 0:
			pyt.legend(loc="upper left", ncol=5, bbox_to_anchor=(-0.2,1.2, 5, 0.05), mode='expand')
# pyt.show()
pyt.savefig("multi.pdf", bbox_inches='tight')
pyt.savefig("multi.png", bbox_inches='tight')
pyt.close()