import dash
import dash_core_components as dcc
import dash_html_components as html

from dash.dependencies import Input, Output, ClientsideFunction

import numpy as np
import pandas as pd
import datetime
from datetime import datetime as dt
import pathlib
import dash_table as dtt
app = dash.Dash(
    __name__,
    meta_tags=[{"name": "viewport", "content": "width=device-width, initial-scale=1"}],
)

server = app.server
app.config.suppress_callback_exceptions = True

# Path
BASE_PATH = pathlib.Path(__file__).parent.resolve()
DATA_PATH = BASE_PATH.joinpath("data").resolve()

# Read data
# ads= pd.read_csv(DATA_PATH.joinpath("ads.csv"))
ads= pd.read_json(DATA_PATH.joinpath("ads_data.json"))

advertiser_list=ads["title"].unique().tolist()
ads['label'] = ads['label'].map({'str_p':'Strong Political (fraction=1)',
                             'str_np':'Non-political (fraction=0 ) ',
                             'p':'Political (1 < fraction <= 0.5)',
                             'np':'Marginally Political  (0.5< fraction <0)'},
                             na_action=None)
label_list=ads["label"].unique().tolist()
#ads['paid_for_by'] = ads['paid_for_by'].replace(['this is empty'],['None'])

paid_list=ads["paid_for_by"].unique().tolist()
ads['fin_ctg'] = ads['fin_ctg'].replace(['disagreement','This ad is about environmental politics','This ad is about education','This ad is about civil and social rights','This ad is about health' , 'This ad is about economy' , 'This ad is about immigration', 'This ad is about security and foreign policy' , 'This ad is about crime', 'This ad is about political values and governance', 'This ad is about guns' ], ['Disagreement','Social Issue : Environmental Politics','Social Issue : Education','Social Issue : Civil and Social Rights','Social Issue : Health', 'Social Issue : Economy' , 'Social Issue : Immigration' , 'Social Issue : Security and Foreign Policy','Social Issue : Crime','Social Issue : Political Values and Governance', 'Social Issue : Guns'] )
ctg_list=ads["fin_ctg"].unique().tolist()

# ads["created_at"] = ads["created_at"].apply(lambda x: pd.Timestamp(x).strftime('%Y-%m-%d'))

ads.fr=ads.fr*100
ads.fr=ads.fr.apply(np.floor)
ads.fr
fr_list=ads["fr"].tolist()

ads['sponsor']=ads['paid_for_by']
ads['sponsor'] = ads['sponsor'].replace(['this is empty'],['None'])
ads.loc[ads.paid_for_by != 'this is empty','paid_for_by']='Yes'
ads.loc[ads.paid_for_by != 'Yes','paid_for_by']='No'

advcategory_list=ads['categories_1'].unique().tolist()

data_columns = ['Ad message','Advertiser', 'Labeled as political by advertiser',  'Advertiser Webpage','Targeting' ,'Fraction of political votes from citizens(%)']
df_columns = ['text','title', 'paid_for_by', 'page','targets', 'fr']

all_departments = ads.to_dict(orient="records")

wait_time_inputs = [] #[Input((i + "_wait_time_graph"), "selectedData") for i in ["1,2",]]
score_inputs = []# [Input((i + "_score_graph"), "selectedData") for i in ["er",]]

def description_card():
    """

    :return: A Div containing dashboard title & descriptions.
    """
    return html.Div(
        id="description-card",
        children=[
            #html.H5("Political Ads Analytics"),
            html.H3("Welcome to the Political ads Dashboard"),
            html.Div(
                id="intro",
                children="Explore what ads are labeled as political by citizens and advertisers.",
            ),
        ],
    )

def generate_control_card():
    """

    :return: A Div containing controls for graphs.
    """

    return html.Div(
        id="control-card",
        children=[

            html.P("Dissagreement among citizens"),

            html.Div(children='(Fraction of political votes from citizens) ', style={
        'textAlign': 'left',
        'color':'rgb(128,128,128)',
    }),

            dcc.Dropdown(
                id="Label_select",
                options=[{"label": i, "value": i} for i in sorted(ads["label"].unique().tolist())],
                multi=True,
                value=[],
                                 style={'width': '350px'},

            ),
            html.Br(),
             html.P("Social or Political issue "),
            dcc.Dropdown(
                id="category_select",
                options=[{"label": i, "value": i} for i in sorted(ads["fin_ctg"].unique().tolist())],
                value=[],
                                 style={'width': '350px'},

                multi=True,

            ),
            html.Br(),
            html.P("Advertiser Category"),
            dcc.Dropdown(
                id="advcategory_select",
                 value=[],
                options=[{"label": i, "value": i} for i in sorted(ads["categories_1"].unique().tolist())],
                 style={'width': '350px'},
                multi=True,
            ),

            html.Br(),
           html.P("Advertiser"),
            dcc.Dropdown(
                id="advertiser_select",
                 value=[],
                options=[{"label":"All","value":"all"},*[{"label": i, "value": i} for i in sorted(ads["title"].unique().tolist())]],
                 style={'width': '350px'},
                multi=True,
            ),
            html.Br(),
            html.P("Labeled as Political by advertiser "),
            dcc.Checklist(
                id="labeled_by_advertiser_select",
                value=[],
                options=[{"label": i, "value": i} for i in ads["paid_for_by"].unique().tolist()],
                 style={'width': '400px',
      },
               labelStyle={'width': '100px','display': 'inline-block'},

                #optionHeight=55
            ),
             html.Br(),
             html.P("Sponsor "),
           dcc.Dropdown(
               id="paid_select",

                         options=[{"label": i, "value": i} for i in sorted(ads["sponsor"].unique().tolist())],
                      value=[],
                 style={'width': '350px' ,  'wordBreak': 'break-all'},
                multi=True,
           ),
           html.Br(),

        ],
    )

app.layout = html.Div(
    id="app-container",
    children=[

  html.Div(

                className="banner",
                id="app-header",
                style={"background-color": "#f9f9f9"},
                children=[
                    html.A(
                        [
                                         html.Img(
                                            #  src='https://pbs.twimg.com/profile_images/1216754056619864064/YgAhO3fC_400x400.jpg',
                                            src='https://www.univ-grenoble-alpes.fr/uas/SITEUI/LOGO/logo+bleu.svg',

                                         style={'height':'100px', 'width':'100px',"margin-top":"22px",'textAlign': 'left'}
                                         ),

                        ],

                        className="four columns header_img",
                         href='https://www.univ-grenoble-alpes.fr',
                    ),
                    html.Div(
                        [
                            html.H2(
                                "Explore disagreement in political ads labeling ",
                                className="header_title",
                                id="app-title",
                            ) ,
                        ],
                       style={'textAlign': 'center', 'color' : 'rgb(44, 140, 255)' }
                    ),
                ],
            ),

        # Left column
        html.Div(
            id="left-column",
            className="four columns",

            children=[description_card(), generate_control_card()]
            + [
                html.Div(
                    ["initial child"], id="output-clientside", style={"display": "none"}
                )
            ],

        ),
        # Right column

        html.Div(
            id="right-column",
            className="eight columns",
            children=[
                # Patient Volume Heatmap
                                html.Div(
                                    children=[
                                        html.H4("Results for Political ads"),
                                        html.P(children=[
                                            html.Span("0 ",id="count"),
                                            html.Span("ads found ."),
                                            ],id="count-container")
                                     ]),

                # Patient Wait time by Department
                html.Div(
                    id="wait_time_card",
                    children=[

                        html.Hr(),

                        html.Div(
                            children=[
                            html.Div([

                                dtt.DataTable(

                                id="my_datatable",
page_size= 9999 ,
    #   pagination_settings={'page_size': 9999999999},
                               sort_action="native",

                                columns=[{ 'name': col, 'id': df_columns[idx] } for (idx, col) in enumerate(data_columns)],
                                #columns =[{'name': i, 'id': i} for i in ads.loc[:,['advertiser','text','page','fr']]],
                                #virtualization = True ,

                                fixed_rows={'headers': True},

                                style_table={'height':'500px',   },

                                 style_data_conditional=[
      					  {
           					 'if': {'row_index': 'odd'},
           					 'backgroundColor': 'rgb(248, 248, 248)'
       						 }
   								 ],
                                style_header={
                                'backgroundColor': 'rgb(230, 230, 230)',
                                 'fontWeight': 'bold',
                                 },
                                style_cell_conditional=[

                                            {'if': {'column_id': 'text'},
                                             'width': '280px',
                                             'whiteSpace': 'normal',
                                                                                'textAlign': 'center',

                                            },
                                              {'if': {'column_id': 'targets'},
                                             'width': '170px',
                                             'whiteSpace': 'normal',
                                             'textAlign': 'left'
                                            },
                                            {'if': {'column_id': 'title'},
                                             'width': '170px'
                                            },
                                             {'if': {'column_id': 'fr'},
                                             'width': '100px', 'textAlign': 'center',

                                            },
                                            {'if': {'column_id': 'page'},
                                              'width': '170px',
                                              'wordBreak': 'break-all'
                                            },
                                            {'if': {'column_id': 'paid_for_by'},
                                              'width': '170px'
                                            }

                                     ],

                                style_cell= {
                                    #  'width':600,
                                     'textAlign': 'center',
                                    #  'display': 'auto',
                                    #  'flex-direction': 'column',
                                        'fontFamily': 'Open Sans',
                #'height': '60px',
               'padding': '10px 10px',
              'whiteSpace': 'normal',

                                 	  'font-size': '15px',
                                      },

                                ),
                            ]),
                            ],
                        ),
                    ],
                ),

                html.H4('Resources'),
                html.Div(
                     id= 'Resourc_section',
                    children=[
                       html.Div(
                           id='data-ressource',
                           className='five columns',
                           children=[
                          html.B('These ads are extracted from  :'),
                          dcc.Markdown('''
                                    [The ProPublica dataset](https://www.propublica.org/datastore/dataset/political-advertisements-from-facebook)

                                    '''),

                          html.B('Detailed measurement methodology and data analysis  :'),
                        dcc.Markdown('''
                                    [Understanding the Complexity of Detecting Political ads](http://lig-membres.imag.fr/gogao/papers/pol_ads_complexity_WWW2021.pdf)

                                    '''),

                                      ], ),

                        html.Div(
                        id='def-ressource',
                                                   className='five columns',

                        children =
                        [html.B('Definitions of political ads :'),
                        dcc.Markdown('''
                                    1. [Facebook Definition for Political ads](https://www.facebook.com/business/help/167836590566506?id=288762101909005)
                                    2. [Google Definition for Political ads](https://support.google.com/adspolicy/answer/6014595?hl=en)
                                    3. [Twitter Definition for Political ads](https://business.twitter.com/en/help/ads-policies/ads-content-policies/political-content.html)
                                    4. [TikTok Definition for Political ads](https://ads.tiktok.com/help/article?aid=9550)
                                    '''),
                                    ],
                                    ),

                     ],
                ),
            ],
        ),
    ],
)

@app.callback(
    Output('count', 'children'),
[Input('my_datatable','data')],
)
def update_count(data):
    return f"{len(data)} "

@app.callback(
    Output('category_select', 'options'),
[Input('Label_select', 'value'),Input('Label_select', 'options')],
)
def update_output4(label_value,label_options):
    if len(label_value) == 0:
        addd4= ads[ads['label'].isin([x["value"] for x in label_options])]
    else:
        addd4= ads[ads['label'].isin(label_value)]
    return [{'label': i, 'value': i} for i in addd4['fin_ctg'].unique().tolist()]

@app.callback(
    Output('category_select', 'value'),
[Input('Label_select', 'value')],
)
def update_initialze_select(Label_select):
    return []

@app.callback(
    Output('advcategory_select', 'options'),
[Input('category_select', 'value'),Input('category_select', 'options')],
)
def update_output3(category_value,category_options):
    if len(category_value) == 0:
        addd= ads[ads['fin_ctg'].isin([x["value"] for x in category_options])]
    else:
        addd= ads[ads['fin_ctg'].isin(category_value)]
    return [{'label': i, 'value': i} for i in addd['categories_1'].unique().tolist()]

@app.callback(
    Output('advcategory_select', 'value'),
[Input('category_select', 'value')],
)
def update_output33(category_select):
      return []

@app.callback(
    Output('advertiser_select', 'options'),
[Input('advcategory_select', 'value'),Input('advcategory_select', 'options')],
)
def update_output1(advcategory_value,advcategory_options):
    if len(advcategory_value) == 0:
        addd= ads[ads['categories_1'].isin([x["value"] for x in advcategory_options])]
    else:
        addd= ads[ads['categories_1'].isin(advcategory_value)]
    return [{'label': i, 'value': i} for i in addd['title'].unique().tolist()]

@app.callback(
    Output('advertiser_select', 'value'),
[Input('advcategory_select', 'value')],
)
def update_output11(advcategory_select):
      return []

@app.callback(
    Output('paid_select', 'options'),
[Input('advertiser_select', 'value'),Input('advertiser_select', 'options')],
)
def update_output2(advertiser_value,advertiser_options):
    if len(advertiser_value) == 0:
        addd1= ads[ads['title'].isin([x["value"] for x in advertiser_options])]
    else:
         addd1= ads[ads['title'].isin(advertiser_value)]
    return [{'label': i, 'value': i} for i in sorted(addd1['sponsor'].unique().tolist())]

@app.callback(
    Output('paid_select', 'value'),
[Input('advertiser_select', 'value')]

)
def update_output22(advertiser_select):
    return []

@app.callback(
    Output('my_datatable','data'),
    [
        Input('advertiser_select','value'),
        Input('Label_select','value'),
        Input('category_select','value'),
        Input('paid_select','value'),
        Input('advcategory_select','value'),
        Input('labeled_by_advertiser_select','value'),

    ],
)
def update_output(advertiser_select, Label_select,category_select,paid_select,advcategory_select,labeled_by_advertiser_select):

    ad_mask = pd.DataFrame(data={"is_empty":[len(advertiser_select) == 0 for i in range(len(ads))]})
    Label_select_mask = pd.DataFrame(data={"is_empty":[len(Label_select) == 0 for i in range(len(ads))]})
    category_select_mask = pd.DataFrame(data={"is_empty":[len(category_select) == 0 for i in range(len(ads))]})
    paid_select_mask = pd.DataFrame(data={"is_empty":[len(paid_select) == 0 for i in range(len(ads))]})
    advcategory_select_mask = pd.DataFrame(data={"is_empty":[len(advcategory_select) == 0 for i in range(len(ads))]})
    labeled_by_advertiser_select_mask=pd.DataFrame(data={"is_empty":[len(labeled_by_advertiser_select) == 0 for i in range(len(ads))]})

    mask = (ad_mask["is_empty"] | ads['title'].isin(advertiser_select)) & (Label_select_mask["is_empty"] | ads['label'].isin(Label_select)) & (category_select_mask["is_empty"] | ads['fin_ctg'].isin(category_select)) & (paid_select_mask["is_empty"] | ads['sponsor'].isin(paid_select)) & (advcategory_select_mask["is_empty"] | ads['categories_1'].isin(advcategory_select)) & (labeled_by_advertiser_select_mask["is_empty"] | ads['paid_for_by'].isin(labeled_by_advertiser_select))

    add= ads[mask]
    return add.to_dict(orient="records")

# Run the server
if __name__ == "__main__":
    app.run_server(debug=True)


import numpy as np
from sklearn.gaussian_process import GaussianProcessRegressor

import inspect

def clean_kwargs(func, kwargs):
    """Checks if the kwargs are valid for the function"""
    cleaned_kwargs = {}
    sig = inspect.signature(func)
    if not any(p.kind == p.VAR_KEYWORD for p in sig.parameters.values()):
        for key, val in kwargs.items():
            if key in sig.parameters.keys():
                cleaned_kwargs[key] = val
    return cleaned_kwargs

class DistributionCentricGPR(GaussianProcessRegressor):
    """Distribution Centric Gaussian Process Regressor

    Note, this is a wrapper around the sklearn GaussianProcessRegressor and
    requires the exact same (incl. hyperparameters) kernel for all distillation steps.

    Parameters
    ----------
    alpha : float
        The alpha for the distribution centric GPR (i.e. the precomputed alpha for all steps).

    alphas : array-like, shape (num_distillations,)
        The alphas for the distribution centric GPR (i.e. list of alphas for each step).

    **kwargs : dict
        The usual kwargs for the GaussianProcessRegressor.
    """

    def __init__(
        self,
        kernel=None,
        alpha=[1e-10],
        alphas=None,
        optimizer="fmin_l_bfgs_b",
        n_restarts_optimizer=0,
        normalize_y=False,
        copy_X_train=True,
        random_state=None,
        **kwargs,
    ):
        if alphas is not None:
            self.alphas = np.asarray(alphas)
            assert np.all(self.alphas > 0), "alphas must be positive"
            self.num_distillations = len(self.alphas)
            self.alpha = 1 / (np.sum(1 / self.alphas))
        else:
            self.alphas = None
            self.alpha = alpha
            self.num_distillations = None
        super().__init__(
            kernel=kernel,
            alpha=self.alpha,
            optimizer=optimizer,
            n_restarts_optimizer=n_restarts_optimizer,
            normalize_y=normalize_y,
            copy_X_train=copy_X_train,
            random_state=random_state,
        )


"""
Reading Simple GemPy model in Subsurface
========================================
"""

import sys

sys.path.insert(0, 'subsurface/')
import pooch
import subsurface as ss
import subsurface.reader.read_netcdf

# %% md
# Single surface
# --------------

# Pull gempy model
model_files = pooch.retrieve(
    url="https://github.com/cgre-aachen/gempy_data/raw/master/"
        "data/subsurface/example1.zip",
    known_hash="fb79a63eeb874cf0cdca557106c62c67eace23811db5935e57c3448fed7f8978",
    processor=pooch.Unzip()
)

# # %%
fname, = [i for i in model_files if "meshes.nc" in i]
dataset = ss.reader.read_netcdf.read_unstruct(fname)

# %%
obj = ss.TriSurf(dataset)
print(obj.mesh.points_attributes_to_dict)

mesh = ss.visualization.to_pyvista_mesh(obj)
ss.visualization.pv_plot([mesh])

# %% md
# Four Layers
# -----------

# Pull gempy model
model_files_2 = pooch.retrieve(
    url="https://github.com/cgre-aachen/gempy_data/raw/master/data/gempy_models/Kim.zip",
    known_hash="f530a88351ed0e38673c6937161c59a2f69df92202e14c1e5d5729ed5d72a323",
    processor=pooch.Unzip()
)

# %% md
# Triangular meshes
# +++++++++++++++++

# %%
fname, = [i for i in model_files_2 if "meshes.nc" in i]
gempy_tri_mesh_unstruct =  ss.reader.read_netcdf.read_unstruct(fname)
tri_surf = ss.TriSurf(gempy_tri_mesh_unstruct)
mesh = ss.visualization.to_pyvista_mesh(tri_surf)
ss.visualization.pv_plot([mesh])

# %% md
# Regular grid
# ++++++++++++

fname, = [i for i in model_files_2 if "regular_grid.nc" in i]
gempy_struct = ss.reader.read_netcdf.read_struct(fname)
regular_grid = ss.StructuredGrid(gempy_struct)

# %%
pyvista_mesh = ss.visualization.to_pyvista_grid(regular_grid,
                                                data_set_name='property_matrix',
                                                attribute_slice={'Properties': 'id'}
                                                )

ss.visualization.pv_plot([pyvista_mesh])

# %%

pyvista_mesh = ss.visualization.to_pyvista_grid(regular_grid,
                                                data_set_name='block_matrix',
                                                attribute_slice={'Properties': 'id',
                                                                 'Features': 'Default series'})

ss.visualization.pv_plot([pyvista_mesh])


import numpy as np

import sys
sys.path.append("..")

from financepy.utils.date import Date
from financepy.market.discount.interpolator import FinInterpTypes
from financepy.market.discount.curve import DiscountCurve

from FinTestCases import FinTestCases, globalTestCaseMode
testCases = FinTestCases(__file__, globalTestCaseMode)

PLOT_GRAPHS = False

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

def test_FinInterpolatedForwards():

    import matplotlib.pyplot as plt

    tValues = np.array([0.0, 3.0, 5.0, 10.0])
    rValues = np.array([0.04, 0.07, 0.08, 0.09])
    df_values = np.exp(-tValues*rValues)
    tInterpValues = np.linspace(0.0, 12.0, 49)

    curveDate = Date(1, 1, 2019)

    tDates = curveDate.addYears(tValues)
    tInterpDates = curveDate.addYears(tInterpValues)

    for interp_type in FinInterpTypes:

        discount_curve = DiscountCurve(curveDate, tDates, df_values, interp_type)
        dfInterpValues = discount_curve.df(tInterpDates)
        fwdInterpValues = discount_curve.fwd(tInterpDates)
        zeroInterpValues = discount_curve.zeroRate(tInterpDates)

        if PLOT_GRAPHS:
            plt.figure(figsize=(8, 6))
            plt.plot(tValues, df_values, 'o', color='g', label="DFS:")
            plt.plot(tInterpValues, dfInterpValues, color='r',
                     label="DF:" + str(interp_type))
            plt.legend()
            plt.figure(figsize=(8, 6))
            plt.plot(tInterpValues, fwdInterpValues, color='r',
                     label="FWD:" + str(interp_type))
            plt.plot(tInterpValues, zeroInterpValues, color='b',
                     label="ZERO:" + str(interp_type))
            plt.plot(tValues, rValues, 'o', color='g',  label="ZERO RATES")
            plt.legend()

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

test_FinInterpolatedForwards()
testCases.compareTestCases()


import nltk
import csv
import re
import stemming

from nltk.corpus import stopwords,wordnet
from nltk.stem import PorterStemmer, WordNetLemmatizer
from nltk.tokenize import word_tokenize

def main():

	stop_words = stopwords.words('english') + ['advertisement','support']
	print(stop_words)
	#stop_words.append('said')
	#stop_words.append('would')
	#stop_words.append('s')
	#raise NotImplementedError
	ps  = PorterStemmer()
	wnl = WordNetLemmatizer()
	for value in range(1,101):
		text = ""
		with open("./NHL/NHL_File_" + str(value)+ ".txt",'r') as f:
			print("Running")
			for line in f:
				#print(para)
				#para = ["runner","running","run"]
				line = line.lower()
				line = re.sub(r"[^A-Za-z]+",' ',line)
				word_tokens = word_tokenize(line)
				#word_tokens = ["runner","running","run"]
				filtered_sentence = []
				stemmed_words = ""
				for w in word_tokens:
					if w not in stop_words:
						filtered_sentence.append(w)
				for w in filtered_sentence:
					if w.endswith('e') or w.endswith('s') or w.endswith('y') or w.endswith('l'):
						#stemmed_words.append(w +' : '+wnl.lemmatize(w))
						stemmed_words = stemmed_words + wnl.lemmatize(w) + " "
					else:
						#stemmed_words.append(w+' : '+ps.stem(w))
						stemmed_words = stemmed_words + ps.stem(w) + " "
				text = text + stemmed_words
			#print(text)
			text_file = open("./NHL_STEM/NHL_STEM_" +str(value) +".txt", "w")
			text_file.write(text)
			text_file.close()
			#raise NotImplementedError

if __name__ == "__main__":
	main()


from datetime import time
from random import randint

from timeit import default_timer as timer
import numba
import pandas as pd
import numpy as np
import argparse
from numba import njit, prange
from tqdm import tqdm
from timebudget import timebudget
timebudget.set_quiet()  # don't show measurements as they happen
timebudget.report_at_exit()  # Generate report when the program exits
np.set_printoptions(threshold=np.inf)
from plot import plot

def read_map(path: str) -> np.ndarray:
    with open(path) as file:
        df = pd.read_csv(file, sep="\s+", header=None, dtype=np.int64)
    df = df - 1

    print(df)
    return df.to_numpy()

# @njit
def map_to_coords(map):
    number_of_cities = np.max(map)+1
    coords = np.zeros(shape=(number_of_cities, 2), dtype=np.int64)

    for i, row in enumerate(map):
        for j, element in enumerate(row):
            if element > 0:
                coords[element] = (i, j)

    return coords

# @njit
def generate_distance(map: np.ndarray):
    number_of_cities = np.max(map)+1
    distance = np.zeros(shape=(number_of_cities, number_of_cities))
    coords = map_to_coords(map)

    for i in range(number_of_cities):
        for j in range(number_of_cities):
            d = np.linalg.norm(coords[i] - coords[j])
            distance[i][j] = d
            distance[j][i] = d

    return distance

def init_gen(l: int, n: int):
    gene = np.zeros(shape=(n,l), dtype=np.int64)
    rg = np.random.default_rng()
    arr = np.arange(l, dtype=np.int64)

    for i in range(n):
        rg.shuffle(arr)
        gene[i] = arr

    return gene

@njit
def gen_fitness(genes, dist):
    fitness = np.zeros(shape=(len(genes),))

    for i in range(len(genes)):
        cg = genes[i] #current gene

        for j in range(len(cg)):
            j_1 = j + 1

            if j_1>=len(cg):
                j_1 = 0
            fitness[i] += dist[ cg[j] ][ cg[j_1] ]
        # fitness[i] = cd

    return fitness

@njit
def genes_sort(genes: np.ndarray, dist) -> np.ndarray:
    fit = gen_fitness(genes, dist).argsort()
    sorted = np.zeros_like(genes)

    for index, sorted_index in enumerate(fit):
        sorted[index] = genes[sorted_index]

    return sorted

@timebudget
@njit
def replicate(genes: np.ndarray, dist) -> np.ndarray:
    genes_sorted = genes_sort(genes, dist)
    genes_new = np.zeros_like(genes)
    a = genes_sorted[0]
    b = genes_sorted[1]

    for i in range(len(genes)):
        if i < len(genes)//2:
            genes_new[i] = a.copy() if i%2==0 else b.copy()
        else:
            genes_new[i] = genes[np.random.randint(len(genes)-1)].copy()

    return genes_new

@timebudget
@njit
def greedy_crossover(gene1: np.ndarray, gene2: np.ndarray, dist) -> np.ndarray:
    new_gene = np.zeros_like(gene1)-1  # init with -1
    new_gene[0] = gene1[0]

    for i in range(1, len(new_gene)):
        prev_city = new_gene[i-1]

        # find next city in gene1 and gene2
        index_1 = np.where(gene1 == prev_city)[0][0] + 1
        index_2 = np.where(gene2 == prev_city)[0][0] + 1

        # move to the start if overflowing
        index_1 = index_1 if index_1 < len(gene1) else 0
        index_2 = index_2 if index_2 < len(gene1) else 0

        candidate1 = gene1[index_1]
        candidate2 = gene2[index_2]

        # distance is infinive if city is allready in new_gene
        # this way a gene allready taken is allways bad
        dist1 = dist[prev_city][index_1] if candidate1 not in new_gene else np.Infinity
        dist2 = dist[prev_city][index_2] if candidate2 not in new_gene else np.Infinity

        next_city = candidate1 if dist1 < dist2 else candidate2

        # take random if both infinive (see up 7 lines)
        while next_city == np.Infinity or next_city in new_gene:
            next_city = np.random.choice(gene1)
            if next_city not in new_gene:
                break

        new_gene[i] = next_city

    return new_gene

@timebudget
# @njit
def recombine(gene, dist, pc, n_protect):
    gene_sorted = genes_sort(gene, dist)
    n_cross = int(pc * len(gene))
    new_gene = np.zeros_like(gene) - 1  # inti with -1
    # print(len(gene), n_protect, n_cross, n_protect+n_cross)
    assert n_cross + n_protect <= len(gene)

    for i in range(n_protect):
        new_gene[i] = gene_sorted[i]

    for i in prange(n_protect, n_protect+n_cross):
        # crossover_gereeedy
        a = np.random.randint(0, len(gene))
        b = np.random.randint(0, len(gene))
        new_gene[i] = greedy_crossover(gene[a], gene[b], dist)

    for i in range(n_protect+n_cross, len(gene)):
        # random
        ri = np.random.randint(0, len(gene))
        new_gene[i] = gene[ri]

    return new_gene

@timebudget
@njit
def mutate(genes, pm):
    for _ in range(int(pm * len(genes) * len(genes[0]))):
        a = np.random.randint(0, len(genes))
        b = np.random.randint(0, len(genes[a]))
        c = np.random.randint(0, len(genes[a]))
        genes[a][b], genes[a][c] = genes[a][c], genes[a][b]

    return genes

def sumup_genes(genes, dist):
    fitness = gen_fitness(genes, dist)
    # for i in range(len(genes)):
    #     print(f"{i}: {fitness[i]} {genes[i]}")
    print(f"avg fitness: {sum(gen_fitness(genes, dist))/len(genes)}")
    print(f"best fitness: {min(gen_fitness(genes, dist))}")
    print_gene(genes_sort(genes, dist)[0], dist)
    print("-------------")

def print_gene(gene, dist):
    print(str(gene +1).replace("  ", " 0")[1:-1])

# @njit
def run(pc, pm, map, n_gene, max_generations, print_best = False):
    dist = generate_distance(map)
    genes = init_gen(len(dist), n_gene)
    n_generations = 0

    for i in range(max_generations):
        n_generations = i+1

        genes = mutate(genes, pm)

        if min(gen_fitness(genes, dist))<=args.best_fitness:
            break
        if parse().replicate:
            genes = replicate(genes, dist)

        if min(gen_fitness(genes, dist))<=args.best_fitness:
            break
        genes = recombine(genes, dist, pc, args.protect)

        if min(gen_fitness(genes, dist))<=args.best_fitness:
            break

    # if print_best:
        # sumup_genes(genes, dist)

    return n_generations

def find_pc_pm():
    args = parse()
    map = read_map(args.input)
    n_gene = args.n_gene
    max_generations = args.max_generations
    max_fitness = args.best_fitness

    pcr = np.arange(args.pc1, args.pc2, args.pcs)
    pmr = np.arange(args.pm1, args.pm2, args.pms)
    avgs = []

    with tqdm(total=len(pcr)*len(pmr)*args.runs) as pbar:
        for pc in pcr:
            for pm in pmr:
                avg = 0
                for _ in range(args.runs):
                    avg += run(pc,pm,map,n_gene,max_generations, max_fitness)
                    pbar.update(1)

                avg = avg/args.runs
                avgs.append((pc, pm, avg))
                # print((pc, pm, avg))

    return avgs

def parse():
    parser = argparse.ArgumentParser()
    parser.add_argument("input", help = "input map", type=str, default="./05-map-10x10-36border.txt")
    parser.add_argument("--n_gene", "-n", help = "Anzahl der Gene", type=int, default=200)
    parser.add_argument("--max_generations", "-g", help = "Maximalanzahl der Generationen, danach Abbruch", type=int, default=1000)
    parser.add_argument("--best_fitness", "-b", help = "Bei welcher Fitness soll abgebrochen werden", type=float, default=0)
    parser.add_argument("--pc", "-r", help = "Rekombinationsrate", type=float, default=0.9)
    parser.add_argument("--pm", "-m", help = "Mutationsrate", type=float, default=0.0015)
    parser.add_argument("--runs", "-a", help = "Zahl der Läufe, über die gerundet wird", type=int, default=50)
    parser.add_argument("--search", "-s", help = "search for best pm pc in a period", default=False, action="store_true")
    parser.add_argument("--pc1",help = "min Rekombinationsrate", type=float, default=0)
    parser.add_argument("--pc2",help = "max Rekombinationsrate", type=float, default=0.9)
    parser.add_argument("--pcs",help = "steps Rekombinationsrate", type=float, default=0.05)
    parser.add_argument("--pm1",help = "min Mutationsrate", type=float, default=0.0)
    parser.add_argument("--pm2",help = "max Mutationsrate", type=float, default=0.2)
    parser.add_argument("--pms",help = "steps Mutationsrate", type=float, default=0.005)
    parser.add_argument("--filename", "-f", help = "Name to save the data as csv and a plot of the data", type=str, default="ga")
    parser.add_argument("--protect", "-p", help = "protect the best n gene from mutation and crossover", type=int, default=0)
    parser.add_argument("--replicate", "-x", help = "replicate or do not Replicate", type=bool, default=True)

    return parser.parse_args()

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

    if args.search:
        start = timer()
        avgs = find_pc_pm()
        end = timer()
        df = pd.DataFrame(avgs)
        name = args.filename
        df.to_csv(f"{args.filename}.csv")
        plot(df, f"{args.filename}.png", f"pc1: {args.pc1} pc2: {args.pc2} pcs: {args.pcs} pm1: {args.pm1} pm2: {args.pm2} pms: {args.pms} \n {(' s=' + str(args.s)) if args.replication_scheme == 2 else ''} recomb: crossover protect:{'best' if args.protect else 'None'} time: {round(end-start)}")
    else:
        r = run(args.pc, args.pm, read_map(args.input), args.n_gene, args.max_generations, print_best=True)
        print(r)

    # test_crossover()
    # find_pc_pm(map, 50, 500, 36)
    # print(map)
    # coords = map_to_coords(map)
    # # print(coords)
    # dist = generate_distance(map)
    # # print(np.around(dist, decimals=2))
    # # print(dist)
    # # print(dist[16][1])
    # gene = init_gen(36, 100)
    # # print(gene)
    # fitness = gen_fitness(gene, dist)
    # # print(fitness)
    # # print(gen_fitness([[i for i in range(36)]], dist))
    # # print_gene(gene, dist)
    # gene = replicate(gene, dist)
    # # print_gene(gene, dist)
    # # print(gene[0])
    # # print(gene[-1])
    # # crossed = greedy_crossover(gene[0],gene[-1], dist)
    # # print(crossed)

    # # print(crossed.shape)
    # print(gen_fitness([gene[0],gene[1],crossed],dist))

    # for i in range(500):
    #     gene = crossover(gene, dist, 0.8)
    #     print_gene(gene, dist)


from PIL import Image, ImageDraw, ImageFont
import requests
from os import getcwd
from io import BytesIO
from json import load, dump
from base.resource_manager import ResourceManager

rm = ResourceManager()

DATA_PATH = rm.genpath('data')
IMAGES_PATH = rm.genpath('images/artifacts')

def create_image(set,  images_list ):
        font = ImageFont.truetype(rm.genpath('misc')+'font.otf', 45)
        with open(f'{getcwd()}/template_image.json','r') as f:
            template =  load(f)
        new = Image.new(mode='RGBA',size=(template['dimensions']['width'],template['dimensions']['height']))
        images = {}
        for c, i in enumerate(images_list,1):
            images[str(c)] = i

        for image in images:
            url = images[image]
            x_pos = template[image]['x']
            y_pos = template[image]['y']
            r = requests.get(url).content
            paste_ = Image.open(BytesIO(r))
            new.paste(paste_, (x_pos,y_pos))

        ImageDraw.Draw(new).text((460,500), text=set.replace(' ','\n',99), font=font, fill=(255,255,255))
        new.save(IMAGES_PATH+set+'.png',format='PNG')
        print('image created for', set)

artifacts = {}
with open(DATA_PATH+'/artifacts.json','r') as f:
    artifacts = load(f)

for arti in artifacts:
    images = [i['img'][:i['img'].find('/revision')] for i in artifacts[arti]['pieces']]
    create_image(arti, images)
    artifacts[arti]['file'] = arti+'.png'

with open(DATA_PATH+'/artifacts_filed.json', 'w') as f:
    dump(artifacts, f, indent=1)


from typing import List, Optional
from fastapi import FastAPI
import wikipedia
from wikipedia import WikipediaPage
import requests
import urllib
from settings import GITHUB_TOKEN
from schema import WikiResponse, GithubRepo, Response

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/search")
async def search(q: str = None):
    wiki = fetch_wiki_article(q)
    # sof = fetch_stackoverflow_answers(parsed_query)
    repos = fetch_github_repos(urllib.parse.quote(q))
    res = Response(
        query=q,
        wiki=wiki,
        repos=repos)
    return res

def fetch_wiki_article(query: str) -> Optional[WikiResponse]:
    try:
        wp: WikipediaPage = wikipedia.page(query)
        return WikiResponse(
            url=wp.url,
            title=wp.title,
            summary=wp.summary
        )
    except wikipedia.exceptions.DisambiguationError as e:
        print(e)
        wps = wikipedia.search(query)
        wp = wps[0]
        if wp:
            return WikiResponse(
                url=wp.url,
                title=wp.title,
                summary=wp.summary
            )
        return None
    except wikipedia.exceptions.PageError as e:
        print(e)
        return None

def fetch_github_repos(query: str) -> Optional[List[GithubRepo]]:
    url = f"https://api.github.com/search/repositories?q={query}+in:description&page=1"
    headers = {"Authorization": f"token {GITHUB_TOKEN}"}
    res = requests.get(url, headers)

    repos = res.json()["items"]
    if len(repos) == 0:
        return None
    return list(map(
        lambda repo: GithubRepo(
            url = repo.get("html_url", None),
            description = repo.get("description", ""),
            updated_at = repo.get("updated_at", ""),
            star = repo.get("stargazers_count", 0)
        ), repos))


import numpy as np
import matplotlib.pyplot as plt
import scipy.sparse as sp
from matplotlib import cm, colors
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.cm as cmx
import os
from utility import *

plt.rcParams.update({'font.size':20})

class cdata:

    def __init__(self, year_start, year_end, pressure):

        self.pressure = pressure
        domain = os.path.join(os.path.join(os.getcwd(), "ARGO"), "compact"+str(pressure))
        if os.path.exists(domain):
            time, lat, long, temp, pres = np.array([]), np.array([]), np.array([]), np.array([]), np.array([])
            time_list, lat_list, long_list, temp_list, pres_list, time_dic = [], [], [], [], [], {}

            for year in range(year_start, year_end + 1):
                loc_time, loc_lat, loc_long, loc_temp, loc_pres = \
                    np.load(os.path.join(domain, str(year)+".npy"), allow_pickle=True)
                time = np.hstack((time, loc_time))
                lat = np.hstack((lat, loc_lat))
                long = np.hstack((long, loc_long))
                temp = np.hstack((temp, loc_temp))
                pres = np.hstack((pres, loc_pres))
            current_indice = 0
            while current_indice < time.shape[0]:
                old_day = int(time[current_indice])
                new_indice = current_indice
                current_day = int(time[current_indice])
                while current_day == old_day and new_indice < time.shape[0]:
                    new_indice += 1
                    if new_indice > time.shape[0]:
                        current_day, old_day = int(time[new_indice-1]), current_day
                    else:
                        current_day, old_day = int(time[new_indice-1]), current_day
                #time_dic[old_day] = len(time_dic)
                time_list.append(time[current_indice:new_indice])
                lat_list.append(lat[current_indice:new_indice])
                long_list.append(long[current_indice:new_indice])
                temp_list.append(temp[current_indice:new_indice])
                pres_list.append(pres[current_indice:new_indice])
                current_indice = new_indice

            time_anchor = np.min(time)
            for i in range(len(time_list)):
                time_list[i] -= time_anchor

            self.time_anchor = time_anchor
            self.time = time
            self.lat = lat
            self.long = long
            self.temp = temp
            self.pres = pres
            self.xyz = cart((self.long + 180) * np.pi / 180, (self.lat + 90) * np.pi / 180)
            self.true_dim = self.temp.shape[0]
            self.pressure = pressure
            self.year_start = year_start
            self.year_end = year_end

            self.time_list = time_list
            self.lat_list = lat_list
            self.long_list = long_list
            self.temp_list = temp_list
            self.pres_list = pres_list
            #self.time_dic = time_dic

        else:
            print("The data has not been compacted.")

    def spatial_plot(self, indice, llon=-180, llat=-70, ulon=180, ulat=70):

        fig, axes = plt.subplots(ncols=1, nrows=1, figsize=(25, 12))
        map = Basemap(projection='cyl', llcrnrlon=llon, llcrnrlat=llat, urcrnrlon=ulon, urcrnrlat=ulat, ax=axes)
        map.drawcoastlines()
        if len(indice) == 2:
            day_start = indice[0]
            day_end = indice[1]
        elif len(indice) == 1:
            day_start = indice[0]
            day_end = indice[0]
        else:
            print("Error; indice must have only one or two indexes")
        temp_plot = np.array([])
        lat_plot = np.array([])
        long_plot = np.array([])
        for day in range(day_start, day_end + 1):
            try:
                temp_plot = np.hstack((temp_plot, self.temp_list[day]))
                lat_plot = np.hstack((lat_plot, self.lat_list[day]))
                long_plot = np.hstack((long_plot, self.long_list[day]))
            except:
                donothing = 1

        x, y = map(long_plot,  lat_plot)
        jet = plt.get_cmap('jet', 2000)
        cnorm = colors.Normalize(vmin=np.min(temp_plot), vmax=np.max(temp_plot))
        scalarMap = cmx.ScalarMappable(norm=cnorm, cmap=jet)
        im = map.scatter(x, y, marker='D', color=scalarMap.to_rgba(temp_plot))
        plt.colorbar(mappable=scalarMap, ax=axes)
        axes.set_title("ARGO data between day " + str(day_start) + " and " + str(day_end) + " (included) at pressure "\
                       + str(self.pressure) + ".")
        map.fillcontinents(color="k")
        plt.show()


import inspect
import re
from abc import ABC, abstractmethod
from functools import partial
from itertools import chain
from os import remove
from tempfile import NamedTemporaryFile
from typing import Mapping, Optional, Sequence, Tuple

from mypy.api import run, run_dmypy

from assert_typecheck.util import normalize_type

class BaseMypyRunner(ABC):
    @abstractmethod
    def run(self, args: Sequence[str], temp_file_name: str, target_name: str) -> Tuple[str, str, int]:
        ...

    def _assert_mypy_typechecks(self, obj, *, python_version: Optional[str] = None, platform: Optional[str] = None,
                                other_options: Sequence[str] = (), aliases: Optional[Mapping] = None, ):
        filename = inspect.getfile(obj)
        mod_source, source_start = inspect.findsource(obj)
        source_end = len(inspect.getblock(mod_source[source_start:])) + source_start

        mod_source = list(mod_source)
        for line_index in chain(range(source_start), range(source_end, len(mod_source))):
            mod_source[line_index] = mod_source[line_index].rstrip() + '# type: ignore\n'

        if aliases:
            aliases = {normalize_type(k): normalize_type(v) for k, v in aliases.items()}

            def repl(match):
                return aliases[match.group(0)]

            repl_pattern = re.compile(
                r'(?<![a-zA-Z_])' + '|'.join(re.escape(k) for k in aliases.keys()) + r'(?![a-zA-Z_])')

            for line_index in range(source_start, source_end):
                mod_source[line_index] = repl_pattern.sub(repl, mod_source[line_index])

        indent = re.match(r'\s*', mod_source[source_start])[0]  # type: ignore[index]
        mod_source.insert(source_end, f'{indent}import typing; __any: typing.Any = ...;{obj.__name__}(*__any)\n')

        args = [*other_options, '--show-error-context', '--show-error-codes', '--no-error-summary']

        if python_version:
            args.append('--python-version')
            args.append(python_version)

        if platform:
            args.append('--platform')
            args.append(platform)

        with NamedTemporaryFile(mode="w", delete=False) as file:
            file.writelines(mod_source)
        try:
            out, err, exit_code = self.run(args, file.name, filename)
        finally:
            remove(file.name)

        if exit_code == 0:
            return

        if err:
            raise RuntimeError(err)

        raise AssertionError(out)

    def assert_mypy_typechecks(self, obj=None, **kwargs):
        if obj is None:
            return partial(self._assert_mypy_typechecks, **kwargs)
        return self._assert_mypy_typechecks(obj, **kwargs)

class MypyRunner(BaseMypyRunner):
    def run(self, args: Sequence[str], temp_file_name: str, target_name: str) -> Tuple[str, str, int]:
        args = [*args, '--shadow-file', target_name, temp_file_name, target_name]
        return run(args)

_global_mypy_runner = MypyRunner()

assert_mypy_typechecks = _global_mypy_runner.assert_mypy_typechecks

class DMyPyRunner(BaseMypyRunner):
    def __init__(self, python_version: Optional[str] = None, platform: Optional[str] = None,
                 other_options: Sequence[str] = ()):
        self.python_version = python_version
        self.platform = platform
        self.other_options = other_options

    def _assert_mypy_typechecks(self, obj, *, python_version: Optional[str] = None, platform: Optional[str] = None,
                                other_options: Sequence[str] = (), aliases: Optional[Mapping] = None, ):
        other_options = tuple(other_options)
        if (python_version != self.python_version
                or platform != self.platform
                or other_options != self.other_options):
            return assert_mypy_typechecks(obj, python_version=python_version, platform=platform,
                                          other_options=other_options, aliases=aliases)
        return super()._assert_mypy_typechecks(obj, python_version=python_version, platform=platform,
                                               other_options=other_options, aliases=aliases)

    def run(self, args: Sequence[str], temp_file_name: str, target_name: str) -> Tuple[str, str, int]:
        args = ['run', '--', *args, temp_file_name]
        raw_results = run_dmypy(args)
        # because we don't shadow the file, we un-alias the target name ourselves
        out, err, exit_code = raw_results
        out = out.replace(temp_file_name, target_name)
        return out, err, exit_code

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        run_dmypy(['stop'])


from django.test import TestCase
from unittest.mock import patch

from msn import services

class ServiceTest(TestCase):
    def setUp(self):
        self.services = services

    @patch('msn.services.extract_info')
    def test_get_weather(self, extract):
        extract.return_value = {}
        result = self.services.get_weather()
        self.assertEquals(result, {})

    def test_extract_info(self):
        data = {
            'url': 'http//test.com',
            'weather': [
                {'icon': 'icon'}],
            'main': {'temp': 0, 'humidity': 0},
            'temperature': 0
        }
        weather = self.services.extract_info(data)
        self.assertEquals(weather['humidity'], 0)

    def test_get_forms(self):
        result = self.services.get_forms()
        self.assertEquals(result.count(), 0)

    def test_get_form_config(self):
        result = self.services.get_forms_config()
        self.assertEquals(len(result), 0)


# -*- coding: utf-8 -*-
#This part is to update the gui-config.json with the decoded SSR URL.
import json

def updateConfig(newConfig):
    #Load file
    with open('gui-config.json') as json_data_file:
        data = json.load(json_data_file)

    #Begin update for the configuration
    for keys, values in newConfig.items():
        for config in data['configs']:
            config[keys] = values

    #Write file
    with open('gui-config.json', 'w') as outfile:
        json.dump(data, outfile, indent = 2)


import sys
from cx_Freeze import setup, Executable

# Chemin de votre fichier principal Python (main.py)
main_script = 'main.py'

# Options de configuration pour l'exécutable
build_exe_options = {
    'packages': ['tkinter', 'gui', 'PIL', ],  # Liste des packages utilisés par votre application
    'excludes': [],  # Liste des modules à exclure
    'include_files': [('assets', 'assets')]  # Liste des fichiers supplémentaires requis (images, etc.)
}

# Création de l'exécutable
base = None
if sys.platform == 'win32':
    base = 'Win32GUI'  # Si vous voulez une application sans console sur Windows

executable = Executable(script=main_script, base=base)

# Configuration de la construction
setup(
    name='projet_football',
    version='1.0',
    description='application permettant la création de schéma de comoosition de football',
    options={'build_exe': build_exe_options},
    executables=[executable]
)


import pytest

from .conftest import run_pipeline, CONFIGS_DIR

@pytest.mark.forked
def test_bare_config():
    out = run_pipeline(
        "bare_config",
        gets=["a"],
        args=[f"@{CONFIGS_DIR / 'bare.toml'}"],
    )
    assert out.strip() == "a = 1"

    out = run_pipeline(
        "bare_config",
        gets=["a"],
    )
    assert out.strip() == "a = 10"

@pytest.mark.forked
def test_process_with_config():
    out = run_pipeline(
        "process_with_config",
        gets=["Process.envs.x", "Process.envs.y"],
        args=[f"@{CONFIGS_DIR / 'process_with_config.toml'}"],
    )
    assert "Process.envs.x = 1" in out
    assert "Process.envs.y = 2" in out


from decimal import Decimal
from trytond.transaction import Transaction
from trytond.pool import Pool, PoolMeta

__all__ = ['TimesheetLine']
__metaclass__ = PoolMeta

class TimesheetLine:
    __name__ = 'timesheet.line'

    def compute_cost(self):
        Currency = Pool().get('currency.currency')

        cost_price = self.employee.compute_cost_price(date=self.date)

        line_company = self.employee.company
        work_company = self.work.company
        if (line_company != work_company and
                line_company.currency != work_company.currency):
            with Transaction().set_context(date=self.date):
                cost_price = Currency.compute(line_company.currency,
                    cost_price, work_company.currency)

        return Decimal(str(self.hours)) * cost_price


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib as mpl
from scipy.stats import linregress
from matplotlib import rcParams as rcp
import random
from utils import*

# /usr/bin/python3 /Users/tyama/tyama_exp/apps/com_pr.py

# ---------------- データ読み込み ------------------
# 重みなし
dataset_name = input("Enter the dataset name: ")
data_loader = DataLoader(dataset_name)
data_loader.load_graph()
data_loader.load_community()
G = data_loader.get_graph()

# {所属するコミュニティ：頂点idのリスト}, {頂点id:所属するコミュニティ}
c_id, id_c = data_loader.get_communities()

print(G) # グラフのノード数、エッジ数出力
print(f"community_num : {len(c_id)}") # コミュニティ数出力
print("-----------------------------------")

# 重みあり
dataset_name = input("Enter the Weight dataset name: ")
data_loader = DataLoader_W(dataset_name)
data_loader.load_graph()
Gw1 = data_loader.get_graph()
print("-----------------------------------")

dataset_name = input("Enter the Weight dataset name: ")
data_loader = DataLoader_W(dataset_name)
data_loader.load_graph()
Gw2 = data_loader.get_graph()

# ---------------------------------------------------

rwer_num = 100
walk_length = 100
d = 0.85
com_id = 0

# コミュニティ単位で PageRank を計算
com_walk_obj = ComWalk(G, c_id, id_c)
pr = com_walk_obj.com_pagerank(rwer_num, walk_length, d, com_id)
pr_sort = sorted(pr.items(), key=lambda x:x[1], reverse=True)

x_data = []
for item in pr_sort:
    x_data.append(item[0])

for i in range(5):
    print(x_data[i])
print("---------")

com_walk_obj_w1 = ComWalkWeighted(Gw1, c_id, id_c)
pr_w1 = com_walk_obj_w1.com_pagerank_weighted(rwer_num, walk_length, d, com_id)
pr_w1_sort = sorted(pr_w1.items(), key=lambda x:x[1], reverse=True)

y1_data = []
for item in pr_w1_sort:
    y1_data.append(item[0])

for i in range(5):
    print(y1_data[i])
print("---------")

com_walk_obj_w2 = ComWalkWeighted(Gw2, c_id, id_c)
pr_w2 = com_walk_obj_w2.com_pagerank_weighted(rwer_num, walk_length, d, com_id)
pr_w2_sort = sorted(pr_w2.items(), key=lambda x:x[1], reverse=True)

y2_data = []
for item in pr_w2_sort:
    y2_data.append(item[0])

for i in range(5):
    print(y2_data[i])
print("---------")

print("End")

# ---------------------------------------------------
# フォントを設定する。
rcp['font.family'] = 'sans-serif'
rcp['font.sans-serif'] = ['Hiragino Maru Gothic Pro', 'Yu Gothic', 'Meirio', 'Takao', 'IPAexGothic', 'IPAPGothic', 'VL PGothic', 'Noto Sans CJK JP']

# カラーマップを用意する。
cmap = plt.get_cmap("tab10")

# Figureを作成する。
fig = plt.figure()
# Axesを作成する。
ax = fig.add_subplot(111)

# Figureの解像度と色を設定する。
fig.set_dpi(150)
fig.set_facecolor("white")

# Axesのタイトルと色を設定する。
#ax.set_title("物品の所有率")
ax.set_facecolor("white")

# x軸とy軸のラベルを設定する。
ax.set_xlabel("Original Rank", fontsize=14)
ax.set_ylabel("PSI Rank", fontsize=14)

i = 0
for x in x_data:
    ax.plot(i, y1_data.index(x), marker='.', color="b")
    i += 1

plt.show()
# ---------------------------------------------------

# フォントを設定する。
rcp['font.family'] = 'sans-serif'
rcp['font.sans-serif'] = ['Hiragino Maru Gothic Pro', 'Yu Gothic', 'Meirio', 'Takao', 'IPAexGothic', 'IPAPGothic', 'VL PGothic', 'Noto Sans CJK JP']

# カラーマップを用意する。
cmap = plt.get_cmap("tab10")

# Figureを作成する。
fig = plt.figure()
# Axesを作成する。
ax = fig.add_subplot(111)

# Figureの解像度と色を設定する。
fig.set_dpi(150)
fig.set_facecolor("white")

# Axesのタイトルと色を設定する。
#ax.set_title("物品の所有率")
ax.set_facecolor("white")

# x軸とy軸のラベルを設定する。
ax.set_xlabel("Original Rank", fontsize=14)
ax.set_ylabel("PSI_reverse Rank", fontsize=14)

i = 0
for x in x_data:
    ax.plot(i, y2_data.index(x), marker='.', color="b")
    i += 1

plt.show()
# ---------------------------------------------------


"""This module provides various helper functions."""
import logging
import sys
import os
import warnings
from functools import wraps
import urllib

import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np
from tqdm import tqdm

import astropy
from astropy.utils.data import download_file
from astropy.units.quantity import Quantity
import astropy.units as u
from astropy.visualization import (
    PercentileInterval,
    ImageNormalize,
    SqrtStretch,
    LinearStretch,
)
from astropy.time import Time

log = logging.getLogger(__name__)

__all__ = [
    "LightkurveError",
    "LightkurveWarning",
    "KeplerQualityFlags",
    "TessQualityFlags",
    "bkjd_to_astropy_time",
    "btjd_to_astropy_time",
    "show_citation_instructions",
    "finalize_notebook_url",
    "remote_jupyter_proxy_url"
]

class QualityFlags(object):
    """Abstract class"""

    STRINGS = {}
    OPTIONS = {}

    @classmethod
    def decode(cls, quality):
        """Converts a QUALITY value into a list of human-readable strings.

        This function takes the QUALITY bitstring that can be found for each
        cadence in Kepler/K2/TESS' pixel and light curve files and converts into
        a list of human-readable strings explaining the flags raised (if any).

        Parameters
        ----------
        quality : int
            Value from the 'QUALITY' column of a Kepler/K2/TESS pixel or lightcurve file.

        Returns
        -------
        flags : list of str
            List of human-readable strings giving a short description of the
            quality flags raised.  Returns an empty list if no flags raised.
        """
        # If passed an astropy quantity object, get the value
        if isinstance(quality, Quantity):
            quality = quality.value
        result = []
        for flag in cls.STRINGS.keys():
            if quality & flag > 0:
                result.append(cls.STRINGS[flag])
        return result

    @classmethod
    def create_quality_mask(cls, quality_array, bitmask=None):
        """Returns a boolean array which flags good cadences given a bitmask.

        This method is used by the readers of :class:`KeplerTargetPixelFile`
        and :class:`KeplerLightCurve` to initialize their `quality_mask`
        class attribute which is used to ignore bad-quality data.

        Parameters
        ----------
        quality_array : array of int
            'QUALITY' column of a Kepler target pixel or lightcurve file.
        bitmask : int or str
            Bitmask (int) or one of 'none', 'default', 'hard', or 'hardest'.

        Returns
        -------
        boolean_mask : array of bool
            Boolean array in which `True` means the data is of good quality.
        """
        # Return an array filled with `True` by default (i.e. ignore nothing)
        if bitmask is None:
            return np.ones(len(quality_array), dtype=bool)
        if isinstance(quality_array, u.Quantity):
            quality_array = quality_array.value
        # A few pre-defined bitmasks can be specified as strings
        if isinstance(bitmask, str):
            try:
                bitmask = cls.OPTIONS[bitmask]
            except KeyError:
                valid_options = tuple(cls.OPTIONS.keys())
                raise ValueError(
                    "quality_bitmask='{}' is not supported, "
                    "expected one of {}"
                    "".format(bitmask, valid_options)
                )
        # The bitmask is applied using the bitwise AND operator
        quality_mask = (quality_array & bitmask) == 0
        # Log the quality masking as info or warning
        n_cadences = len(quality_array)
        n_cadences_masked = (~quality_mask).sum()
        percent_masked = 100.0 * n_cadences_masked / n_cadences
        logmsg = (
            "{:.0f}% ({}/{}) of the cadences will be ignored due to the "
            "quality mask (quality_bitmask={})."
            "".format(percent_masked, n_cadences_masked, n_cadences, bitmask)
        )
        if percent_masked > 20:
            log.warning("Warning: " + logmsg)
        else:
            log.info(logmsg)
        return quality_mask

class KeplerQualityFlags(QualityFlags):
    """
    This class encodes the meaning of the various Kepler QUALITY bitmask flags,
    as documented in the Kepler Archive Manual (Ref. [1], Table 2.3).

    References
    ----------
    .. [1] Kepler: A Search for Terrestrial Planets. Kepler Archive Manual.
        http://archive.stsci.edu/kepler/manuals/archive_manual.pdf
    """

    AttitudeTweak = 1
    SafeMode = 2
    CoarsePoint = 4
    EarthPoint = 8
    ZeroCrossing = 16
    Desat = 32
    Argabrightening = 64
    ApertureCosmic = 128
    ManualExclude = 256
    # Bit 2**10 = 512 is unused by Kepler
    SensitivityDropout = 1024
    ImpulsiveOutlier = 2048
    ArgabrighteningOnCCD = 4096
    CollateralCosmic = 8192
    DetectorAnomaly = 16384
    NoFinePoint = 32768
    NoData = 65536
    RollingBandInAperture = 131072
    RollingBandInMask = 262144
    PossibleThrusterFiring = 524288
    ThrusterFiring = 1048576

    #: DEFAULT bitmask identifies all cadences which are definitely useless.
    DEFAULT_BITMASK = (
        AttitudeTweak
        | SafeMode
        | CoarsePoint
        | EarthPoint
        | Desat
        | ManualExclude
        | DetectorAnomaly
        | NoData
        | ThrusterFiring
    )
    #: HARD bitmask is conservative and may identify cadences which are useful.
    HARD_BITMASK = (
        DEFAULT_BITMASK
        | SensitivityDropout
        | ApertureCosmic
        | CollateralCosmic
        | PossibleThrusterFiring
    )
    #: HARDEST bitmask identifies cadences with any flag set. Its use is not recommended.
    HARDEST_BITMASK = 2096639

    #: Dictionary which provides friendly names for the various bitmasks.
    OPTIONS = {
        "none": 0,
        "default": DEFAULT_BITMASK,
        "hard": HARD_BITMASK,
        "hardest": HARDEST_BITMASK,
    }

    #: Pretty string descriptions for each flag
    STRINGS = {
        1: "Attitude tweak",
        2: "Safe mode",
        4: "Coarse point",
        8: "Earth point",
        16: "Zero crossing",
        32: "Desaturation event",
        64: "Argabrightening",
        128: "Cosmic ray in optimal aperture",
        256: "Manual exclude",
        1024: "Sudden sensitivity dropout",
        2048: "Impulsive outlier",
        4096: "Argabrightening on CCD",
        8192: "Cosmic ray in collateral data",
        16384: "Detector anomaly",
        32768: "No fine point",
        65536: "No data",
        131072: "Rolling band in optimal aperture",
        262144: "Rolling band in full mask",
        524288: "Possible thruster firing",
        1048576: "Thruster firing",
    }

class TessQualityFlags(QualityFlags):
    """
    This class encodes the meaning of the various TESS QUALITY bitmask flags,
    as documented in the TESS Data Products Description Document (Ref. [1], Table 28).

    References
    ----------
    .. [1] TESS Science Data Products Description Document (EXP-TESS-ARC-ICD-0014)
        https://archive.stsci.edu/missions/tess/doc/EXP-TESS-ARC-ICD-TM-0014.pdf
    """

    AttitudeTweak = 1
    SafeMode = 2
    CoarsePoint = 4
    EarthPoint = 8
    Argabrightening = 16
    Desat = 32
    ApertureCosmic = 64
    ManualExclude = 128
    Discontinuity = 256
    ImpulsiveOutlier = 512
    CollateralCosmic = 1024
    #: The first stray light flag is set manually by MIT based on visual inspection.
    Straylight = 2048
    #: The second stray light flag is set automatically by Ames/SPOC based on background level thresholds.
    Straylight2 = 4096
    # See TESS Science Data Products Description Document
    PlanetSearchExclude = 8192
    BadCalibrationExclude = 16384
    # Set in the sector 20 data release notes
    InsufficientTargets = 32768

    #: DEFAULT bitmask identifies all cadences which are definitely useless.
    DEFAULT_BITMASK = (
        AttitudeTweak | SafeMode | CoarsePoint | EarthPoint | Desat | ManualExclude
    )
    #: HARD bitmask is conservative and may identify cadences which are useful.
    HARD_BITMASK = (
        DEFAULT_BITMASK | ApertureCosmic | CollateralCosmic | Straylight | Straylight2
    )
    #: HARDEST bitmask identifies cadences with any flag set. Its use is not recommended.
    HARDEST_BITMASK = 65535

    #: Dictionary which provides friendly names for the various bitmasks.
    OPTIONS = {
        "none": 0,
        "default": DEFAULT_BITMASK,
        "hard": HARD_BITMASK,
        "hardest": HARDEST_BITMASK,
    }

    #: Pretty string descriptions for each flag
    STRINGS = {
        1: "Attitude tweak",
        2: "Safe mode",
        4: "Coarse point",
        8: "Earth point",
        16: "Argabrightening",
        32: "Desaturation event",
        64: "Cosmic ray in optimal aperture",
        128: "Manual exclude",
        256: "Discontinuity corrected",
        512: "Impulsive outlier",
        1024: "Cosmic ray in collateral data",
        2048: "Straylight",
        4096: "Straylight2",
        8192: "Planet Search Exclude",
        16384: "Bad Calibration Exclude",
        32768: "Insufficient Targets for Error Correction Exclude",
    }

def channel_to_module_output(channel):
    """Returns a (module, output) pair given a CCD channel number.

    Parameters
    ----------
    channel : int
        Channel number

    Returns
    -------
    module, output : tuple of ints
        Module and Output number
    """
    if channel < 1 or channel > 88:
        raise ValueError("Channel number must be in the range 1-88.")
    lookup = _get_channel_lookup_array()
    lookup[:, 0] = 0
    modout = np.where(lookup == channel)
    return modout[0][0], modout[1][0]

def module_output_to_channel(module, output):
    """Returns the CCD channel number for a given module and output pair.

    Parameters
    ----------
    module : int
        Module number
    output : int
        Output number

    Returns
    -------
    channel : int
        Channel number
    """
    if module < 1 or module > 26:
        raise ValueError("Module number must be in range 1-26.")
    if output < 1 or output > 4:
        raise ValueError("Output number must be 1, 2, 3, or 4.")
    return _get_channel_lookup_array()[module, output]

def _get_channel_lookup_array():
    """Returns a lookup table which maps (module, output) onto channel."""
    # In the array below, channel == array[module][output]
    # Note: modules 1, 5, 21, 25 are the FGS guide star CCDs.
    return np.array(
        [
            [0, 0, 0, 0, 0],
            [1, 85, 0, 0, 0],
            [2, 1, 2, 3, 4],
            [3, 5, 6, 7, 8],
            [4, 9, 10, 11, 12],
            [5, 86, 0, 0, 0],
            [6, 13, 14, 15, 16],
            [7, 17, 18, 19, 20],
            [8, 21, 22, 23, 24],
            [9, 25, 26, 27, 28],
            [10, 29, 30, 31, 32],
            [11, 33, 34, 35, 36],
            [12, 37, 38, 39, 40],
            [13, 41, 42, 43, 44],
            [14, 45, 46, 47, 48],
            [15, 49, 50, 51, 52],
            [16, 53, 54, 55, 56],
            [17, 57, 58, 59, 60],
            [18, 61, 62, 63, 64],
            [19, 65, 66, 67, 68],
            [20, 69, 70, 71, 72],
            [21, 87, 0, 0, 0],
            [22, 73, 74, 75, 76],
            [23, 77, 78, 79, 80],
            [24, 81, 82, 83, 84],
            [25, 88, 0, 0, 0],
        ]
    )

def running_mean(data, window_size):
    """Returns the moving average of an array `data`.

    Parameters
    ----------
    data : array of numbers
        The running mean will be computed on this data.
    window_size : int
        Window length used to compute the running mean.
    """
    if window_size > len(data):
        window_size = len(data)
    cumsum = np.cumsum(np.insert(data, 0, 0))
    return (cumsum[window_size:] - cumsum[:-window_size]) / float(window_size)

def bkjd_to_astropy_time(bkjd) -> Time:
    """Converts Kepler Barycentric Julian Day (BKJD) time values to an
    `astropy.time.Time` object.

    Kepler Barycentric Julian Day (BKJD) is a Julian day minus 2454833.0
    (UTC=January 1, 2009 12:00:00) and corrected to the arrival times
    at the barycenter of the Solar System.
    BKJD is the format in which times are recorded in the Kepler data products.
    The time is in the Barycentric Dynamical Time frame (TDB), which is a
    time system that is not affected by leap seconds.
    See Section 2.3.2 in the Kepler Archive Manual for details.

    Parameters
    ----------
    bkjd : float or array of floats
        Barycentric Kepler Julian Day.

    Returns
    -------
    time : `astropy.time.Time` object
        Resulting time object.
    """
    bkjd = np.atleast_1d(bkjd)
    # Some data products have missing time values;
    # we need to set these to zero or `Time` cannot be instantiated.
    bkjd[~np.isfinite(bkjd)] = 0
    return Time(bkjd, format="bkjd", scale="tdb")

def btjd_to_astropy_time(btjd) -> Time:
    """Converts TESS Barycentric Julian Day (BTJD) values to an
    `astropy.time.Time` object.

    TESS Barycentric Julian Day (BTJD) is a Julian day minus 2457000.0
    and corrected to the arrival times at the barycenter of the Solar System.
    BTJD is the format in which times are recorded in the TESS data products.
    The time is in the Barycentric Dynamical Time frame (TDB), which is a
    time system that is not affected by leap seconds.

    Parameters
    ----------
    btjd : float or array of floats
        Barycentric TESS Julian Day

    Returns
    -------
    time : `astropy.time.Time` object
        Resulting time object.
    """
    btjd = np.atleast_1d(btjd)
    btjd[~np.isfinite(btjd)] = 0
    return Time(btjd, format="btjd", scale="tdb")

def plot_image(
    image,
    ax=None,
    scale="linear",
    origin="lower",
    xlabel="Pixel Column Number",
    ylabel="Pixel Row Number",
    clabel="Flux ($e^{-}s^{-1}$)",
    title=None,
    show_colorbar=True,
    vmin=None,
    vmax=None,
    **kwargs
):
    """Utility function to plot a 2D image

    Parameters
    ----------
    image : 2d array
        Image data.
    ax : `~matplotlib.axes.Axes`
        A matplotlib axes object to plot into. If no axes is provided,
        a new one will be generated.
    scale : str
        Scale used to stretch the colormap.
        Options: 'linear', 'sqrt', or 'log'.
    origin : str
        The origin of the coordinate system.
    xlabel : str
        Label for the x-axis.
    ylabel : str
        Label for the y-axis.
    clabel : str
        Label for the color bar.
    title : str or None
        Title for the plot.
    show_colorbar : bool
        Whether or not to show the colorbar
    vmin : float
        Minimum colorbar value. By default, the 2.5%-percentile is used.
    vmax : float
        Maximum colorbar value. By default, the 97.5%-percentile is used.
    kwargs : dict
        Keyword arguments to be passed to `matplotlib.pyplot.imshow`.

    Returns
    -------
    ax : `~matplotlib.axes.Axes`
        The matplotlib axes object.
    """
    if isinstance(image, u.Quantity):
        image = image.value
    if ax is None:
        _, ax = plt.subplots()

    if vmin is None or vmax is None:
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", RuntimeWarning)  # ignore image NaN values
            mask = np.nan_to_num(image) > 0
            if mask.any() > 0:
                vmin_default, vmax_default = PercentileInterval(95.0).get_limits(
                    image[mask]
                )
            else:
                vmin_default, vmax_default = 0, 0
            if vmin is None:
                vmin = vmin_default
            if vmax is None:
                vmax = vmax_default

    norm = None
    if scale is not None:
        if scale == "linear":
            norm = ImageNormalize(
                vmin=vmin, vmax=vmax, stretch=LinearStretch(), clip=False
            )
        elif scale == "sqrt":
            norm = ImageNormalize(
                vmin=vmin, vmax=vmax, stretch=SqrtStretch(), clip=False
            )
        elif scale == "log":
            # To use log scale we need to guarantee that vmin > 0, so that
            # we avoid division by zero and/or negative values.
            norm = LogNorm(vmin=max(vmin, sys.float_info.epsilon), vmax=vmax, clip=True)
        else:
            raise ValueError("scale {} is not available.".format(scale))
    cax = ax.imshow(image, origin=origin, norm=norm, **kwargs)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_title(title)
    if show_colorbar:
        cbar = plt.colorbar(cax, ax=ax, label=clabel)
        cbar.ax.yaxis.set_tick_params(tick1On=False, tick2On=False)
        cbar.ax.minorticks_off()
    return ax

class LightkurveError(Exception):
    """Class for Lightkurve exceptions."""

    pass

class LightkurveWarning(Warning):
    """Class for all Lightkurve warnings."""

    pass

class LightkurveDeprecationWarning(LightkurveWarning):
    """Class for all Lightkurve deprecation warnings."""

    pass

def suppress_stdout(f, *args, **kwargs):
    """A simple decorator to suppress function print outputs."""

    @wraps(f)
    def wrapper(*args, **kwargs):
        # redirect output to `null`
        with open(os.devnull, "w") as devnull:
            old_out = sys.stdout
            sys.stdout = devnull
            try:
                return f(*args, **kwargs)
            # restore to default
            finally:
                sys.stdout = old_out

    return wrapper

def validate_method(method, supported_methods):
    """Raises a `ValueError` if a method is not supported.

    Parameters
    ----------
    method : str
        The method specified by the user.
    supported_methods : list of str
        The methods supported.  All method names must be lowercase.

    Returns
    -------
    method : str
        Will return the method name if it is supported.
    """
    method = method.lower()
    if method in supported_methods:
        return method
    raise ValueError(
        "method '{}' is not supported; "
        "must be one of {}".format(method, supported_methods)
    )

def centroid_quadratic(data, mask=None):
    """Computes the quadratic estimate of the centroid in a 2d-array.

    This method will fit a simple 2D second-order polynomial
    $P(x, y) = a + bx + cy + dx^2 + exy + fy^2$
    to the 3x3 patch of pixels centered on the brightest pixel within
    the image.  This function approximates the core of the Point
    Spread Function (PSF) using a bivariate quadratic function, and returns
    the maximum (x, y) coordinate of the function using linear algebra.

    For the motivation and the details around this technique, please refer
    to Vakili, M., & Hogg, D. W. 2016, ArXiv, 1610.05873.

    Caveat: if the brightest pixel falls on the edge of the data array, the fit
    will tend to fail or be inaccurate.

    Parameters
    ----------
    data : 2D array
        The 2D input array representing the pixel values of the image.
    mask : array_like (bool), optional
        A boolean mask, with the same shape as `data`, where a **True** value
        indicates the corresponding element of data is masked.

    Returns
    -------
    column, row : tuple
        The coordinates of the centroid in column and row.  If the fit failed,
        then (NaN, NaN) will be returned.
    """
    if isinstance(data, u.Quantity):
        data = data.value
    # Step 1: identify the patch of 3x3 pixels (z_)
    # that is centered on the brightest pixel (xx, yy)
    if mask is not None:
        data = data * mask
    arg_data_max = np.nanargmax(data)
    yy, xx = np.unravel_index(arg_data_max, data.shape)
    # Make sure the 3x3 patch does not leave the TPF bounds
    if yy < 1:
        yy = 1
    if xx < 1:
        xx = 1
    if yy > (data.shape[0] - 2):
        yy = data.shape[0] - 2
    if xx > (data.shape[1] - 2):
        xx = data.shape[1] - 2

    z_ = data[yy - 1 : yy + 2, xx - 1 : xx + 2]

    # Next, we will fit the coefficients of the bivariate quadratic with the
    # help of a design matrix (A) as defined by Eqn 20 in Vakili & Hogg
    # (arxiv:1610.05873). The design matrix contains a
    # column of ones followed by pixel coordinates: x, y, x**2, xy, y**2.
    A = np.array(
        [
            [1, -1, -1, 1, 1, 1],
            [1, 0, -1, 0, 0, 1],
            [1, 1, -1, 1, -1, 1],
            [1, -1, 0, 1, 0, 0],
            [1, 0, 0, 0, 0, 0],
            [1, 1, 0, 1, 0, 0],
            [1, -1, 1, 1, -1, 1],
            [1, 0, 1, 0, 0, 1],
            [1, 1, 1, 1, 1, 1],
        ]
    )
    # We also pre-compute $(A^t A)^-1 A^t$, cf. Eqn 21 in Vakili & Hogg.
    At = A.transpose()
    # In Python 3 this can become `Aprime = np.linalg.inv(At @ A) @ At`
    Aprime = np.matmul(np.linalg.inv(np.matmul(At, A)), At)

    # Step 2: fit the polynomial $P = a + bx + cy + dx^2 + exy + fy^2$
    # following Equation 21 in Vakili & Hogg.
    # In Python 3 this can become `Aprime @ z_.flatten()`
    a, b, c, d, e, f = np.matmul(Aprime, z_.flatten())

    # Step 3: analytically find the function maximum,
    # following https://en.wikipedia.org/wiki/Quadratic_function
    det = 4 * d * f - e ** 2
    if abs(det) < 1e-6:
        return np.nan, np.nan  # No solution
    xm = -(2 * f * b - c * e) / det
    ym = -(2 * d * c - b * e) / det
    return xx + xm, yy + ym

def _query_solar_system_objects(
    ra, dec, times, radius=0.1, location="kepler", cache=True, show_progress=True
):
    """Returns a list of asteroids/comets given a position and time.

    This function relies on The Virtual Observatory Sky Body Tracker (SkyBot)
    service which can be found at http://vo.imcce.fr/webservices/skybot/

    Parameters
    ----------
    ra : float
        Right Ascension in degrees.
    dec : float
        Declination in degrees.
    times : array of float
        Times in Julian Date.
    radius : float
        Search radius in degrees.
    location : str
        Spacecraft location. Options include `'kepler'` and `'tess'`.
    cache : bool
        Whether to cache the search result. Default is True.
    show_progress : bool
        Whether to display a progress bar during the download. Default is True.

    Returns
    -------
    result : `pandas.DataFrame`
        DataFrame containing the list of known solar system objects at the
        requested time and location.
    """
    # We import pandas locally, because it takes quite a bit of time to import,
    # and it is only required for this specific feature.
    import pandas as pd

    if (location.lower() == "kepler") or (location.lower() == "k2"):
        location = "C55"
    elif location.lower() == "tess":
        location = "C57"

    url = "http://vo.imcce.fr/webservices/skybot/skybotconesearch_query.php?"
    url += "-mime=text&"
    url += "-ra={}&".format(ra)
    url += "-dec={}&".format(dec)
    url += "-bd={}&".format(radius)
    url += "-loc={}&".format(location)

    df = None
    times = np.atleast_1d(times)
    for time in tqdm(times, desc="Querying for SSOs", disable=~show_progress):
        url_queried = url + "EPOCH={}".format(time)
        response = download_file(url_queried, cache=cache, show_progress=show_progress)
        if open(response).read(10) == "# Flag: -1":  # error code detected?
            raise IOError(
                "SkyBot Solar System query failed.\n"
                "URL used:\n" + url_queried + "\n"
                "Response received:\n" + open(response).read()
            )
        res = pd.read_csv(response, delimiter="|", skiprows=2)
        if len(res) > 0:
            res["epoch"] = time
            res.rename(
                {"# Num ": "Num", " Name ": "Name", " Class ": "Class", " Mv ": "Mv"},
                inplace=True,
                axis="columns",
            )
            res = res[["Num", "Name", "Class", "Mv", "epoch"]].reset_index(drop=True)
            if df is None:
                df = res
            else:
                df = pd.concat([df, res])
    if df is not None:
        df.reset_index(drop=True)
    return df

def show_citation_instructions():
    """Show citation instructions."""
    from . import PACKAGEDIR, __citation__

    # To make installing Lightkurve easier, ipython is an optional dependency,
    # because we can assume it is installed when notebook-specific features are called
    try:
        from IPython.display import HTML

        ipython_installed = True
    except ModuleNotFoundError:
        ipython_installed = False

    if not is_notebook() or not ipython_installed:
        print(__citation__)
    else:
        from pathlib import Path  # local import to speed up `import lightkurve`
        import astroquery  # local import to speed up `import lightkurve`

        templatefile = Path(PACKAGEDIR, "data", "show_citation_instructions.html")
        template = open(templatefile, "r").read()
        template = template.replace("LIGHTKURVE_CITATION", __citation__)
        template = template.replace("ASTROPY_CITATION", astropy.__citation__)
        template = template.replace("ASTROQUERY_CITATION", astroquery.__citation__)
        return HTML(template)

def _get_notebook_environment():
    """Returns 'jupyter', 'colab', or 'terminal'.

    One can detect whether or not a piece of Python is running by executing
    `get_ipython().__class__`, which returns the following result:

        * Jupyter notebook: `ipykernel.zmqshell.ZMQInteractiveShell`
        * Google colab: `google.colab._shell.Shell`
        * IPython terminal: `IPython.terminal.interactiveshell.TerminalInteractiveShell`
        * Python terminal: `NameError: name 'get_ipython' is not defined`
    """
    try:
        ipy = str(type(get_ipython())).lower()
        if "zmqshell" in ipy:
            return "jupyter"
        if "colab" in ipy:
            return "colab"
    except NameError:
        pass  # get_ipython() is not a builtin
    return "terminal"

def is_notebook():
    """Returns `True` if we are running in a notebook."""
    return _get_notebook_environment() in ["jupyter", "colab"]

def remote_jupyter_proxy_url(port):
    """
    Callable to configure Bokeh's show method when a proxy must be
    configured.    If port is None we're asking about the URL
    for the origin header.
    """
    base_url = os.environ['LK_JUPYTERHUB_EXTERNAL_URL']
    host = urllib.parse.urlparse(base_url).netloc

    # If port is None we're asking for the URL origin
    # so return the public hostname.
    if port is None:
        return host

    service_url_path = os.environ['JUPYTERHUB_SERVICE_PREFIX']
    proxy_url_path = 'proxy/%d' % port

    user_url = urllib.parse.urljoin(base_url, service_url_path)
    full_url = urllib.parse.urljoin(user_url, proxy_url_path)
    return full_url

def finalize_notebook_url(notebook_url):
    """Based on `notebook_url` and the environment, compute a final value for
    notebook_url to be passed on to bokeh enabling transparent operation on JupyterHub.

    See Bokeh instructions here:
    https://docs.bokeh.org/en/latest/docs/user_guide/output/jupyter.html

    This handles two aspects of Bokeh made tricky by JupyterHub, firstly
    accessing the random Bokeh server port while behind a proxy, and second not
    triggering CORS restrictions while accessing a second server.

    A key aspect of the computed URL is the externally visible DNS name of the
    JupyterHub, so for the case of TIKE we might have:

    export LK_JUPYTERHUB_EXTERNAL_URL="https://timeseries.science.stsci.edu"

    If LK_JUPYTERHUB_EXTERNAL_URL is implicitly defined by the hub environment,
    JupyterHub users can nominally ignore the notebook_url parameter and
    Lightkurve should "just work" as if the local default URL localhost:8888
    was sufficient.

    For example remote_jupyter_proxy_url(25346) would return the URL
    "https://test.timeseries.science.stsci.edu/user/homer@stsci.edu/proxy/24356"

    which is essentially HUB + USER_SESSION + BOKEH_PORT_IN_SESSION

    The function result should be identical to past behavior unless the definition
    of LK_JUPYTERHUB_EXTERNAL_URL indicates JupyterHub is in use.  In this case the
    use of remote_jupyter_proxy_url is activated.   This effectively makes it the
    JupyterHub default instead of localhost:8888.
    """
    if notebook_url is not None:
        return notebook_url
    elif os.environ.get("LK_JUPYTERHUB_EXTERNAL_URL"):
        return remote_jupyter_proxy_url
    else:
        return "localhost:8888"


from django.http import Http404
from blog.models import Blogs,Blogs1
from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse

def home(request):
    all_blog = Blogs.objects.all()
    #return HttpResponse("Hello,welcome to bLOGGER")
    return render(request,'blog/home.html',{
        'all_blogs' : all_blog
    })

def details(request, blog_id):
    try:
        all_blog1 = Blogs.objects.get(pk =blog_id)
        blog_id1 =blog_id
    except Blogs.Does.Not.Exit:
        raise Http404("Blogs does not exist")
    return render(request,'blog/details.html',{
         'all_blog1' :all_blog1 ,'blog_id1' :blog_id1
        #'all_blog1' : Blogs.objects.filter(id =blog_id)
    })

def favorite(request,blog_id):
    all_blog1 =get_object_or_404(Blogs, pk =blog_id)
    blog_id1 =blog_id
    try:

        selected_blog =all_blog1.blogs1_set.get(pk =request.POST['x'])
    except(KeyError):
    #except(KeyError,blog1.Does.Not.Exist):
        return render(request,'blog/details.html',{
            'all_blog1': all_blog1 ,'blog_id1' :blog_id1,'error_message' : "you did not select a valid blog"
        })
    else:
        selected_blog.is_favorite =True
        selected_blog.save()
    return render(request,'blog/details.html',{
        'all_blog1': all_blog1 ,'blog_id1' :blog_id1
            })

# Create your views here.


"""
compute simlarity of inferred vectors between all neurosynth datasets
"""

import os,pickle
import numpy,pandas
from joblib import Parallel, delayed,load,dump

def vector_corr(i,data):
    """
    find the real image that most closely matches the index image
    ala Kay et al., 2008
    """
    corr_all=numpy.zeros(data.shape[0])
    for j in range(i,data.shape[0]):
        corr_all[j]=numpy.corrcoef(data[i,:],data[j,:])[0,1]
    return corr_all

data=pandas.read_csv('../data/neurosynth/ns_doc2vec_300dims_projection.csv',index_col=0)
data=data.values

testmode=False

if testmode:
    data=data[:5,:]

dump(data,'ns_vectors.mm')

data = load('ns_vectors.mm', mmap_mode='r')

results=Parallel(n_jobs=24)(delayed(vector_corr)(i,data) for i in range(data.shape[0]))

pickle.dump(results,open('../data/neurosynth/ns_vector_similarity_results.pkl','wb'))

os.remove('ns_vectors.mm')


from keras_retinanet.models import load_model
from keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image
import cv2
import os
import time
import matplotlib.pyplot as plt
from keras_retinanet.utils.visualization import *

def evaluate(modelPath, config):
    inferenceModel = load_model(modelPath, backbone_name=config.backbone)

    for i in range(1169, 1183):

        labels_to_names = {0: 'person', 1: 'bicycle', 2: 'car'}
        image = read_image_bgr(os.path.join("..", "Data", "images", "set00-V000-" + str(i) + ".jpg"))
        draw = image.copy()
        draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

        # preprocess image for network
        image = preprocess_image(image)
        image, scale = resize_image(image)

        # process image
        start = time.time()
        boxes, scores, labels = inferenceModel.predict_on_batch(np.expand_dims(image, axis=0))
        print("processing time: ", time.time() - start)
        print(sum(sum(boxes)))
        # correct for image scale
        boxes /= scale
        print(scores[0])
        # visualize detections
        for box, score, label in zip(boxes[0], scores[0], labels[0]):
            # scores are sorted so we can break
            print(score)
            if score < 0.5:
                break

            color = label_color(label)

            b = box.astype(int)
            draw_box(draw, b, color=color)

            caption = "{} {:.3f}".format(labels_to_names[label], score)
            draw_caption(draw, b, caption)

        plt.figure(figsize=(15, 15))
        plt.axis('off')
        plt.imshow(draw)
        plt.show()


import os
import glob
import sys
import time
import argparse
from manuscript_reader import DocxManuscriptReader, TxtManuscriptReader, CsvManuscriptReader
from manuscript_writer import DocxManuscriptWriter, TxtManuscriptWriter
from gpt_api_handler import GPTAPIHandler
from openai.error import RateLimitError  # Import RateLimitError

def read_prompt_file(prompt_file):
    with open(prompt_file, 'r') as file:
        return file.read()

def get_reader(file_path, start_text):
    if file_path.endswith(".docx"):
        return DocxManuscriptReader(file_path, start_text=start_text)
    elif file_path.endswith(".txt"):
        return TxtManuscriptReader(file_path, start_text=start_text)
    elif file_path.endswith(".csv"):
        return CsvManuscriptReader(file_path, start_text=start_text)
    else:
        raise ValueError(f"Unsupported file type for {file_path}")

def get_writer(file_path):
    if file_path.endswith(".docx"):
        return DocxManuscriptWriter(file_path)
    elif file_path.endswith(".txt"):
        return TxtManuscriptWriter(file_path)
    elif file_path.endswith(".csv"):
        return TxtManuscriptWriter(file_path)  # Assuming writing CSV as plain text for now
    else:
        raise ValueError(f"Unsupported file type for {file_path}")

def main(input_directory, output_directory, prompt_file, start_text=None):
    api_key = os.environ.get('OPENAI_API_KEY')
    if not api_key:
        print("Error: OPENAI_API_KEY environment variable not set.")
        sys.exit(1)

    # Read the content of the prompt file
    with open(prompt_file, 'r') as file:
        prompt = file.read()

    # Get a list of all files in the input directory
    input_files = glob.glob(os.path.join(input_directory, "*"))

    # Create a file to store all chunks
    with open('temp_output.txt', 'w+') as temp_file:
        for input_file in input_files:
            # Determine the output file path
            base_name = os.path.basename(input_file)
            output_file = os.path.join(output_directory, base_name)

            # Determine the appropriate reader and writer based on the file extension
            if input_file.lower().endswith(".docx"):
                reader = DocxManuscriptReader(input_file, start_text=start_text)
                writer = DocxManuscriptWriter(output_file)
            elif input_file.lower().endswith(".txt"):
                reader = TxtManuscriptReader(input_file, start_text=start_text)
                writer = TxtManuscriptWriter(output_file)
            elif input_file.lower().endswith(".csv"):
                reader = CsvManuscriptReader(input_file, start_text=start_text)
                writer = TxtManuscriptWriter(output_file)
            else:
                print(f"Skipping file {input_file} with unrecognized extension.")
                continue

            gpt_api = GPTAPIHandler(prompt, api_key)

            chunks = list(reader.get_chunks())
            total_chunks = len(chunks)

            for i, chunk in enumerate(chunks):
                print(f"Processing chunk {i+1} of {total_chunks} for file {input_file}")
                print(f"Sending text to GPT API: {chunk[:100]}...")

                # Modified part
                while True:
                    try:
                        edited_text = gpt_api.get_edited_text(chunk)
                        break  # Breaks the loop if the API call was successful
                    except Exception as e:
                        print(f"Error occurred: {str(e)}. Retrying after 60 seconds.")
                        time.sleep(60)  # Wait for 60 seconds before trying again

                print(f"Received edited text: {edited_text[:100]}...")

                # Write edited text to temp file
                temp_file.write(edited_text + "\n")
                temp_file.flush()  # Flush the buffer

                writer.add_paragraph(edited_text)

            # Save the document after all chunks have been processed
            writer.save()

            print(f"Edited manuscript for file {input_file} saved to {output_file}")

        print(f"All edited texts have been recorded in the file: {temp_file.name}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Manuscript Editor")
    parser.add_argument("input_directory", help="Input manuscript directory path")
    parser.add_argument("output_directory", help="Output edited manuscript directory path")
    parser.add_argument("prompt_file", help="Prompt file path")
    parser.add_argument("--start_text", help="Optional start text to process the input manuscript from", default=None)

    args = parser.parse_args()

    main(args.input_directory, args.output_directory, args.prompt_file, args.start_text)