from pathlib import Path
import pandas as pd
from zipfile import ZipFile
import streamlit as st
import urllib.response
import altair as alt
from sklearn.feature_selection import r_regression
import numpy as np

def line_break():
    st.text("\n")
    st.text("\n")
    st.text("\n")
    st.text("\n")
    st.text("\n")
    st.text("\n")
    st.text("\n")

    return

def year_range(start_year:int, end_year_exclusive:int):
    return [*range(start_year, end_year_exclusive)]

st.set_page_config(layout="wide")

st.sidebar.title("Sobre")
st.sidebar.info(
    """

    **Visualização Computacional** - SCC0252 - Trabalho de Recuperação

    **Autor**: Heitor Carvalho Pinheiro\n
    **Docente**: Maria Cristina Ferreira de Oliveira

    """
)

st.sidebar.title("Contato")
st.sidebar.info(
    """
    E-mail: heitor.c.pinheiro@usp.br
    [LinkedIn](https://www.linkedin.com/in/heitor-carvalho-pinheiro-4b58851b7/) | [Github](https://github.com/Heitorcp)
    """
)

st.write("# Emissões Globais de CO2 :factory:")

#filtering the years
start_year, end_year = st.select_slider(
    ":date: **Selecione os anos que deseja observar**",
    options = [*range(1750, 2023)], value = (1900, 2022))

def pivot_df(df, index:str, columns:str, values:str):

    pivot_df = pd.pivot(df, index=index, columns=columns, values=values)
    return pivot_df

@st.cache
def read_CO2_data():
    try:
        csv_path = Path("data/GCB2022v27_MtCO2_flat.csv")
    except:
        if not csv_path.is_file():
            Path("data").mkdir(parents=True, exist_ok=True)
            with ZipFile("data/archive.zip", 'r') as zObject:
                zObject.extractall(path="data")
    return pd.read_csv(Path("data/GCB2022v27_MtCO2_flat.csv"))

@st.cache
def data_with_coords():
    try:
        data = read_CO2_data().iloc[:, [0] + [*range(2,11)]]
        coords = pd.read_csv("./data/country-coord.csv", usecols=[0,3,4,5], dtype={'Numeric code':str})
        coords = coords.rename(columns={"Numeric code":"id"})

        df_with_coords = data.join(coords.set_index('Country'), on='Country')
        #filter years
        df_with_coords = df_with_coords.set_index('Year').loc[year_range(2000,2022)].reset_index()

    except BaseException as e:
        raise e

    return df_with_coords

def data_with_coords_r2_score():
    try:
        years = [*range(2000,2022)]
        data = data_with_coords().iloc[:,[0,1,2,10,11,12]]
        data = data.set_index("Year").loc[years].reset_index()
        data_groupped = data.groupby("Country")

        r2_dict = {}

        countries = data.Country.unique()

        for country in countries:

            df = data_groupped.get_group(country)

            X = df.iloc[:, [0]]
            y = df.iloc[:, 2]
            r2_score = r_regression(X,y)[0]
            r2_score = r2_score.round(2)

            r2_dict[country] = r2_score

        df_final = pd.DataFrame.from_dict(r2_dict, orient="index", columns=["r2_score"]).reset_index()
        df_final.columns = ["Country", "r2_score"]

        conditions = [

        df_final['r2_score'] < -0.8,
        (df_final['r2_score'] < -0.6) & (df_final['r2_score'] >= -0.8),
        (df_final['r2_score'] <= 0.6) & (df_final['r2_score'] >= 0.),
        (df_final['r2_score'] <= 0.)  & (df_final['r2_score'] >= -0.6),
        (df_final['r2_score'] <= 0.8) & (df_final['r2_score'] >= 0.6),
        df_final['r2_score'] > 0.8

                    ]

        choices = ["Group A", "Group B", "Group C", "Group C", "Group D", "Group E"]

        df_final["Group"] = np.select(conditions, choices)

        #merging coords with groups
        data = data.iloc[:, [1,3,4,5]]

        df_groups = pd.merge(data, df_final, on="Country")
        df_groups.drop_duplicates(inplace=True)

    except BaseException as e:
        raise e

    return df_groups

print(data_with_coords_r2_score())

def emission_per_category():

    df_per_category = read_CO2_data()[read_CO2_data().Country != "Global"]
    df_per_category = df_per_category.groupby(["Year","Coal","Oil","Gas","Cement","Flaring","Other"]).sum()
    df_per_category = df_per_category.reset_index()
    df_per_category = df_per_category.iloc[:,[0,1,2,3,4,5,6]]
    df_per_category = df_per_category.set_index("Year").loc[years]
    df_per_category = df_per_category.reset_index().melt(id_vars=["Year"], var_name = "Category", value_name="Total")
    df_per_category = df_per_category.astype({"Year":"string","Category":"string", "Total":"float64"})
    df_per_category = df_per_category.groupby(["Year","Category"])["Total"].sum().reset_index()

    return df_per_category

@st.cache
def rank_by_column(column_to_rank_by:str = ["Total", "Oil", "Coal", "Gas", "Flaring", "Cement", "Other", "Per Capita"]) -> pd.DataFrame():

    years = year_range(2000, 2022)

    df = read_CO2_data().loc[(read_CO2_data().Year.isin(years)) & (read_CO2_data().Country != "Global")]
    column_ranked = df[[column_to_rank_by, "Year", "Country"]].sort_values(by=["Year",column_to_rank_by], ascending=(True, False))

    ranks = column_ranked.groupby("Year")[column_to_rank_by].rank(method='first', ascending=False).reset_index()
    ranks.rename(columns={column_to_rank_by:"Rank"}, inplace=True)

    #joining on index
    ranked_df = column_ranked.join(ranks.set_index('index'), on=column_ranked.index)
    return ranked_df

try:
    df_raw = read_CO2_data()
    df = df_raw.iloc[:,[0,2,3]]
    df = df[df.Year.isin(range(start_year, end_year))]
    # print(df)

    countries = st.multiselect(
        ":earth_americas: **Selecione os países**", list(df.Country.unique()), ["USA", "China", "Russia",
        "Germany", "United Kingdom", "Japan", "India", "France","Canada"]
    )
    if not countries:
        st.error("Please select at least one country.")
    else:

        #df-wide format
        df = pivot_df(df, index='Country', columns='Year', values='Total')
        df = df.loc[countries]

        # st.write("Annually CO2 Emission", df.sort_index())

        #df long-format
        df =df.T.reset_index()
        df = df.astype({"Year":"str"})
        df = pd.melt(df, id_vars=["Year"]).rename(columns={
            "value":"Total (kt)"
        })

        area_chart = (
            alt.Chart(df).mark_area(opacity=0.4).encode(
                x="Year:T",
                y=alt.Y("Total (kt):Q", stack=None),
                color="Country:N"
            )
        )

        st.altair_chart(area_chart, use_container_width=True)
except urllib.error.URLError as e:
    st.error(
        """
        **This dashboard requires internet access.**
        Conection error: %s
        """
        % e.reason
    )

st.markdown("O gráfico acima, por *default*, representa os 9 países com as maiores emissões de CO2, por kilotoneladas, ao longo dos últimos 120 anos.\n\
    Fica evidente a tendência de crescimento da emissão ao longo dos anos.")

#LAYING OUT THE MIDDLE SECTION OF THE APP
row2_1, row_2_2 = st.columns((2,1))

with row2_1:
    st.subheader("Maiores emissores de CO2")
    st.write("**With the highest historical CO2 emissions**")

    try:
        total_by_country = df_raw.groupby('Country')['Total'].sum().reset_index()

        top_10_countries = total_by_country.sort_values(by="Total", ascending=False)[1:11]
        top_10_countries = top_10_countries.astype({"Total":"float64", "Country":"string"})

        bar_chart_top10 = alt.Chart(top_10_countries).mark_bar().encode(
                x=alt.X('Total:Q'),
                y = alt.Y('Country:N', sort="-x"),
                color=alt.Color(
                    "Country",
                    scale=alt.Scale(
                    domain = top_10_countries.Country.tolist(),
                    range = ['red']*3+['steelblue']*7),
                    legend=None
                )
            )

        st.altair_chart(bar_chart_top10, use_container_width=True)

    except BaseException as e:
        raise e

with row_2_2:
    line_break()
    st.markdown("Os três maiores países com as maiores taxas de emissões de CO2 históricas são os :red[países mais populosos]")

row_3_1, row_3_2 = st.columns((3,1))

with row_3_1:
    st.subheader("Menores emissores de CO2")
    st.write("**With the lowest historical CO2 emissions**")

    try:
        total_by_country = df_raw.groupby('Country')['Total'].sum().reset_index()

        top_10_low_countries = total_by_country.sort_values(by="Total", ascending=True)[:10]
        top_10_low_countries = top_10_low_countries.astype({"Total":"float64", "Country":"string"})

        bar_chart_top10_low = alt.Chart(top_10_low_countries).mark_bar().encode(
                x=alt.X('Total:Q'),
                y = alt.Y('Country:N', sort="x"),
                color=alt.Color(
                    "Country",
                    scale=alt.Scale(
                    domain = top_10_low_countries.Country.tolist(),
                    range = ['green']*3+['steelblue']*7),
                    legend=None
                )
            )

        st.altair_chart(bar_chart_top10_low, use_container_width=True)

    except BaseException as e:
        raise e

with row_3_2:
    line_break()
    st.markdown("Os três países com as menores taxas de emissões de CO2 históricas são os :green[países que apresentam as menores taxas populacionais]")

st.markdown("## Variação Anual para o período de 2000-2020")
st.markdown("### Emissão Total por ano")
st.markdown("Como observado no início, nos últimos 20 anos a tendência observada é de crescimento an emissão de CO2.\n \
    As quedas observadas em 2008 e 2019, referem-se, respectivamente, à crise imobiliária de 2008 e à pandemia do COVID-19")

years = year_range(2000, 2022)

try:
    df_last_22_years = df_raw[df_raw.Country != "Global"].groupby("Year")["Total"].sum().reset_index()
    df_last_22_years = df_last_22_years.set_index("Year")
    df_last_22_years = df_last_22_years.loc[years].reset_index()
    df_last_22_years = df_last_22_years.astype({"Year":"string", "Total":"float64"})

    annual_change_total_chart = alt.Chart(df_last_22_years).mark_line(point=True).encode(
        x=alt.X("Year:T", timeUnit="year", title="Year"),
        y="Total:Q"
    )

    st.altair_chart(annual_change_total_chart, use_container_width=True)

except BaseException as e:
    raise e

st.markdown("### Emissão total por ano por categoria")
st.markdown("A emissão de **carvão** (Coal), **gás** (Gas) e **óleo** (Oil), as três maiores fontes de emissão, diminuíram durante os anos de 2008 (crise financeira mundial) e 2019 (Covid-19) \
    Apesar disso, a tendência continua de crescimento nos últimos anos")

try:

    annual_emission_per_category = alt.Chart(emission_per_category()).mark_line(point=True).encode(
        x=alt.X("Year:T", timeUnit="year", title="Year"),
        y="Total:Q",
        color="Category"
    )

    st.altair_chart(annual_emission_per_category, use_container_width=True)

except BaseException as e:
    raise e

row_4_1, row_4_2 = st.columns([2.5,1])

with row_4_2:
    try:

        line_break()
        st.text("\n")
        st.text("\n")
        st.text("\n")

        emission_source = st.selectbox(
            ':pick: **Selecione uma fonte de emissão**',
            ("Total", "Coal", "Oil", "Gas", "Cement", "Flaring", "Other", "Per Capita")
        )

        st.write("Você selecionou:", emission_source)

        df_ranked = rank_by_column(column_to_rank_by=emission_source)
        df_ranked = df_ranked[~df_ranked.loc[:,emission_source].isnull()]
        df_ranked = df_ranked.astype({"Rank":"int64", emission_source:"float64", "Year":"string", "Country":"string"})

        countries_rank = st.multiselect(
        ":earth_americas: **Selecione os países**", list(read_CO2_data().Country.unique()), ["USA", "China", "Russia",
        "Germany", "United Kingdom", "Japan"])

        df_ranked = df_ranked[df_ranked.Country.isin(countries_rank)].sort_values(by=["Year", "Rank"], ascending=(True, False))

        if not countries_rank:
            st.error("Selecione um país, pelo menos.")

    except BaseException as e:
        raise e

with row_4_1:
    try:
        st.markdown("# Ranking de países")
        st.markdown("Selecione a **fonte de emissão** e o **país** no filtro ao lado para verificar o rank dos países durante os anos de 2000-2020.\n\
              \n**Emissões per capita** pode corrigir sua visão sobre o número de emissões por país.\n \
                Por exemplo, China lidera o número de emissões total há 15 anos, porém, em emissões per capita USA é o líder.")

        countries_rank_chart = alt.Chart(df_ranked).mark_line(point=True).encode(
            x=alt.X("Year:T", timeUnit="year", title="Year"),
            y=alt.Y("Rank:Q",
                    sort="descending",
                    scale=alt.Scale(domain=[1,df_ranked.Rank.max()]),
                    axis=alt.Axis(values=[i for i in range(1,df_ranked.Rank.max())], tickMinStep=1)),
            color="Country"
        ).properties(
                        title="Ranking dos países",
                        width=800,
                        height=400
                    )

        st.altair_chart(countries_rank_chart, use_container_width=True)

    except BaseException as e:
        raise e

st.markdown("# Grupos de Países")
st.markdown("Os 231 países foram divididos em 5 grupos de acordo com o coeficiente de determinação $R^{2}$ resultante do ajuste de um modelo \
    de Regressão Linear entre os anos e o total de emissões.")
st.markdown("Selecione ao lado o **Grupo de países** que deseja observar.")

row_5_1, row_5_2 = st.columns([4,1])

with row_5_2:

    group_box = st.selectbox(
    ':factory: **Grupo**',
    ("All groups", "Group A", "Group B", "Group C", "Group D", "Group E")
    )

    if group_box == "All groups":
        lookup_data = data_with_coords_r2_score()
    else:
        lookup_data = data_with_coords_r2_score().set_index("Group").loc[group_box].reset_index()

    st.markdown("#### Grupo A")
    st.markdown("Esse grupo apresentou uma tendência clara de redução de emissões ao longo dos anos de 2000-2021")
    st.markdown("#### Grupo B")
    st.markdown("Esse grupo apresentou uma tendência não muito expressiva de redução de emissões ao longo dos anos de 2000-2021")
    st.markdown("#### Grupo C")
    st.markdown("Esse grupo não apresentou uma tendência clara de redução ou aumento de emissões ao longo dos anos de 2000-2021")
    st.markdown("#### Grupo D")
    st.markdown("Esse grupo apresentou uma tendência de aumento não muito expressiva de emissões ao longo dos anos de 2000-2021")
    st.markdown("#### Grupo E")
    st.markdown("Esse grupo apresentou uma tendência clara de aumento de emissões ao longo dos anos de 2000-2021")

with row_5_1:

    from vega_datasets import data

    countries = alt.topo_feature(data.world_110m.url, "countries")

    background = alt.Chart(countries).mark_geoshape(fill='white')

    foreground = alt.Chart(countries).mark_geoshape(
        stroke='black'
    ).encode(
        color=alt.Color(
            "Group:O", scale=alt.Scale(scheme="lightgreyred"), legend=None,
        ),
        tooltip=[
            alt.Tooltip("Country:N", title="Country"),
            alt.Tooltip("r2_score:Q", title="R2 Score"),
            alt.Tooltip("Group:O", title="Group"),
        ],
    ).transform_lookup(
        lookup="id",
        from_=alt.LookupData(lookup_data, "id", ["Group", "Country", "r2_score"])
    )

    final_map = (
    (background + foreground)
    .configure_view(strokeWidth=0)
    .properties(width=1200, height=800)
    .project("naturalEarth1")

    )

    st.altair_chart(final_map, use_container_width=True)


from conftest import botocore_client

def zones():
    """
    https://botocore.amazonaws.com/v1/documentation/api/latest/reference/services/route53.html#Route53.Client.list_hosted_zones
    """
    return (
        botocore_client.get("route53", "list_hosted_zones", [], {})
        .extract_key("HostedZones")
        .flatten()
        .values()
    )

def cnames():
    records = []

    for zone in zones():
        zone_id = zone["Id"].split("/")[2]
        zone_records = (
            botocore_client.get(
                "route53", "list_resource_record_sets", [], {"HostedZoneId": zone_id}
            )
            .extract_key("ResourceRecordSets")
            .flatten()
            .values()
        )
        records.extend([record for record in zone_records if record["Type"] == "CNAME"])

    return records


import json
from rest_framework.response import Response
from rest_framework import permissions
from rest_framework import viewsets
from rest_framework.decorators import action
from .models import Sheets, Series, Program
from .serializers import SheetSerializer, ProgramSerializer

# Create your views here.

class ProjectViewSet(viewsets.ModelViewSet):
    permission_classes = (permissions.AllowAny,)

    @action(methods=['get'], detail=False, url_path='status')
    def get_status(self, request):
        return Response({"status": "Online"})

    @action(methods=['get'], detail=False, url_path='getAllSeries')
    def get_series(self, request):
        qs = Series.objects.all()
        data = []
        for series in qs:
            data.append({'id': series.id, 'text': series.name})

        return Response({"series": data})

    @action(methods=['get'], detail=True, url_path='getAllPrograms')
    def get_programs(self, request, pk):
        qs = Sheets.objects.filter(series_id=pk)
        program_qs = Program.objects.all()
        data = []
        for sheets in qs:
            for program in program_qs:
                index = next((index for (index, d) in enumerate(data) if d["id"] == program.id), None)
                if sheets.id in program.sheets and index == None:
                    data.append({'id': program.id, 'text': program.name})

        return Response({"data": data})

    @action(methods=['get'], detail=True, url_path='getAllSheets')
    def get_sheets(self, request, pk):
        qs = Program.objects.get(id=pk)
        sheets = Sheets.objects.filter(id__in=qs.sheets)

        data = SheetSerializer(sheets, context={'request': request}, many=True). data

        return Response({"data": data})

    @action(methods=['get'], detail=True, url_path='getAllHeaders')
    def get_headers(self, request, pk):
        qs = Program.objects.get(id=pk)

        return Response({"data": qs.headers})

    @action(methods=['post'], detail=False, url_path='getTimeSeries')
    def get_time_series(self, request):
        data = request.data
        sheets_qs = Sheets.objects.filter(id__in=data['sheets'])
        time_series = []
        time_series_list = []
        for sheets in sheets_qs:
            for index, time in enumerate(sheets.Time_cycle):
                time_series_list.append(time)
                sensor = []
                for headers in data['headers']:
                    value = sheets.__getattribute__(headers)
                    if value[index] == '':
                        value[index] = 0
                    print(value)
                    sensor.append(float(value[index]))
                index = next((index for (index, d) in enumerate(time_series) if d["TIME"] == time), None)
                if not index:
                    obj = {
                        "TIME": time,
                        "SENSOR_DATA": sensor
                    }
                    time_series.append(obj)
                else:
                    time_series[index]['SENSOR_DATA'] += sensor

        return Response({"data": time_series, 'times': time_series_list})

    @action(methods=['post'], detail=False, url_path='signalTransform')
    def get_signal(self, request):
        data = request.data
        import numpy as np
        from scipy.fft import fft, ifft
        signal = []
        for time in data['time_series']:
            x = np.array(time['data'])
            y = fft(x)
            value = []
            for data in y:
                value.append(data.__float__())

            signal.append({
                'name': time['name'],
                'data': value
            })

        return Response({"data": signal})


from fractions import Fraction

import cv2

from .stages.initialize_stage import InitializeStage, InitializeStageConfig
from .tools.video_stream import PiCameraConfig, PiCameraStream

camera_config = PiCameraConfig(
    framerate_range=(Fraction(10, 1), Fraction(30, 1)),
    resolution=(1280, 720),
    iso=800,
    exposure_mode="off",
    awb_mode="off",
    hflip=True,
    vflip=True,
)

def main() -> None:
    camera = None
    try:
        camera = PiCameraStream(camera_config)
        camera.start()

        detector = InitializeStage()
        detector_config = InitializeStageConfig(camera)
        detector.set_config(detector_config, None)
        detector.run()
    finally:
        cv2.destroyAllWindows()

        if camera is not None:
            camera.stop()

if __name__ == "__main__":
    main()


import os
import logging
import multiprocessing as mp
from common import utils
from common import health_check
from election import leader_election

config = utils.initialize_config()
LOGGING_LEVEL = config['GENERAL']['logging_level']
utils.initialize_log(LOGGING_LEVEL)

health_check_process = mp.Process(target=health_check.echo_server, daemon=False)
health_check_process.start()

logging.basicConfig(level=logging.DEBUG)

ELECTION_ID = int(os.environ['ELECTION_ID'])
ELECTION_REPLICAS_AMOUNT = int(os.environ['ELECTION_REPLICAS_AMOUNT'])
SERVICES = os.environ['SERVICES'].split(',')
RETRIES = int(os.environ['RETRIES'])

def dummy():
	health_check.monitor(SERVICES, RETRIES)

le = leader_election.LeaderElection(ELECTION_REPLICAS_AMOUNT, ELECTION_ID, dummy)
le.run()

health_check_process.kill()
health_check_process.join()


import pandas as pd

def infer_freq(series):
    """
    infer the frequency from a pandas Series with `DatetimeIndex`

    :param pandas.Series series: a pandas Series with `DatetimeIndex`
    :return: the inferred frequency object
    :rtype: pandas.DateOffset
    """
    if series.index.freq is not None and hasattr(series.index.freq, "delta"):
        delta = series.index.freq.delta
    else:
        if len(series) <= 1:
            raise ValueError("cannot infer freq from series with one or less elements")
        else:
            delta = series.index[1] - series.index[0]
            diffs = series.index[1:] - series.index[:-1]
            if any([delta != f for f in diffs]):
                raise ValueError("could not infer freq from series")
    return coerce_freq(delta)

def coerce_freq(freq):
    """
    return a convenient representation of a frequency as a pandas.DateOffset object

    :param str/datetime.timedelta/pandas.Timedelta/pandas.DateOffset freq:
        a frequency string or frequency object to coerce
    :return: coerced frequency object
    :rtype: pandas.DateOffset
    """
    if freq is None:
        return freq
    else:
        try:
            return pd.tseries.frequencies.to_offset(freq)
        except ValueError:
            raise ValueError('invalid frequency')


def write_temporal_networks_by_route_type(gtfs, extract_output_dir):
    """
    Write temporal networks by route type to disk.

    Parameters
    ----------
    gtfs: gtfspy.GTFS
    extract_output_dir: str
    """
    util.makedirs(extract_output_dir)
    for route_type in route_types.TRANSIT_ROUTE_TYPES:
        pandas_data_frame = temporal_network(gtfs, start_time_ut=None, end_time_ut=None, route_type=route_type)
        tag = route_types.ROUTE_TYPE_TO_LOWERCASE_TAG[route_type]
        out_file_name = os.path.join(extract_output_dir, tag + ".tnet")
        pandas_data_frame.to_csv(out_file_name, encoding='utf-8', index=False)


# MINHA SOLUÇãO
print('{:=^40}'.format(' TUPLAS '))
print(30*'=')
print('{:^30}'.format('LISTA'))
print(30*'=')

lista = ('Laranja', 2,
         'Abacate', 3.5,
         'Cenoura', 2.35,
         'Tomate', 2.4,
         'Beterraba', 5.3,
         'Água com gás', 4.25,
         'Barra de chocolate', 19.5,
         'Sorvete', 32.5)
for pos in range(0, len(lista)):
    if pos % 2 == 0:
        print(f'{lista[pos]:.<30}', end='')
    else:
        print(f'R${lista[pos]:>6.2f}')

# Exercício Python 076: Crie um programa que tenha uma tupla
# única com nomes de produtos e seus respectivos preços, na
# sequência. No final, mostre uma listagem de preços, organizando
# os dados em forma tabular.


n, d = map(int, input().strip().split(' '))
arr = [int(x) for x in input().strip().split(' ')]

# O(n) solution
# arr2 = arr
# for _ in range(d):
#     arr2 = arr
#     #print(arr, arr2)
#     l = len(arr2)
#     arr2.insert(l, arr[0])
#     #print(arr2)
#     arr = arr2[1:]
#

# O(1) solution
arr2 = list()
arr2 = arr[d:] + arr[:d]

for x in arr2:
    print(x, "", end="")


# Experiment to run k-CTCs

# import necessary components
from CellularPottsModel import CellularPottsModel
from EnergyFunction import EnergyFunction
from CustomEnergyFunctions import CustomEnergyFunctions
from Gradient import Gradient
#### ---- D E F A U L T   P A R A M E T E R S ---- ####
DEFAULT_ENERGY_FUNCTION = EnergyFunction(
	{
		'1,1':1,
		'1,0':5,
	},
	{
		'AreaConstraint' : CustomEnergyFunctions.AreaConstraint,
		'Y-gradient' : CustomEnergyFunctions.yGradientInteract,
	}
)
DEFAULT_SIMULATION_PARAMETERS = {
	'method': 'neumann',
	'showVisualization': False,
	'mutationRate':0.0
}
DEFAULT_INIT_PARAMETERS = {
	'method' : 'custom',
	'starterArea': 20,
	'pos':[(5,30),(5,34)],
	'name': '0'
}
gradients = {
	'Y-gradient': Gradient( lambda x: 26 * x, 100, interactionStrength = 20 )
}

STARTING_POSITIONS = [
	[ ( 5, 45 ) ],
	[ ( 5, 30 ), ( 5, 34 ) ],
	[ ( 5, 30 ), ( 5, 34 ), ( 9, 32 ) ],
	[ ( 5, 30 ), ( 5, 34 ), ( 9, 30 ), ( 9, 34 ) ],
	[ ( 5, 30 ), ( 5, 34 ), ( 9, 30 ), ( 9, 34 ), ( 13, 32 ) ],
	[ ( 5, 25 ), ( 5, 29 ), ( 5, 33 ), ( 9, 25 ), ( 9, 29 ), ( 9, 33 ) ],
	[ ( 5, 30 ), ( 5, 34 ), ( 7, 26 ), ( 9, 30 ), ( 9, 34 ), ( 7, 38 ), ( 13, 32 ) ],
	[ ( 5, 32 ), ( 9, 30 ), ( 9, 34 ), ( 11, 26 ), ( 11, 38 ), ( 13, 30 ), ( 13, 34), ( 17, 32 ) ],
	[ ( 5, 26 ), ( 5, 30 ), ( 5, 34 ), ( 9, 26 ), ( 9, 30 ), ( 9, 34 ), ( 13, 26 ), ( 13, 30 ), ( 13, 34 ) ],
	[ ( 10, 24 ), ( 9, 28 ), ( 9, 32 ), ( 9, 36 ), ( 10, 40 ), ( 5, 32 ), ( 13, 28 ), ( 13, 32 ), ( 17, 32 ), ( 13, 36 )]
]

import numpy as np
import matplotlib.pyplot as plt

def simulate(N, i):
	DEFAULT_INIT_PARAMETERS['name'] = './experiments/'+str(N)+'_'+str(i)

	x = CellularPottsModel( 100 , DEFAULT_ENERGY_FUNCTION , specialObjects = gradients )
	x.initialize( len(DEFAULT_INIT_PARAMETERS['pos']) , **DEFAULT_INIT_PARAMETERS )
	x.runSimulation( 10000 , **DEFAULT_SIMULATION_PARAMETERS)
	x.saveData('./experiments/'+str(N)+'_'+str(i))
	# x.visualize(hold=True)

for N in range(0, len(STARTING_POSITIONS)+1):
	# SET THE STARTING POSITONS
	DEFAULT_INIT_PARAMETERS['pos'] = STARTING_POSITIONS[N-1]

	# SAVE PRIOR DATA
	q = CellularPottsModel( 100, DEFAULT_ENERGY_FUNCTION, specialObjects = gradients )
	q.initialize ( len(DEFAULT_INIT_PARAMETERS['pos']) , **DEFAULT_INIT_PARAMETERS )
	q.saveData( './experiments/BEFORE_' + str(N) )
	del q
	for i in range(0,10):
		# RUN SIMULATIONS
		simulate(N, i)


# -*- coding: utf-8 -*-
"""
@file
@brief Defines a :epkg:`sphinx` extension which proposes a new version of ``.. contents::``
which takes into account titles dynamically added.
"""
from docutils import nodes
from docutils.parsers.rst import directives
from sphinx.util import logging

import sphinx
from sphinx.util.logging import getLogger
from docutils.parsers.rst import Directive
from .sphinx_ext_helper import traverse, NodeLeave, WrappedNode

class postcontents_node(nodes.paragraph):
    """
    defines ``postcontents`` node
    """
    pass

class PostContentsDirective(Directive):
    """
    Defines a sphinx extension which proposes a new version of ``.. contents::``
    which takes into account titles dynamically added.

    Example::

        .. postcontents::

        title 1
        =======

        .. runpython::
            :rst:

            print("title 2")
            print("=======")

    Which renders as:

    .. contents::
        :local:

        title 1
        =======

        title 2
        =======

    Directive ``.. contents::`` only considers titles defined by the user,
    not titles dynamically created by another directives.

    .. warning:: It is not recommended to dynamically insert
        such a directive. It is not recursive.
    """

    node_class = postcontents_node
    name_sphinx = "postcontents"
    has_content = True
    option_spec = {'depth': directives.unchanged,
                   'local': directives.unchanged}

    def run(self):
        """
        Just add a @see cl postcontents_node.

        @return                         list of nodes or list of nodes, container
        """
        lineno = self.lineno

        settings = self.state.document.settings
        env = settings.env if hasattr(settings, "env") else None
        docname = None if env is None else env.docname
        if docname is not None:
            docname = docname.replace("\\", "/").split("/")[-1]
        else:
            docname = ''  # pragma: no cover

        node = postcontents_node()
        node['pclineno'] = lineno
        node['pcdocname'] = docname
        node["pcprocessed"] = 0
        node["depth"] = self.options.get("depth", "*")
        node["local"] = self.options.get("local", None)
        return [node]

def process_postcontents(app, doctree):
    """
    Collect all *postcontents* in the environment.
    Look for the section or document which contain them.
    Put them into the variable *postcontents_all_postcontents* in the config.
    """
    logger = getLogger('postcontents')
    env = app.builder.env
    attr = 'postcontents_all_postcontents'
    if not hasattr(env, attr):
        setattr(env, attr, [])
    attr_list = getattr(env, attr)
    for node in doctree.traverse(postcontents_node):
        # It looks for a section or document which contains the directive.
        parent = node
        while not isinstance(parent, (nodes.document, nodes.section)):
            parent = node.parent
        node["node_section"] = WrappedNode(parent)
        node["pcprocessed"] += 1
        node["processed"] = 1
        attr_list.append(node)
        logger.info("[postcontents] in '{}.rst' line={} found:{}".format(
            node['pcdocname'], node['pclineno'], node['pcprocessed']))
        _modify_postcontents(node, "postcontentsP")

def _modify_postcontents(node, event):
    node["transformed"] = 1
    logger = getLogger('postcontents')
    logger.info("[{}] in '{}.rst' line={} found:{}".format(
        event, node['pcdocname'], node['pclineno'], node['pcprocessed']))
    parent = node["node_section"]
    sections = []
    main_par = nodes.paragraph()
    node += main_par
    roots = [main_par]
    # depth = int(node["depth"]) if node["depth"] != '*' else 20
    memo = {}
    level = 0

    for _, subnode in traverse(parent):
        if isinstance(subnode, nodes.section):
            if len(subnode["ids"]) == 0:
                subnode["ids"].append(f"postid-{id(subnode)}")
            nid = subnode["ids"][0]
            if nid in memo:
                raise KeyError(  # pragma: no cover
                    f"node was already added '{nid}'")
            logger.info("[%r]  %ssection id %r", event, '  ' * level, nid)
            level += 1
            memo[nid] = subnode
            bli = nodes.bullet_list()
            roots[-1] += bli
            roots.append(bli)
            sections.append(subnode)
        elif isinstance(subnode, nodes.title):
            logger.info("[%r]  %stitle %r", event,
                        '  ' * level, subnode.astext())
            par = nodes.paragraph()
            ref = nodes.reference(refid=sections[-1]["ids"][0],
                                  reftitle=subnode.astext(),
                                  text=subnode.astext())
            par += ref
            bullet = nodes.list_item()
            bullet += par
            roots[-1] += bullet
        elif isinstance(subnode, NodeLeave):
            parent = subnode.parent
            if isinstance(parent, nodes.section):
                ids = None if len(parent["ids"]) == 0 else parent["ids"][0]
                if ids in memo:
                    level -= 1
                    logger.info("[%r]  %send of section %r",
                                event, "  " * level, parent["ids"])
                    sections.pop()
                    roots.pop()

def transform_postcontents(app, doctree, fromdocname):
    """
    The function is called by event ``'doctree_resolved'``. It looks for
    every section in page stored in *postcontents_all_postcontents*
    in the configuration and builds a short table of contents.
    The instruction ``.. contents::`` is resolved before every directive in
    the page is executed, the instruction ``.. postcontents::`` is resolved after.

    @param      app             Sphinx application
    @param      doctree         doctree
    @param      fromdocname     docname

    Thiis directive should be used if you need to capture a section
    which was dynamically added by another one. For example @see cl RunPythonDirective
    calls function ``nested_parse_with_titles``. ``.. postcontents::`` will capture the
    new section this function might eventually add to the page.
    For some reason, this function does not seem to be able to change
    the doctree (any creation of nodes is not taken into account).
    """
    logger = logging.getLogger("postcontents")

    # check this is something to process
    env = app.builder.env
    attr_name = 'postcontents_all_postcontents'
    if not hasattr(env, attr_name):
        setattr(env, attr_name, [])
    post_list = getattr(env, attr_name)
    if len(post_list) == 0:
        # No postcontents found.
        return

    for node in post_list:
        if node["pcprocessed"] != 1:
            logger.warning("[postcontents] no first loop was ever processed: 'pcprocessed'=%s , File %r, line %r",
                           node["pcprocessed"], node["pcdocname"], node["pclineno"])
            continue
        if len(node.children) > 0:
            # already processed
            continue

        _modify_postcontents(node, "postcontentsT")

def visit_postcontents_node(self, node):
    """
    does nothing
    """
    pass

def depart_postcontents_node(self, node):
    """
    does nothing
    """
    pass

def setup(app):
    """
    setup for ``postcontents`` (sphinx)
    """
    if hasattr(app, "add_mapping"):
        app.add_mapping('postcontents', postcontents_node)

    app.add_node(postcontents_node,
                 html=(visit_postcontents_node, depart_postcontents_node),
                 epub=(visit_postcontents_node, depart_postcontents_node),
                 elatex=(visit_postcontents_node, depart_postcontents_node),
                 latex=(visit_postcontents_node, depart_postcontents_node),
                 text=(visit_postcontents_node, depart_postcontents_node),
                 md=(visit_postcontents_node, depart_postcontents_node),
                 rst=(visit_postcontents_node, depart_postcontents_node))

    app.add_directive('postcontents', PostContentsDirective)
    app.connect('doctree-read', process_postcontents)
    app.connect('doctree-resolved', transform_postcontents)
    return {'version': sphinx.__display_version__, 'parallel_read_safe': True}


from worker import Worker
import logging
import pickle
from multiprocessing import Process, Queue, Array, cpu_count
from time import sleep
from Env_example import Env
from baseline import LinearBaseline
import os
import numpy as np
import argparse
from tensorflow import nn
from tensorflow.python.client import device_lib

#load previously saved global initial parameters
def load_params(path):
    global_params = []
    with open(path,'rb') as f:
            gps = pickle.load(f)
    for gp in gps:
        global_params.append(Array('f', gp.flatten()))
    return global_params

def parse_args():
    parser = argparse.ArgumentParser(description='Implement multi-process TRPO\
                                     in tensorflow, solving zero-order \
                                     optimization problem')
    parser.add_argument('--init_params_path', type=str, default='global_vars.pickle',
                        help='the path to saved initial parameters')
    parser.add_argument('--hidden_sizes', type=str, default="128,64",
                        help='the size of hidden layer of the policy network\
                        format should be l1,l2, ... ,ln')
    parser.add_argument('--init_std', type=float, default=1.0,
                        help='the initial value of std dev used by gaussian\
                        sampling function')
    parser.add_argument('--hidden_activation', type=str, default="relu",
                        help='the activation used by hidden layer')
    parser.add_argument('--output_activation', type=str, default="tanh",
                        help='the activation used by output layer')
    parser.add_argument('--max_global_iters', type=int, default=501,
                        help='the max number of total iterations of all the\
                        worker processes')
    parser.add_argument('--batch_size', type=int, default=5000,
                        help='the batch size for each worker used to train')
    parser.add_argument('--max_path_length', type=int, default=10,
                        help='the maximum length of each path')
    parser.add_argument('--discount', type=float, default=0.0,
                        help='discount factor when calculate accumulative rewards')
    parser.add_argument('--log_file_name', type=str, default="tf_multiprocess.log",
                        help='the file name of the log recording each workers \
                        activity')
    parser.add_argument('--plot', type=int, default=0,
                        help='the flag indicate to save sampled path or not')
    parser.add_argument('--save_period', type=int, default=100,
                        help='the period of global iterations in which worker_0\
                        will save parameters. If plot is set to be true, a sampled\
                        path will be saved into a pickled binary file')
    parser.add_argument('--over_sampling_ratio', type=int, default=40,
                        help='the ratio over the batch_size that data sampler will sampled by')
    parser.add_argument('--alpha', type=float, default=1.35,
                        help='the parameter alpha of the exponential distribution\
                        that prioritized experience will use')
    parser.add_argument('--beta_zero', type=float, default=0.4,
                        help='the parameter beta of the importance sampling\
                        annealing that prioritized experience will use')
    parser.add_argument('--seeds', type=str, default="1018,1120,211,318",
                        help='the random seeds that each worker will recieve,\
                        format should be s1,s2,...,sn')
    parser.add_argument('--predict_size', type=int, default=10,
                        help='the batch_size that each worker will sampled when\
                        predicting the final minimum point')
    parser.add_argument('--log_std_tol', type=float, default=-5.0,
                        help='the tolerance of the log standard deviation that\
                        gaussian sampling will be parametrized. A too samll value\
                        can increase the convergence rate of the policy, leadint to\
                        a finer result. However, in this case, the training process\
                        will be controlled by max_global_iters. A too large value\
                        will lead to an inaccurate result')
    parser.add_argument('--verbose', type=int, default=0,
                        help='verbose level of logging. 0 only plot basic info, \
                        1 will plot specific training info, 2 will plot detailed \
                        policy convergence data')
    parser.add_argument('--num_gpu_devices', type=int, default=1,
                        help='The number of gpu devices in your mahcine. I have tried\
                        cpu implementation but it is way too slow.')
    parser.add_argument('--vanilla', type=int, default=0,
                        help='Use vanilla TRPO or prioritized experience TRPO. Vanilla TRPO usually has higher convergence stability but easily get stuck in local minimum.')
    return parser.parse_args()

def get_activation(act_type="relu"):
    if act_type == "relu":
        return nn.relu
    elif act_type == "tanh":
        return nn.tanh
    else:
        raise NotImplementedError

def getKey(path):
    return path[0]

if __name__ == "__main__":
    args = parse_args()

    #parsing some options
    init_path = args.init_params_path
    hidden_sizes_str = args.hidden_sizes.split(',')
    hidden_sizes = []
    for h in hidden_sizes_str:
        hidden_sizes.append(int(h))
    init_std = args.init_std
    hidden = args.hidden_activation
    output = args.output_activation
    hidden_activation = get_activation(hidden)
    output_activation = get_activation(output)
    max_global_iters = args.max_global_iters
    batch_size = args.batch_size
    max_path_length = args.max_path_length
    discount = args.discount
    plot = args.plot
    if plot is not 0:
        plot = 1
    save_period = args.save_period
    over_sampling_ratio = args.over_sampling_ratio
    alpha = args.alpha
    beta_zero = args.beta_zero
    seeds = []
    seeds_str = args.seeds.split(',')
    for s in seeds_str:
        seeds.append(int(s))
    predict_size = args.predict_size
    log_std_tol = args.log_std_tol
    verbose = args.verbose

    #logging setting
    log_file_name = args.log_file_name
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                        datefmt='%d %b %H:%M:%S',
                        filename=log_file_name,
                        filemode='w')

    global_params = load_params(init_path)
    results = []
    num_gpu_devices = args.num_gpu_devices
    vanilla = args.vanilla
    worker_count = 0
    while(len(seeds) > 0):
        #object lists to record each worker
        workers = []
        envs = []
        baselines = []
        # Create worker classes
        for i in range(num_gpu_devices):
            if(i >= len(seeds)):
                break;
            envs.append(Env(seeds[i]))
            baselines.append(LinearBaseline())
            workers.append(Worker(
                                envs[-1],
                                worker_count,
                                i,
                                baselines[-1],
                                hidden_sizes = hidden_sizes,
                                init_std = init_std,
                                hidden_activation = hidden_activation,
                                output_activation = output_activation,
                                batch_size=batch_size,
                                max_path_length=max_path_length,
                                max_global_iters = max_global_iters,
                                discount=discount,
                                save_period = save_period,
                                plot = plot,
                                over_sampling_ratio = over_sampling_ratio,
                                alpha = alpha,
                                beta_zero = beta_zero,
                                seed = seeds[i],
                                predict_size = predict_size,
                                log_std_tol = log_std_tol,
                                verbose = verbose,
                                vanilla = vanilla
                            ))
            worker_count += 1
        #Start worker process using python multiprocessing wrapper
        worker_processes = []
        results_queue = Queue()
        for worker in workers:
            p = Process(target=worker.work, args=(global_params, results_queue))
            p.start()
            sleep(0.5)
            worker_processes.append(p)
        for process in worker_processes:
            process.join()
        for i in range(num_gpu_devices):
            results.append(results_queue.get())
        if len(seeds) > num_gpu_devices:
            seeds = seeds[num_gpu_devices:]
        else:
            seeds = []
    #Gathering and sorting results from each child process
    logging.info("The global minimum is:")
    logging.info(np.array2string(sorted(results,key=getKey)[0][1], max_line_width=10000))


import os
import matplotlib.pyplot as plt

import torch

from population import Population
from genome import Genome
from optimizer import ADAMGene, SGDGene
from node import Node
from gene import KernelGene, PoolGene, DenseGene
from net import build_net_from_genome

def decode(line):
    """
    Reconstructs genome from str printed to console
    """
    alias = {"depth_change": "depth_size_change", "str": "stride", "pad": "padding", "d_mult": "depth_mult",
             "pool": "pooling", "mom": "momentum",
             "size": lambda x: [["width", "height"], x.split("*")] if not x == "None" else [[], []],
             "ID": lambda x: [["id"], [int(x)]] if "+" not in x else
                             [["id", "id_in", "id_out"], list(map(int, x.split("+")))]}
    # Stack of objects to be build. Form [class, attributes, Subobject in creation]
    object_stack = []
    for part in line.replace("|", ",").replace("->", "+").replace("(", "+").replace(")", ",").replace(">", ",>").\
            replace("[", "[,").replace("]", ",]").replace("\n", "").replace("False", "enabled=False").\
            replace("True", "enabled=True").split(","):
        if "<" in part or "[" in part:
            # Start genome
            if len(object_stack) == 0:
                object_stack.append([Genome, {"population": 1}, None])
            # New object attribute
            else:
                if "=" not in part:
                    name = None
                    strobj = part
                else:
                    name, strobj = part.split("=")
                obj = None
                for o, n in [(ADAMGene, "ADAMGene"), (SGDGene, "SGDGene"), (KernelGene, "KernelGene"),
                             (PoolGene, "PoolGene"), (DenseGene, "DenseGene"), (Node, "Node"), (list, "[")]:
                    if n in strobj:
                        obj = o
                        break
                if name is not None:
                    object_stack[-1][2] = name.replace(" ", "")
                # Add to Object Stack
                if obj == list:
                    object_stack.append([obj, [], None])
                else:
                    object_stack.append([obj, dict(), None])
        # List done
        elif part == "]":
            obj_raw = object_stack.pop(-1)
            object_stack[-1][1].update({object_stack[-1][2]: obj_raw[1]})
        # Object done
        elif part == ">":
            obj_raw = object_stack.pop(-1)
            obj = obj_raw[0](**obj_raw[1])
            if len(object_stack) == 0:
                # Genome constructed
                obj.nodes_by_id[0].role = 'input'
                obj.nodes_by_id[1].role = 'flatten'
                obj.nodes_by_id[2].role = 'output'
                return obj
            # Add the object attribute
            if isinstance(object_stack[-1][1], list):
                object_stack[-1][1] += [obj]
            else:
                object_stack[-1][1].update({object_stack[-1][2]: obj})
        elif "=" in part:
            # kwarg
            k, v = part.replace(" ", "").split("=")
            if callable(alias.get(k, k)):
                ks, vs = alias.get(k)(v)
            else:
                ks, vs = [alias.get(k, k)], [v]
            for i, v in enumerate(vs):
                try:
                    vs[i] = int(v)
                except (ValueError, TypeError):
                    try:
                        vs[i] = float(v)
                    except (ValueError, TypeError):
                        pass
            for k, v in zip(ks, vs):
                object_stack[-1][1].update({k: v})
        else:
            # arg (only in lists)
            v = part.replace(" ", "")
            try:
                v = int(v)
            except ValueError:
                pass
            object_stack[-1][1] += [v]

def from_human_readable(evaluate, input_size, output_size):
    while True:
        file = input("check genomes from file:")
        if not os.path.exists(file):
            continue
        with open(file, 'r') as f:
            for line in f.readlines():
                if "Genome" in line:
                    genome = decode(line)
                    fig, ax = plt.subplots()
                    genome.visualize(ax=ax, input_size=(1, 28, 28), dbug=True)
                    plt.show()
                    net, _, _ = build_net_from_genome(genome, input_size, output_size)
                    evaluate(net)
        return

def show_genomes(input_size, simultan=False):
    # What to show
    checkpoint = input("checkpoint name:")
    gens = input("generations ['all' / list separated by ' ']:")
    if gens == 'all':
        i = 1
        while os.path.exists(os.path.join("checkpoints", checkpoint, "%02d.cp" % i)):
            i += 1
        generations = list(range(1, i))
    else:
        generations = list(map(int, gens.split(" ")))

    for i in generations:
        p = Population(1, 1, 1, 1, 1, 1, 1, load=(checkpoint, i), load_params=False)
        show = 7
        fig, axs = plt.subplots(len(p.species), show, squeeze=False, figsize=(20, 10))
        fig.canvas.set_window_title('Generation %d' % i)
        for j, sp in enumerate(sorted(p.species.keys(), key=lambda x: p.history[-1][x][1] or 0, reverse=True)):
            for k, g in enumerate(sorted(p.species[sp], key=lambda x: x.acc or 0, reverse=True)[:show]):
                g.visualize(ax=axs[j, k], input_size=input_size)
                axs[j, k].title.set_text('%sacc: %s %%' % ("Species %d " % sp if k == 0 else "",
                                                           "%.2f" % (100 * g.acc) if g.acc is not None else "-"))
        if simultan:
            plt.pause(0.01)
        else:
            plt.show()
    plt.show()

"""
    # Plot Species representative ?
    for i in generations:
        p = Population(1, 1, 1, 1, 1, 1, 1, load=(checkpoint, i))
        show = 7
        fig, axs = plt.subplots(len(p.species), show, squeeze=False, figsize=(20, 10))
        fig.canvas.set_window_title('Generation %d' % i)
        for j, sp in enumerate(sorted(p.species.keys(), key=lambda x: p.history[-1][x][1] or 0, reverse=True)):
            p.species_repr[sp].visualize(ax=axs[j, 0], input_size=input_size)
            axs[j, k].title.set_text('species %d, mean_acc: %s %%' % (sp, p.history[j-1][sp][1] * 100))
            for k, g in enumerate(sorted(p.species[sp], key=lambda x: x.score or 0, reverse=True)[:show]):
                g.visualize(ax=axs[j, k], input_size=input_size)
                axs[j, k].title.set_text('acc: %.2f %%' % ((100 * g.score) if g.score is not None else "-"))
        plt.pause(0.01)
"""


# 인스타그램 에서 해시태그 수집

# http://www.instagram.com/randyolson
# SPA - singgle page application 단일페이지로 웹 어플리케이션 생성
# SPA 특성상 서버로부터 데이터를 일겅와서
# script 태그를 이용해서 _shareData 변수를 만들고
# 이 변수에 데이터를 저장해 둠
# 이미지 클릭시 자바스크립트를 이용해서 UI를 생성후 출력

# request + beatifulsoup
import pprint

from bs4 import BeautifulSoup
import requests

import json
import re

url = 'http://www.instagram.com/randyolson'
ua = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36'}

res = requests.get(url, headers= ua)
html =BeautifulSoup(res.text,'lxml')

# 본문에서 script 태그에 정의된 _sharedData 변수를 찾아냄
body = html.find('body')
print(body)
print('\r\n')
script = body.find('script')
print(script)
print('\r\n')

# script 태그내 문자열만 추출함
raw_json_data = script.text.strip() # 문자열추출, 공백제거
raw_json_data = raw_json_data.replace('window._sharedData = ','').replace(';','')
print(raw_json_data)
print('\r\n')
print('----------------------------------------------------------------------------')

# 전처리된 json 데이터 확인 - 예쁘게 출력
# pprint.pprint(raw_json_data, indent=2)
# print('\r\n')

# json 형식으로 데이터를 메모리에 불러온 후
# 필요한 데이터 추출

#화면에 출력된 게시물 관련 데이터는 총 12개
json_data = json.loads(raw_json_data)

# for i in range(0,12):
#     post_list = json_data['entry_data']['ProfilePage'][0]
#     post_list = post_list['graphql']['user']
#     post_list = post_list['edge_owner_to_timeline_media']
#     post_list = post_list['edges'][i]['node']
#     post_list = post_list['edge_media_to_caption']['edges']
#     tag_list = post_list[0]['node']['text']
#
# # pprint.pprint(tag_list, indent=1)
#
# # 게시물 본문을 #으로 구분지어서 분리한 뒤 리스트에 저장
# tags = tag_list.split('#')
#
# for j in range(1,len(tags)):
#     print(tags[j])

# 정규 표현식으로 해쉬 태그 찾기
# #으로 시작하는 단어를 모두 찾아서 리스트에 저장
tags = re.findall(r'#\w+',raw_json_data)
print(len(tags))

# 리스트에 저장된 해쉬태그를 반복문으로 출력
for i in range(0,len(tags)):
    print(tags[i])

# requests는 브라우저 스크롤 기능이 없음
# 인스타그램 첫페이지에 대해 해쉬태그 추출하고
# 두번째 페이지 부터는 수집불가 - selenium 이용


from django import forms
from .models import UserProfile

# Class to display a form based on the UserProfile model
class UserProfileForm(forms.ModelForm):

    class Meta:
        model = UserProfile
        exclude = ('user',)

    gender = forms.ChoiceField(label='Gender', choices=[('Male', 'Male'), ('Female', 'Female'), ('Other', 'Other')], widget=forms.RadioSelect(), required=False)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        placeholders = {
            'date_of_birth': 'YYYY-MM-DD',
        }

        for field_name, field in self.fields.items():
            field.widget.attrs['class'] = 'border-dark rounded-0'

        self.fields['initials'].widget.attrs['autofocus'] = True
        for field in self.fields:
            if field == 'date_of_birth':
                placeholder = placeholders[field]
                self.fields[field].widget.attrs['placeholder'] = placeholder


import math
n = int(input())
for i in range(n):
    inp = int(input())
    lst = [int(x) for x in input().split()]
    me = 0
    je = 0
    for j in range(inp):
        me+= math.ceil((lst[j]+1)/30) * 10
        je+= math.ceil((lst[j]+1)/60) * 15

    if me < je :
        print("Case {inc}: Mile {dta}".format(inc = i+1,dta = me))
    elif me > je :
        print("Case {inc}: Juice {dta}".format(inc = i+1,dta = je))
    else:
        print("Case {inc}: Mile Juice {dta}".format(inc = i+1,dta = je))


import networkx as nx
import random
import matplotlib.pyplot as plt

def main(plot=False, load=False, prnt_list=False, start_percent=5, infect_percent=15,
        days_infected=3, nodes=100, type="watts", draw="circular", vaccinate_percent=0, vaccine_affectiveness=0):
    num_days = list()
    num_healthy = list()
    for i in range(0, 10):
        if load:
            graphs = LoadFile()
        else:
            graphs = GetRandomGraph(nodes, type)
        print("Clustering Coefficient: " + str(nx.clustering(graphs[0])))
        G = StartInfection(graphs[0], start_percent, days_infected)
        G = Vaccinate(graphs[0], vaccinate_percent)
        color_map = MakeColorMap(G)

        statuses = ListStatus(G)
        if prnt_list:
            print("Day: 0")
            print(statuses)

        if plot:
            if draw == "circular":
                nx.draw_circular(G, node_color=color_map, with_labels=True, font_weight='bold')
            else:
                nx.draw(G, node_color=color_map, with_labels=True, font_weight='bold')
            plt.show()

        # for i in range(0, 10):
        i = 0
        while "Infected" in statuses:
            G = SpreadInfection(G, infect_percent,vaccine_affectiveness)
            # color_map = MakeColorMap(G)
            # nx.draw(G, node_color=color_map, with_labels=True, font_weight='bold')
            # plt.show()

            G = UpdateInfection(G, days_infected)
            color_map = MakeColorMap(G)

            i += 1
            statuses = ListStatus(G)
            if prnt_list:
                print("Day: " + str(i))
                print(statuses)

            if plot:
                if draw == "circular":
                    nx.draw_circular(G, node_color=color_map, with_labels=True, font_weight='bold')
                else:
                    nx.draw(G, node_color=color_map, with_labels=True, font_weight='bold')
                plt.show()
        num_days.append(i)
        num_healthy.append(len(statuses['Healthy']))
    print(num_days)
    print(num_healthy)

def Vaccinate(G, vaccinate_percent):
    for node_data in G.nodes.data():
        node = G.nodes[node_data[0]]
        if random.randint(0, 100) < vaccinate_percent:
            node['vaccinated'] = True
        else:
            node['vaccinated'] = False
    return G

def SpreadInfection(G, infect_percent, vaccine_affectiveness):
    for edges in G.edges:
        if (G.nodes[edges[0]]["status"] == "Healthy" and G.nodes[edges[1]]["status"] == "Infected") or (G.nodes[edges[0]]["status"] == "Infected" and G.nodes[edges[1]]["status"] == "Healthy"):
            node = edges[0]
            if G.nodes[edges[1]]["status"] == "Healthy":
                node = edges[1]

            if random.randint(0, 100) < infect_percent:
                if not G.nodes[node]["vaccinated"] or random.randint(0, 100) > vaccine_affectiveness:
                    G.nodes[node]["status"] = "Newly Infected"

        # print(edges[0] + ":" + G.nodes[edges[0]]["status"])
        # print(edges[1] + ":" + G.nodes[edges[1]]["status"])
        # print()
    return G

def UpdateInfection(G, days_infected):
    for node_data in G.nodes.data():
        node = G.nodes[node_data[0]]
        if node['status'] == "Infected":
            node['days_remaining'] -= 1
            if node['days_remaining'] == 0:
                node['status'] = "Recovered"
        elif node["status"] == "Newly Infected":
            node['status'] = "Infected"
            node['days_remaining'] = days_infected
    return G

def StartInfection(G, start_percent, days_infected):
    for node_data in G.nodes.data():
        node = G.nodes[node_data[0]]
        if random.randint(0, 100) < start_percent:
            node['status'] = "Infected"
            node['days_remaining'] = days_infected
        else:
            node['status'] = "Healthy"
    return G

def MakeColorMap(G):
    color_map = list()
    for node_data in G.nodes.data():
        node = G.nodes[node_data[0]]
        if node['status'] == "Infected":
            color_map.append("red")
        elif node['status'] == "Healthy":
            color_map.append("blue")
        elif node["status"] == "Newly Infected":
            color_map.append("yellow")
        elif node['status'] == "Recovered":
            color_map.append("green")
    return color_map

def ListStatus(G):
    statuses = dict()
    for node_data in G.nodes.data():
        node = G.nodes[node_data[0]]
        if node['status'] not in statuses:
            statuses[node['status']] = list()
        statuses[node['status']].append(node_data[0])
    return statuses

def GetRandomGraph(nodes, type):
    if (type == "random"):
        return [nx.gnm_random_graph(nodes, 350)]
    elif (type == "watts"):
        return [nx.connected_watts_strogatz_graph(nodes, 10, .3)]
    return [nx.connected_watts_strogatz_graph(nodes, 5, .3)]

def LoadFile():
    graph_list = list()
    with open('12_3_3.txt') as f:
        isGraph = True
        count = 0
        entry = 0
        for x in f:
            if x.startswith("Graph"):
                isGraph = True
                graph_list.append(dict())
                entry = 1
            elif x.startswith("Taillenweite"):
                isGraph = False
                count += 1
            elif isGraph and x.strip() != "":
                graph_list[count][entry] = x.split(":")[1].strip().split(" ")
                entry += 1
    count = 0
    graphs = list()
    for graph in graph_list:
        count += 1
        G = nx.Graph()
        for key in graph:
            for edge in graph[key]:
                G.add_edge(str(key), edge)
        graphs.append(G)

    return graphs


#!/usr/bin/env python
"""
Module with channel estimation implementations based on the reference signals in
this package.
"""

from typing import Union

import numpy as np

from .dmrs import DmrsUeSequence
from .srs import SrsUeSequence, UeSequence

class CazacBasedChannelEstimator:
    """
    Estimated the (uplink) channel based on CAZAC (Constant Amplitude Zero
    AutoCorrelation) reference sequences sent by one user (either SRS or
    DMRS).

    The estimation is performed according to the paper [Bertrand2011]_,
    where the received signal in the FREQUENCY DOMAIN is used by the
    estimator.

    Note that for SRS sequences usually a comb pattern is employed such
    that only half of the subcarriers is used to send pilot
    symbols. Therefore, an FFT in the during the estimation will
    effectively interpolate for the other subcarriers. This is controlled
    by the `size_multiplier` argument (default is 2 to accommodate comb
    pattern). If all subcarriers are used to send pilot symbols then set
    `size_multiplier` to 1.

    Parameters
    ----------
    ue_ref_seq : SrsUeSequence | DmrsUeSequence | np.ndarray
        The reference signal sequence.
    size_multiplier : int, optional
        Multiplication factor for the FFT to get the actual channel size.
        When using the comb pattern for SRS this should be 2 (default value),
        but for DMRS, which does not employ the comb pattern, this should be
        set to 1.

    Notes
    -----

    .. [Bertrand2011] Bertrand, Pierre, "Channel Gain Estimation from
       Sounding Reference Signal in LTE," Conference: Proceedings of the
       73rd IEEE Vehicular Technology Conference.
    """
    def __init__(self,
                 ue_ref_seq: Union[SrsUeSequence, DmrsUeSequence, np.ndarray],
                 size_multiplier: int = 2) -> None:
        # If ue_ref_seq is not an instance of UeSequence (or a subclass)
        # assume it is a numpy array.
        if isinstance(ue_ref_seq, UeSequence):
            self._normalized_ref_seq = ue_ref_seq.normalized
            ue_ref_seq = ue_ref_seq.seq_array()
        else:
            self._normalized_ref_seq = False

        self._ue_ref_sequence = ue_ref_seq
        self._size_multiplier = size_multiplier

    @property
    def ue_ref_seq(self) -> np.ndarray:
        """Get the sequence of the UE."""
        return self._ue_ref_sequence

    def estimate_channel_freq_domain(self, received_signal: np.ndarray,
                                     num_taps_to_keep: int) -> np.ndarray:
        """
        Estimate the channel based on the received signal.

        Parameters
        ----------
        received_signal : np.ndarray
            The received reference signal after being transmitted through the
            channel (in the frequency domain). If this is a 2D numpy array
            the first dimensions is assumed to be "receive antennas" while
            the second dimension are the received sequence elements. The
            number of elements in the received signal (per antenna) is
            equal to the channel size (number of subcarriers) divided by
            `size_multiplier`.
        num_taps_to_keep : int
            Number of taps (in delay domain) to keep. All taps from 0 to
            `num_taps_to_keep`-1 will be kept and all other taps will be
            zeroed before applying the FFT to get the channel response in
            the frequency domain.

        Returns
        -------
        freq_response : np.ndarray
            The channel frequency response. Note that for SRS sequences
            this will have twice as many elements as the sent SRS signal,
            since the SRS signal is sent every other subcarrier.
        """
        # Reference signal sequence
        r = self.ue_ref_seq

        if received_signal.ndim == 1:
            # First we multiply (element-wise) the received signal by the
            # conjugate of the reference signal sequence
            y = np.fft.ifft(np.conj(r) * received_signal, r.size)

            # The channel impulse response consists of the first
            # `num_taps_to_keep` elements in `y`.
            tilde_h = y[0:num_taps_to_keep + 1]
        elif received_signal.ndim == 2:
            # Case with multiple receive antennas
            y = np.fft.ifft(
                np.conj(r)[np.newaxis, :] * received_signal, r.size)

            # The channel impulse response consists of the first
            # `num_taps_to_keep` elements in `y`.
            tilde_h = y[:, 0:num_taps_to_keep + 1]
        else:
            raise ValueError(  # pragma: no cover
                "received_signal must have either one dimension ("
                "one receive antenna) or two dimensions (first "
                "dimension being the receive antenna dimension).")

        # Now we can apply the FFT to get the frequency response.
        # The number of subcarriers is twice the number of elements in the
        # SRS sequence due to the comb pattern
        Nsc = r.size
        tilde_H = np.fft.fft(tilde_h, self._size_multiplier * Nsc)

        if self._normalized_ref_seq is True:
            tilde_H *= Nsc

        return tilde_H

class CazacBasedWithOCCChannelEstimator(CazacBasedChannelEstimator):
    """
    Estimated the (uplink) channel based on CAZAC (Constant Amplitude Zero
    AutoCorrelation) reference sequences sent by one user including the
    Orthogonal Cover Code (OCC).

    With OCC the user will send reference signal in multiple time slots, in
    each slot multiplied with the respective OCC sequence element.

    Parameters
    ----------
    ue_ref_seq : DmrsUeSequence
        The reference signal sequence.
    """
    def __init__(self, ue_ref_seq: DmrsUeSequence) -> None:
        cover_code = ue_ref_seq.cover_code
        ue_ref_seq_array = ue_ref_seq.seq_array()
        reference_seq = ue_ref_seq_array[0] * cover_code[0]

        super().__init__(reference_seq, size_multiplier=1)

        self._cover_code = cover_code
        self._normalized_ref_seq = ue_ref_seq.normalized

    @property
    def cover_code(self) -> np.ndarray:
        """Get the cover code of the UE."""
        return self._cover_code

    def estimate_channel_freq_domain(
            self,
            received_signal: np.ndarray,
            num_taps_to_keep: int,
            extra_dimension: bool = True) -> np.ndarray:
        """
        Estimate the channel based on the received signal with cover codes.

        Parameters
        ----------
        received_signal : np.ndarray
            The received reference signal after being transmitted through
            the channel (in the frequency domain).

            Dimension: Depend if there are multiple receive antennas and if
            `extra_dimension` is True or False. Let :math:`Nr` be the
            number of receive antennas, :math:`Ne` be the number of reference
            signal elements (reference signal size without cover code) and
            :math:`Nc` be the cover code size. The dimension of
            `received_signal` must match the table below.

            =================  =======================  ======================
                   /            extra_dimension: True   extra_dimension: False
            =================  =======================  ======================
            Single Antenna      Nc x Ne           (2D)   Ne * Nc          (1D)
            Multiple Antennas   Nr x Nc x Ne      (3D)   Nr x (Ne * Nc)   (2D)
            =================  =======================  ======================

        num_taps_to_keep : int
            Number of taps (in delay domain) to keep. All taps from 0 to
            `num_taps_to_keep`-1 will be kept and all other taps will be
            zeroed before applying the FFT to get the channel response in
            the frequency domain.
        extra_dimension : bool
            If True then the should be an extra dimension in
            `received_signal` corresponding to the cover code dimension. If
            False then the cover code is included in the dimension of the
            reference signal elements.

        Returns
        -------
        freq_response : np.ndarray
            The channel frequency response.
        """
        # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        # xxxxx Add the extra dimension if it does not exist xxxxxxxxxxxxxx
        # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        # Create a view for the received signals. If extra_dimension is
        # false we will reshape this view to add a dimension for the cover
        # code
        r = received_signal.view()
        if extra_dimension is False:
            # Let's reorganize the received signal so that we have the
            # extra dimension
            if received_signal.ndim == 1:
                # Case with a single antenna. Cover code dimension will be
                # the first dimension.
                r.shape = (self.cover_code.size, -1)

            elif received_signal.ndim == 2:
                # Case with multiple antennas. Cover code dimension will be
                # the second dimension.
                num_antennas = r.shape[0]
                r.shape = (num_antennas, self.cover_code.size, -1)
            else:
                raise RuntimeError(
                    'Invalid dimension for received_signal: {0}'.format(
                        r.ndim))

        # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        # xxxxxxxxxxxxxxx Average over the cover code dimension xxxxxxxxxxx
        # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        # Now we can consider the case with the extra cover_code dimension
        if r.ndim == 2:
            # Apply the cover code
            r_mean = np.mean(r * self.cover_code[:, np.newaxis], axis=0)
        elif r.ndim == 3:
            r_mean = np.mean(r * self.cover_code[np.newaxis, :, np.newaxis],
                             axis=1)

        else:
            raise RuntimeError(
                'Invalid dimension for received_signal: {0}'.format(r.ndim))

        # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        # xxxxxxxxxxxxxxx Perform the estimation xxxxxxxxxxxxxxxxxxxxxxxxxx
        # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        # Call the estimate_channel_freq_domain from the base class
        return super().estimate_channel_freq_domain(r_mean, num_taps_to_keep)


import os
from tkinter.filedialog import askdirectory
import pygame
from tkinter import *

root = Tk()
root.minsize(400, 400)

listOfSongs = []
index = 0
labelstring = StringVar()

def playsong(event):
    pygame.mixer.music.play()

def stopsong(event):
    pygame.mixer.music.stop()

def nextsong(event):
    global index

    if index == len(listOfSongs) - 1:
        return
    index += 1
    pygame.mixer.music.load(listOfSongs[index])
    pygame.mixer.music.play()
    updatelable()

def prevsong(event):
    global index

    if index == 0:
        return
    index -= 1
    pygame.mixer.music.load(listOfSongs[index])
    pygame.mixer.music.play()
    updatelable()

def updatelable():
    global index
    labelstring.set(listOfSongs[index])

def directorychooser():

    directory = askdirectory()
    os.chdir(directory)

    for files in os.listdir(directory):
        if files.endswith(".mp3"):
            listOfSongs.append(files)

    pygame.mixer.init()
    labelstring.set(listOfSongs[index])
    pygame.mixer.music.load(listOfSongs[index])
    pygame.mixer.music.play()

directorychooser()

programlabel = Label(root, text='Media Player')
programlabel.pack()

listbox = Listbox(root)
listbox.pack()

songlable = Label(textvariable=labelstring, width=20)
songlable.pack()

i = 0
for song in listOfSongs:
    listbox.insert(i, song)
    i += 1

# Buttons Section
nextbutton = Button(root, text='Next Song')
previousbutton = Button(root, text='Previous Song')
playbutton = Button(root, text='Play')
stopbutton = Button(root, text='Stop')

playbutton.pack()
stopbutton.pack()
nextbutton.pack()
previousbutton.pack()

playbutton.bind("<Button-1>", playsong)
stopbutton.bind("<Button-1>", stopsong)
previousbutton.bind("<Button-1>", prevsong)
nextbutton.bind("<Button-1>", nextsong)

root.mainloop()


class Solution:
    def daysBetweenDates(self, date1: str, date2: str) -> int:
        if date1 > date2:
            date1, date2 = date2, date1
        y1, y2 = int(date1[:4]), int(date2[:4])
        days2 = 0
        while y1 < y2:
            days2 += 366 if isleap(y1) else 365
            y1 += 1
        days1 = dayOfYear(date1)
        days2 += dayOfYear(date2)
        return days2-days1

def isleap(year):
    return year % 400 == 0 or year % 4 == 0 and year % 100 != 0

def dayOfYear(date):
    days=[31,28,31,30,31,30,31,31,30,31,30,31]
    year, month, date = map(int, date.split('-'))
    if isleap(year):
        days[1]+=1
    res=0
    for i in range(month-1):
        res+=days[i]
    return res+date


#!/usr/bin/python3

def search_replace(my_list, search, replace):
    """
    Replaces all occurrences of a given element with another element in a list.

    Args:
        my_list (list): The original list to be modified.
        search: The element to be replaced.
        replace: The new element to replace 'search' with.

    Returns:
        list: Modified list where 'search' items are replaced with 'replace'.
    """
    modified_list = my_list.copy()
    for item in range(len(modified_list)):
        if modified_list[item] == search:
            modified_list[item] = replace
    return modified_list


# pommerman/cli/run_battle.py
# pommerman/agents/TensorFlowAgent/pit.py

import atexit
from datetime import datetime
import os
import random
import sys
import time

import argparse
import numpy as np

from pommerman import helpers, make
from TensorFlowAgent import TensorFlowAgent

from pommerman import utility

import tensorflow as tf

class Pit(object):

    def __init__(self, tfa, saver, game_nums=2):

        self.tfa = tfa
        self.saver = saver
        self.game_nums = game_nums

    def launch_games(self, sess, render=True):

        sess.run(tf.global_variables_initializer())
        self.tfa.restore_weigths(sess, self.saver)
        env = self.tfa.getEnv()

        reward_board = np.zeros((1, 4))

        for i in range(self.game_nums):

            curr_state = env.reset()

            while True:

                if render:
                    env.render()

                all_actions = env.act(curr_state)
                next_state, reward, terminal, _ = env.step(all_actions)

                if terminal:

                    reward_board += np.array(reward)

                    print("Game #{0}, rewards = {1}, reward agent = {2}".format(i, "".join(str(i) + " " for i in reward), reward[self.tfa.agent_id]))
                    break

def main(args):

    tf.reset_default_graph()

    with tf.Session() as sess:

        tfa = TensorFlowAgent(name="TFA", args=args, sess=sess)
        saver = tf.train.Saver(allow_empty=True)
        pit = Pit(tfa, saver, game_nums=2)

        pit.launch_games(sess)

if __name__ == "__main__":

    parser = argparse.ArgumentParser()

    parser.add_argument("--environment", type=str, default="pommerman")
    parser.add_argument("--policy", type=str, default="MlpPolicy")
    parser.add_argument("--checkpoint_dir", type=str, default="./save_model")
    parser.add_argument("--a_learning_rate", type=float, default=0.0001)
    parser.add_argument("--c_learning_rate", type=float, default=0.0002)
    parser.add_argument('--summary_dir', type=str, default='./summary_log')
    parser.add_argument("--cliprange", type=float, default=0.2)
    parser.add_argument("--batch_size", type=int, default=32)
    parser.add_argument("--training_step", type=int, default=10)
    parser.add_argument("--gamma", type=float, default=0.9)
    parser.add_argument("--train", type=str, default="False", choices=["False"])
    parser.add_argument("--type", type=str, default="Simple", choices=["Simple, CNN"])

    args = parser.parse_args()

    main(args)


import pandas as pd
import numpy as np

if __name__ == '__main__':
    pd.set_option('display.max_columns', None)

    columns=["Sex", "Country"]
    X = [[np.nan, 'Germany'], ['Male', np.nan], ['Female', 'Poland'],['Female', 'Brasil'],[np.nan, 'Brasil']]
    df=pd.DataFrame(data=X,columns=columns)
    print(df.head())

    drop_columns=True
    for column in columns:
        labels=df[column].unique().tolist()
        labels = [str(x) for x in labels]  # converting nan to 'nan
        try:
            labels.remove('nan') #remove nan labels
        except:
            pass
        for label in labels:

            new_label=str(column)+'_'+str(label)
            print(f"New label: {new_label} Column: {column}, label: {label}")
            df[new_label] = np.where(df[column] == label, 1, 0)
            df.loc[df[column].isna(), new_label] = np.nan
    if drop_columns:
        df=df.drop(columns=columns) #drop encoded columns

    print(df.head())
    new_columns=df.columns.to_list()
    print(new_columns)

    # explicitly require this experimental feature
    from sklearn.experimental import enable_iterative_imputer  # noqa
    # now you can import normally from sklearn.impute
    from sklearn.impute import IterativeImputer

    imp_mean = IterativeImputer(min_value=0,
                                max_value=1,
                                random_state=0,
                                max_iter=100,
                                tol=1e-6,
                                verbose=2)

    df_imputed=imp_mean.fit_transform(df)
    df_imputed=pd.DataFrame(df_imputed,columns=new_columns)
    print(df_imputed.head())


# a, b = 0, 1
# for i in range(1, 10):
#     print(a)
#     a, b = b, a + b

# x = [1, [1, ['a', 'b']]]
# print(x)

# def search_list(list_of_tuples, value):
#     for comb in list_of_tuples:

#         for x in comb:
#             if x == value:
#                 print(x)
#                 return comb
#     else:
#         return 0

# prices = [('AAPL', 96.43), ('IONS', 39.28), ('GS', 159.53)]
# ticker = 'GS'
# print(search_list(prices, ticker))
import datetime
date = '01-Apr-03'
date_object = datetime.datetime.strptime(date, '%d-%b-%y')
print((date_object).date())


def sum_everything(a, b):
    if isinstance(a,int):#int,list
        print("int,list:")
        print(a+len(b))
        return(a+len(b))
    elif isinstance(a,list):
        if isinstance(b,list):#list,list
            print("list,list:")

            print(a + b)
            return a + b
        elif isinstance(b,str):#list,str
            a.append(b)
            print("list,str:")
            print(a)
            return (a)

    else:                      #str,int
        print("str,int:")
        print(a+str(b))

        return (a+str(b))

sum_everything(['q','2'],['w','5'])#list,list
sum_everything(1,["2","g","hj","2"])#int,list
sum_everything(["2","g","hj","2"],"dgnjgfjgf")#list,str
sum_everything("dgnjgfjgf",4568)#str,int


import pandas as pd
import numpy as np
import os

from fundamentals import constants
from fundamentals.fragments import initialize_peaks
from fundamentals.annotation.annotation import annotate_spectra
from fundamentals.mod_string import maxquant_to_internal, internal_without_mods
import h5py
import argparse, pathlib
from fundamentals.mod_string import parse_modstrings, maxquant_to_internal
from fundamentals.constants import ALPHABET
from mgf_filter.util import timeStamped
from mgf_filter.masterSpectrum import MasterSpectrum

def int_to_onehot(charge):
    precursor_charge = np.full((6), False)
    precursor_charge[charge-1] = True
    return precursor_charge

parser = argparse.ArgumentParser()
parser.add_argument("pool", type=str)					# Filename
args = parser.parse_args()
pool = args.pool

base_path = "/Users/adams/Projects/300K/2022-library-run/Annotation/"
calibrated_path = base_path + "full-truncated-qc/calibrated-40-ppm/" + pool + ".csv"
# calibrated_path = base_path + "precursor-consensus/calibrated-20-ppm/" + pool + ".csv"
# calibrated_path = base_path + "scan-consensus/calibrated-40-ppm/" + pool + ".csv"
file_path = base_path + "full-truncated-qc/calibrated-hdf5/frame-40-ppm-calibrated.hdf5"
# file_path = base_path + "precursor-consensus/calibrated-hdf5/precursor-20-ppm-calibrated.hdf5"
# file_path = base_path + "scan-consensus/calibrated-hdf5/scan-40-ppm-calibrated.hdf5"

calibrated_annot_df = pd.read_csv(calibrated_path)

calibrated_annot_df.INTENSITIES = calibrated_annot_df.INTENSITIES.str.split(";").apply(lambda s: [float(x) for x in s])
calibrated_annot_df.MZ = calibrated_annot_df.MZ.str.split(";").apply(lambda s: [float(x) for x in s])
calibrated_annot_df.SEQUENCE_INT = calibrated_annot_df.SEQUENCE_INT.str.strip("][").str.split(", ").apply(lambda s: [int(x) for x in s])

calibrated_annot_df["PRECURSOR_CHARGE_ONEHOT"] = calibrated_annot_df.apply(lambda row: int_to_onehot(row.PRECURSOR_CHARGE), axis = 1)

collision_energies = np.asarray(calibrated_annot_df.ORIG_COLLISION_ENERGY.values)
adjusted_collision_energies = np.asarray(calibrated_annot_df.aligned_collision_energy.values/100.0)
collision_energies_normalized = np.asarray(calibrated_annot_df.ORIG_COLLISION_ENERGY.values/100.0)
intensities = np.asarray(calibrated_annot_df.INTENSITIES.values.tolist())
masses = np.asarray(calibrated_annot_df.MZ.values.tolist())
methods = np.asarray(calibrated_annot_df.FRAGMENTATION.values,dtype ='S3')
precursor_charges = np.asarray(calibrated_annot_df.PRECURSOR_CHARGE_ONEHOT.values.tolist())
raw_files = np.asarray(calibrated_annot_df.RAW_FILE.values,dtype='S120')
scan_numbers = np.asarray(calibrated_annot_df.SCAN_NUMBER.values)
scores = np.asarray(calibrated_annot_df.SCORE.values)
sequence_integers = np.asarray(calibrated_annot_df.SEQUENCE_INT.values.tolist())
retention_time = np.asarray(calibrated_annot_df.RETENTION_TIME.values)

if os.path.exists(file_path):
    with h5py.File(file_path, 'a') as hf:
        hf["collision_energy"].resize((hf["collision_energy"].shape[0] + collision_energies.shape[0]), axis = 0)
        hf["collision_energy"][-collision_energies.shape[0]:] = collision_energies

        hf["collision_energy_aligned_normed"].resize((hf["collision_energy_aligned_normed"].shape[0] + adjusted_collision_energies.shape[0]), axis = 0)
        hf["collision_energy_aligned_normed"][-adjusted_collision_energies.shape[0]:] = adjusted_collision_energies

        hf["collision_energy_normed"].resize((hf["collision_energy_normed"].shape[0] + collision_energies_normalized.shape[0]), axis = 0)
        hf["collision_energy_normed"][-collision_energies_normalized.shape[0]:] = collision_energies_normalized

        hf["intensities_raw"].resize((hf["intensities_raw"].shape[0] + intensities.shape[0]), axis = 0)
        hf["intensities_raw"][-intensities.shape[0]:] = intensities

        hf["masses_raw"].resize((hf["masses_raw"].shape[0] + masses.shape[0]), axis = 0)
        hf["masses_raw"][-masses.shape[0]:] = masses

        hf["method"].resize((hf["method"].shape[0] + methods.shape[0]), axis = 0)
        hf["method"][-methods.shape[0]:] = methods

        hf["precursor_charge_onehot"].resize((hf["precursor_charge_onehot"].shape[0] + precursor_charges.shape[0]), axis = 0)
        hf["precursor_charge_onehot"][-precursor_charges.shape[0]:] = precursor_charges

        hf["rawfile"].resize((hf["rawfile"].shape[0] + raw_files.shape[0]), axis = 0)
        hf["rawfile"][-raw_files.shape[0]:] = raw_files

        hf["scan_number"].resize((hf["scan_number"].shape[0] + scan_numbers.shape[0]), axis = 0)
        hf["scan_number"][-scan_numbers.shape[0]:] = scan_numbers

        hf["score"].resize((hf["score"].shape[0] + scores.shape[0]), axis = 0)
        hf["score"][-scores.shape[0]:] = scores

        hf["sequence_integer"].resize((hf["sequence_integer"].shape[0] + sequence_integers.shape[0]), axis = 0)
        hf["sequence_integer"][-sequence_integers.shape[0]:] = sequence_integers

        hf["retention_time"].resize((hf["retention_time"].shape[0] + retention_time.shape[0]), axis = 0)
        hf["retention_time"][-retention_time.shape[0]:] = retention_time
else:
    h5f = h5py.File(file_path,'w')
    h5f.create_dataset('collision_energy', data=collision_energies, compression="gzip", chunks=True, maxshape=(None,))
    h5f.create_dataset('collision_energy_aligned_normed', data=adjusted_collision_energies, compression="gzip", chunks=True, maxshape=(None,))
    h5f.create_dataset('collision_energy_normed', data=collision_energies_normalized, compression="gzip", chunks=True, maxshape=(None,))
    h5f.create_dataset('intensities_raw', data=intensities, compression="gzip", chunks=True, maxshape=(None,174))
    h5f.create_dataset('masses_raw', data=masses, compression="gzip", chunks=True, maxshape=(None,174))
    h5f.create_dataset('method', data=methods, compression="gzip", chunks=True, maxshape=(None,))
    h5f.create_dataset('precursor_charge_onehot', data=precursor_charges, compression="gzip", chunks=True, maxshape=(None,6))
    h5f.create_dataset('rawfile', data=raw_files, compression="gzip", chunks=True, maxshape=(None,))
    h5f.create_dataset('scan_number', data=scan_numbers, compression="gzip", chunks=True, maxshape=(None,))
    h5f.create_dataset('score', data=scores, compression="gzip", chunks=True, maxshape=(None,))
    h5f.create_dataset('sequence_integer', data=sequence_integers, compression="gzip", chunks=True, maxshape=(None,32))
    h5f.create_dataset('retention_time', data=retention_time, compression="gzip", chunks=True, maxshape=(None,))
    h5f.close()