import pandas as pd
import os
import json
import numpy as np

import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../..")))
import streamlit as st
import plotly.express as px
from streamlit_plotly_events import plotly_events
import plotly.graph_objects as go  # Import Plotly graph objects
from ml.src.utils.helper import render_svg
from ml.src.pipeline.constants import OUTPUT_DIR_PATH

MOST_RECENT = 0
TARGET_RUN = "2023-10-18_22-36-10" #"2023-10-14_16-38-47_all_post_ml_pipeline"

st.set_page_config(
    layout="wide",
)

def blank_to_underscore(x):
    return x.replace(' ', '_')

rename = {
    'hitcall': 'hitcall',
    'hitcall_c': 'hitcall (c)',
    'True': 'positive',
    'False': 'negative',
    'macro avg': 'macro avg',
    'weighted avg': 'weighted avg',
    'val': 'Internal validation',
    'mb_val_structure': 'MB validation from structure',
    'mb_val_sirius': 'MB validation SIRIUS-predicted',
    'Internal validation': 'Internal',
    'MB validation from structure': 'MB structure',
    'MB validation SIRIUS-predicted': 'MB SIRIUS',
    'default': 'default=0.5',
    'tpr': 'TPR≈0.5',
    'tnr': 'TNR≈0.5',
    'optimal': 'cost(TPR,TNR)',
    'XGBClassifier': 'XGBoost',
    'XGBoost': 'XGB',
    'RF': 'RF',
    'RBF SVM': 'SVM',
    'MLP': 'MLP',
    'LR': 'LR',
    'RandomForestClassifier': 'RF',
    'LogisticRegression': 'LR',
    'SVC': 'RBF SVM',
    'MLPClassifier': 'MLP',
    'accuracy': 'accuracy',
}

reverse_rename = {v: k for k, v in rename.items()}

logs_folder = os.path.join(OUTPUT_DIR_PATH)
run_folder = st.sidebar.selectbox('Select Run', [run_folder for run_folder in os.listdir(logs_folder)][::-1])
folder = os.path.join(logs_folder, run_folder)

# Load the JSON data from the output files
with open(os.path.join(folder, 'aeid_paths.json'), 'r') as fp:
    aeid_paths = json.load(fp)

with open(os.path.join(folder, 'model_paths.json'), 'r') as fp:
    model_paths = json.load(fp)

with open(os.path.join(folder, 'feature_importances_paths.json'), 'r') as fp:
    feature_importances_paths = json.load(fp)

with open(os.path.join(folder, 'validation_results.json'), 'r') as fp:
    validation_results = json.load(fp)

with open(os.path.join(folder, 'validation_results_scores.json'), 'r') as fp:
    validation_results_scores = json.load(fp)

selected_target_variable = st.sidebar.selectbox('Select Target Variable', list(validation_results.keys()))
selected_ml_algorithm = st.sidebar.selectbox('Select ML Algorithm', list(validation_results[selected_target_variable].keys()))
dummy_aeid = str(list(aeid_paths[selected_target_variable][selected_ml_algorithm].keys())[0])
selected_preprocessing_model = st.sidebar.selectbox('Select Feature Selection Model', list(validation_results[selected_target_variable][selected_ml_algorithm][dummy_aeid].keys())[::-1])
selected_validation_type = st.sidebar.selectbox('Select Validation Set', ['Internal validation', 'MB validation from structure', 'MB validation SIRIUS-predicted'])
selected_threshold = st.sidebar.selectbox('Select Classification Threshold', ['default=0.5', 'cost(TPR,TNR)', 'TPR≈0.5', 'TNR≈0.5'])
class_metrics = ['macro avg', 'weighted avg', 'positive', 'negative',  'accuracy']
support_class_metrics = ['macro avg', 'positive', 'negative']
selected_class_metric = st.sidebar.selectbox('Select Class Slice', class_metrics)
st.sidebar.divider()
trendline_type = st.sidebar.selectbox('Select Trendline Type', [None, 'ols', 'lowess'])
marginal_type = st.sidebar.selectbox('Select Marginal Type', ['box', 'histogram', 'violin', 'rug', None])
selected_marker_size = st.sidebar.selectbox('Select Marker Size based on', ['Higher Imbalance = LARGER', 'Total Support', 'Support Positive', 'Support Negative', None])

color_palette_mapping = {
    'Light24': px.colors.qualitative.Light24,
    'Plotly': px.colors.qualitative.Plotly,
    'Safe': px.colors.qualitative.Safe,
    'Bold': px.colors.qualitative.Bold,
    'D3': px.colors.qualitative.D3,
    'G10': px.colors.qualitative.G10,
}

with st.sidebar.expander('Settings', expanded=False):
    marker_symbol = st.sidebar.select_slider('Select marker symbol',
                                              options=['circle-dot','circle-open-dot',
                                                       'x-thin', 'x',
                                                       'circle-x', 'circle-x-open',
                                                        'cross-thin', 'asterisk'])
    opacity = st.sidebar.slider('Opacity', min_value=0.0, max_value=1.0, value=1.0, step=0.1)
    marker_line_width = st.sidebar.slider('Marker Line Width', min_value=0.3, max_value=2.0, value=0.4, step=0.1)
    marker_line_color = st.sidebar.checkbox('Dark Marker Line', value='#000000')

    selected_palette = st.sidebar.selectbox('Select Color Palette', list(color_palette_mapping.keys()))
    selected_palette = color_palette_mapping.get(selected_palette)
    show_summary = st.sidebar.checkbox("Show Summary", value=True)
    group_by_estimator = st.sidebar.checkbox("Group By Estimator", value=True)
    clip_axis = st.sidebar.checkbox("Clip Axis", value=True)
    autoscale = st.sidebar.checkbox("Autoscale", value=True)
    save_figure = st.sidebar.checkbox("Save Figure", value=False)
    show_latex = st.sidebar.checkbox("Generate LaTeX", value=False)
    hover_event = st.sidebar.checkbox("Enable Hover Event", value=False)

reports = {}
for estimator_model in validation_results[selected_target_variable][selected_ml_algorithm][dummy_aeid][selected_preprocessing_model].keys():
    reports[estimator_model] = {}
    for aeid in validation_results[selected_target_variable][selected_ml_algorithm].keys():
        reports[estimator_model][aeid] = {}
        for metric in validation_results[selected_target_variable][selected_ml_algorithm][aeid][selected_preprocessing_model][estimator_model][reverse_rename[selected_validation_type]][reverse_rename[selected_threshold]][reverse_rename[selected_class_metric]].keys():
            value = validation_results[selected_target_variable][selected_ml_algorithm][aeid][selected_preprocessing_model][estimator_model][reverse_rename[selected_validation_type]][reverse_rename[selected_threshold]][reverse_rename[selected_class_metric]][metric]
            reports[estimator_model][aeid][metric] = value
        for metric_class in support_class_metrics:
            new_metric_class = f"{metric_class}_support"
            value = validation_results[selected_target_variable][selected_ml_algorithm][aeid][selected_preprocessing_model][estimator_model][reverse_rename[selected_validation_type]][reverse_rename[selected_threshold]][reverse_rename[metric_class]]['support']
            reports[estimator_model][aeid][new_metric_class] = value

        accuracy = validation_results[selected_target_variable][selected_ml_algorithm][aeid][selected_preprocessing_model][estimator_model][reverse_rename[selected_validation_type]][reverse_rename[selected_threshold]][reverse_rename['accuracy']]['precision'] # for all the same, just take the first one
        reports[estimator_model][aeid]['accuracy'] = accuracy

        roc_auc = validation_results_scores[selected_target_variable][selected_ml_algorithm][aeid][selected_preprocessing_model][estimator_model][reverse_rename[selected_validation_type]]['roc_auc']
        reports[estimator_model][aeid]['roc_auc'] = roc_auc
        pr_auc = validation_results_scores[selected_target_variable][selected_ml_algorithm][aeid][selected_preprocessing_model][estimator_model][reverse_rename[selected_validation_type]]['pr_auc']
        reports[estimator_model][aeid]['pr_auc'] = pr_auc
        balanced_accuracy = validation_results_scores[selected_target_variable][selected_ml_algorithm][aeid][selected_preprocessing_model][estimator_model][reverse_rename[selected_validation_type]]['balanced_accuracy']
        reports[estimator_model][aeid]['balanced_accuracy'] = balanced_accuracy

df = pd.DataFrame(reports)
df = df.stack().reset_index()
df = df.rename(columns={'level_0': 'aeid', 'level_1': 'Estimator', 'level_2': 'Metric', 0: 'Value'})

df['Target Variable'] = rename[selected_target_variable]
df['Feature Selection'] = rename[selected_preprocessing_model.split('_')[2]]
df['Validation Set'] = rename[selected_validation_type]

df['Estimator'] = df['Estimator'].apply(lambda x: rename[x])

df['Precision'] = df['Value'].apply(lambda x: x['precision'])
df['Recall'] = df['Value'].apply(lambda x: x['recall'])
df['F1'] = df['Value'].apply(lambda x: x['f1-score'])
df['Accuracy'] = df['Value'].apply(lambda x: x['accuracy'])

df['ROC AUC'] = df['Value'].apply(lambda x: x['roc_auc'])
df['PR AUC'] = df['Value'].apply(lambda x: x['pr_auc'])
df['Balanced Accuracy'] = df['Value'].apply(lambda x: x['balanced_accuracy'])

df['Total Support'] = df['Value'].apply(lambda x: x['macro avg_support'])
df['Support Positive'] = df['Value'].apply(lambda x: x['positive_support'])
df['Support Negative'] = df['Value'].apply(lambda x: x['negative_support'])

df['Imbalance'] = df['Support Negative'] - df['Support Positive']
df['Imbalance Score'] = (df['Support Positive'] - df['Support Negative']) / df['Total Support']
df['Imbalance Score'] = df['Imbalance Score'] / df['Imbalance Score'].abs().max()
df['Imbalance Score'] = df['Imbalance Score'].round(3)

df = df.drop(columns=['Value'])

if selected_marker_size == 'Total Support':
    df['Marker Size'] = (df['Total Support'] ** 0.42)
elif selected_marker_size == 'Support Positive':
    df['Marker Size'] = (df['Support Positive'] ** 0.55)
elif selected_marker_size == 'Support Negative':
    df['Marker Size'] = (df['Support Negative'] ** 0.45)
elif selected_marker_size == 'Higher Imbalance = LARGER':
    df['Marker Size'] = np.abs(df['Imbalance Score']) * 15
    # sort df['Support'] with the same sort key

    ratio = df['Support Positive'] ** 0.2 / df['Support Negative'] ** 0.2
    # Normalize the ratio values to control marker size
    min_ratio = min(ratio)
    max_ratio = max(ratio)
    normalized_ratio = (ratio - min_ratio) / (max_ratio - min_ratio)
    # sort
    # sorted_normaliezd_ratio = sorted(normalized_ratio)
    # st.line_chart(sorted_normaliezd_ratio)
    # Define a marker size range (you can adjust this as needed)
    min_marker_size = 4
    max_marker_size = 15

    # make new figure for marker size distribution, simple line plot with px

    # Calculate marker sizes based on the normalized ratio
    marker_sizes = min_marker_size + normalized_ratio * (max_marker_size - min_marker_size)
else:
    df['Marker Size'] = 10

df['Marker Size'] = df['Marker Size'].round(3)

trendline_args = {'trendline': trendline_type} if trendline_type is not None else {}
hisogram_args = {'marginal_x': marginal_type, 'marginal_y': marginal_type} if marginal_type is not None else {}

args = {'opacity': opacity,
        'hover_data': ['Target Variable', 'aeid', 'Feature Selection', 'Estimator', 'Validation Set', 'Precision', 'Recall', 'Accuracy', 'Balanced Accuracy', 'F1', 'ROC AUC', 'PR AUC',
                       'Total Support', 'Support Positive', 'Support Negative',
                       'Imbalance', 'Imbalance Score', 'Marker Size'],
        'color_discrete_sequence': selected_palette,
        'custom_data': ['Marker Size'], # Add customdata to match marker size
        }

# Scatter plot
fig = px.scatter(df, x='Recall', y='Precision', color='Estimator', **args, **trendline_args, **hisogram_args)

# Update the marker size based on the customdata
if trendline_type is None:
    for j, trace in enumerate(fig.data):
        if 'scatter' in trace.type:
            # Get the marker size from the customdata
            marker_size = list(fig.data[j].customdata[:, 0])
            marker_line_color_ = 'black' if marker_line_color else None
            marker_line_width_ = marker_line_width if marker_line_color else None
            fig.data[j].update(marker=dict(
                symbol=marker_symbol, #'circle-open-dot',
                size=marker_size,
                line=dict(color=marker_line_color_, width=marker_line_width_)
                )
            )

# drop the dummy values again
# df = df[df['aeid'] != '-1']
offset = 0.01
min_x = np.min(df['Recall']) - offset
max_x = np.max(df['Recall']) + offset
min_y = np.min(df['Precision']) - offset
max_y = np.max(df['Precision']) + offset

if autoscale:
    fig.update_layout(autosize=True)
else:
    if clip_axis:
        fig.update_layout(xaxis=dict(range=[min_x, max_x]))
        fig.update_layout(yaxis=dict(range=[min_y, max_y]))
    else:
        fig.update_layout(xaxis=dict(range=[0.0, 1.0]))
        fig.update_layout(yaxis=dict(range=[0.0, 1.0]))

title = f"y={rename[selected_target_variable]} | FS={rename[selected_preprocessing_model.split('_')[2]]} | {selected_validation_type} | {selected_threshold} | {selected_class_metric}"
margin = None # 0
axis_font_size = 20.5
fig.update_layout(width=1000, height=1000, title=title, title_font=dict(size=21, color='black'), xaxis_title_font=dict(size=axis_font_size, color="black"), yaxis_title_font=dict(size=axis_font_size, color="black"),  margin=dict(t=margin))
fig.update_layout(yaxis=dict(showgrid=True, zeroline=True, gridcolor='lightgray'), xaxis=dict(showgrid=True, zeroline=True, gridcolor='lightgray'))
fig.update_xaxes(tickfont=dict(size=axis_font_size-2, color="black"))
fig.update_yaxes(tickfont=dict(size=axis_font_size-2, color="black"))

# legend
fig.update_layout(legend_title_text='', legend_traceorder="reversed")
fig.update_layout(legend=dict(orientation='v', yanchor='top',  xanchor='left',
                                y=1.01,
                                x=0.743,
                                bgcolor='rgba(255, 255, 255, 0.6)',
                                font=dict(size=31, color='black')))

selected_points = plotly_events(fig, click_event=True, hover_event=hover_event, key="plotly_event") # click_event=True, hover_event=True
if selected_points:
    info = selected_points[0]
    curve_number = info['curveNumber']
    point_index = info['pointIndex']
    estimator_clicked = fig.data[curve_number].legendgroup
    aeid_clicked = fig.data[curve_number].customdata[point_index, 2]

    # Load custom data
    aeid_path = aeid_paths[selected_target_variable][selected_ml_algorithm][aeid_clicked]
    cm_path = os.path.join(aeid_path, selected_preprocessing_model, reverse_rename[estimator_clicked], reverse_rename[selected_validation_type], f'cm_{reverse_rename[selected_threshold]}.svg')

    # Load feature importances:
    if estimator_clicked in ['XGBoost', 'RF']:
        # tod change path when feature importances are available in correct folder, currently in sirius folder
        feature_importances_path = os.path.join(aeid_path, selected_preprocessing_model, reverse_rename[estimator_clicked], 'mb_val_sirius', f'sorted_feature_importances.csv')
        feature_importances = pd.read_csv(feature_importances_path)
        feature_importances = feature_importances.sort_values(by=['feature'], ascending=True).reset_index(drop=True)
        feature_importances = feature_importances.rename(columns={'feature': 'Abs. Feature Index', 'importances': 'Feature Importance'}).reset_index().rename(columns={'index': 'Rel. Feature Index'})
        fig_feature_importances = px.bar(feature_importances, x='Rel. Feature Index', y='Feature Importance', hover_data=['Abs. Feature Index'])

        fig_feature_importances.update_layout(title=f"Feature Importances on selected_preprocessing_model (independent of validation set, threshold and metric)", title_font=dict(size=14, color='black'))

    with st.expander(f'Infos for {estimator_clicked} on assay endpoint with aeid={aeid_clicked}', expanded=True):
        st.divider()
        render_svg(open(cm_path).read())
        st.divider()
        if estimator_clicked in ['XGBoost', 'RF']:
            st.plotly_chart(fig_feature_importances)
        else:
            st.write(f"Feature importances not available for {estimator_clicked}, only for XGBoost and RF")

parent_folder = os.path.dirname(os.path.abspath(__file__))
full_name = f"{selected_target_variable}_" \
            f"{selected_ml_algorithm}_" \
            f"{selected_preprocessing_model}_" \
            f"{reverse_rename[selected_validation_type]}_" \
            f"{reverse_rename[selected_threshold]}_" \
            f"{blank_to_underscore(reverse_rename[selected_class_metric])}"
if save_figure:
    file = f"{full_name}.png"
    dest_path = os.path.join(parent_folder, 'generated_results', file)
    os.makedirs(os.path.dirname(dest_path), exist_ok=True)
    fig.write_image(dest_path, format='png', engine='kaleido', scale=1)

if show_summary:
    st.subheader('Median performance metrics across 345 target assay endpoints')
    if group_by_estimator:
        grouped = df[['Estimator', 'Precision', 'Recall',  'Accuracy', 'Balanced Accuracy', 'F1', 'ROC AUC', 'PR AUC']].groupby(['Estimator']).median().reset_index()
    else:
        grouped = df[['Target Variable', 'aeid', 'Feature Selection', 'Estimator', 'Validation Set', 'Precision', 'Recall',  'Accuracy', 'Balanced Accuracy', 'F1', 'ROC AUC', 'PR AUC']].groupby(['Target Variable', 'aeid', 'Feature Selection', 'Estimator', 'Validation Set']).median().reset_index()

    grouped['Accuracy'] = grouped['Accuracy'].apply(lambda x: f'{x:.3f}')
    grouped['Precision'] = grouped['Precision'].apply(lambda x: f'{x:.3f}')
    grouped['Recall'] = grouped['Recall'].apply(lambda x: f'{x:.3f}')
    grouped['F1'] = grouped['F1'].apply(lambda x: f'{x:.3f}')

    grouped['ROC AUC'] = grouped['ROC AUC'].apply(lambda x: f'{x:.3f}')
    grouped['PR AUC'] = grouped['PR AUC'].apply(lambda x: f'{x:.3f}')
    grouped['Balanced Accuracy'] = grouped['Balanced Accuracy'].apply(lambda x: f'{x:.3f}')

    if group_by_estimator:
        summary = grouped[['Estimator', 'Precision', 'Recall', 'F1', 'Accuracy', 'Balanced Accuracy', 'ROC AUC', 'PR AUC']]
        summary = summary.rename(columns={'Estimator': 'Estimator', 'Precision': 'Precision', 'Recall': 'Recall', 'F1': 'F1', 'Accuracy': 'Acc.', 'Balanced Accuracy': 'Bal. Acc.', 'ROC AUC': 'ROC-AUC', 'PR AUC': 'PR-AUC'})
    else:
        summary = grouped[['Target Variable', 'aeid', 'Feature Selection', 'Estimator', 'Validation Set', 'Precision', 'Recall',  'Accuracy', 'Balanced Accuracy', 'F1', 'ROC AUC', 'PR AUC']]
        summary = summary.rename(columns={'Target Variable': 'y', 'aeid': 'aeid', 'Feature Selection': 'f. s.', 'Estimator': 'model',  'Validation Set': 'val. set', 'Precision': 'prec.', 'rec.': 'Recall', 'F1': 'f1', 'Accuracy': 'acc',  'Balanced Accuracy': 'bacc', 'ROC AUC': 'roc-auc', 'PR AUC': 'pr-auc'})

    st.dataframe(summary, use_container_width=True)
    if save_figure:
        file = f"{full_name}.tex"
        dest_path = os.path.join(parent_folder, 'generated_results', file)

        def pandas_df_to_latex(data, caption='', label=''):
            # Initialize the LaTeX table with the longtable environment
            latex_table = f'\\begin{{longtable}}{{{"l" * len(data.columns)}}}\n'
            latex_table += '\\caption{' + caption + '}\\label{tab:' + label.lower().replace(' ', '_') + '}\\\\\n'
            latex_table += '\\toprule\n\\midrule\n'

            # Extract the header and remove it from the DataFrame
            header = data.columns
            data = data.values

            # Add the header row with \small to the LaTeX table
            latex_table += ' & '.join(['\\small ' + col for col in header]) + '\\\\\n\\hline\n'

            # Iterate through the data rows and add them to the table
            for row in data:
                latex_table += ' & '.join(map(str, row)) + '\\\\\n'

            # Add table footer and replace \endtabular with \endlongtable
            latex_table += '\\bottomrule\n\\end{longtable}'
            latex_table = latex_table.replace('\\endtabular', '\\endlongtable')

            return latex_table

        # latex_table = pandas_df_to_latex(summary, caption="Performance Metrics. See Figure~\ref{" + full_name ".png}.", label=f"table:{full_name}")
        caption = f"Median Performance Metrics belonging to Figure~\\ref" + "{fig:" + full_name + "}."
        latex_table = pandas_df_to_latex(summary, caption=caption, label=f"table:{full_name}")

        with open(dest_path, 'w') as file:
            file.write(latex_table)

        if show_latex:
            with st.expander('LaTeX table string', expanded=True):
                st.latex(latex_table)

# Calculate the ratio between x and y for each data point
single_estimator = df[df['Estimator'] == 'XGBoost'].reset_index(drop=True)
single_estimator = single_estimator.sort_values(by=['Imbalance'])
index = [i for i in range(len(single_estimator))]

# plot Imbalance and support
fig = go.Figure()

# Add the bar plots
fig.add_trace(go.Bar(x=index, y=single_estimator['Support Positive'], name='Positive Support', marker_color='blue'))
fig.add_trace(go.Bar(x=index, y=-single_estimator['Support Negative'], name='Negative Support', marker_color='red'))

# Add the line plots
fig.add_trace(go.Scatter(x=index, y=single_estimator['Imbalance'], mode='lines', name='Imbalance', line=dict(color='black', width=2, dash='dash')))
fig.add_trace(go.Scatter(x=index, y=single_estimator['Total Support'],  mode='lines', name='Total Support', line=dict(color='green', width=2)))

fig.update_layout(
    title=f'Sorted Imbalance and Support Across Target Assay Endpoints: {" ".join(selected_validation_type.split(" ")[:2])}, y={rename[selected_target_variable]}',
    barmode='group',
    yaxis_title='Support',
    xaxis_title='Assay Endpoint Index',
    title_font=dict(size=17, color='black'),
    xaxis_title_font=dict(size=18, color='black'),
    yaxis_title_font=dict(size=18, color='black'),
)
fig.update_xaxes(tickfont=dict(size=16, color="black"))
fig.update_yaxes(tickfont=dict(size=16, color="black"))

# legend
fig.update_layout(legend=dict(orientation='v', yanchor='top',  xanchor='left',
                                # y=1.01,
                                # x=0.743,
                                # bgcolor='rgba(255, 255, 255, 0.6)',
                                font=dict(size=16, color='black')))

st.plotly_chart(fig, use_container_width=True)


import functools
import numpy as np
from functools import wraps
from functools import partial
import pandas as pd
import tqdm
import pickle
from molecular_simply import Process
from rdkit import Chem
from typing import Union
from typing import Tuple
import re
import random
import os

# ===================================
#            some params
# ===================================
ATOM = ['C', 'H', 'O', 'N', 'P', 'S', 'B', 'F', "Cl", "Br", "I", "Si", "Mg", "Ca", "Fe", "As", "Al"]
SMILES_CHAR = ['c', 'o', 'n', 's', 'p', '=', '(', ')', '[', ']', '#', '@', '%', '/', '\\', '+', '-', '0',
               '1', '2', '3', '4', '5', '6', '7', '8', '9']
SEMANTIC = ["<p0>", "<PAD>", "<MASK>", "<SOS>", "<EOS>", "<CLS>", "<UNK>"]
PAT = r"Br|Cl|Si|Mg|Ca|Fe|As|Al|[1234567890]|[CHONPSBFIcons]|[#%@=\\\\/\+\-\(\)\[\]]"
SUPPLEMENTARY = ["<p{}>".format(i) for i in range(1, 20)]
FULL_LIST = SEMANTIC + ATOM + SMILES_CHAR + SUPPLEMENTARY
c2i = {c: idx for idx, c in enumerate(FULL_LIST)}
i2c = {idx: c for c, idx in c2i.items()}

def molecular_initiation(smiles: Union[np.ndarray, list, pd.DataFrame] = None, atom_list: list = None,
                         length_limit: int = 500, smiles_shuffle: bool = False) -> Union[list, pd.DataFrame]:
    p = Process(atom_list=atom_list, re_hydro=True, defn_list='[Na,K,Mg,I,Cl,Br,Ca,Fe,Al,As]', atom_number_limit=120,
                shuffle_atoms=smiles_shuffle)

    def _limit(_smiles: list = None) -> list:
        _limit_list = list()
        for i in _smiles:
            if i is not None and len(i) <= length_limit:
                _limit_list += [i]
            else:
                _limit_list += [None]
        return _limit_list

    if isinstance(smiles, (list, np.ndarray)):
        print("Input SMILEs:", len(smiles))
        returnSMILES = p(smiles)
        returnSMILES = _limit(returnSMILES)
        while True:
            if None in returnSMILES:
                returnSMILES.remove(None)
            else:
                break
        return returnSMILES

    elif isinstance(smiles, pd.DataFrame):
        print("Input SMILEs:", len(smiles))
        smi_list = smiles["Smiles"].values.tolist()
        returnSMILES = p(smi_list)
        returnSMILES = _limit(returnSMILES)
        smiles.Smiles = returnSMILES
        smiles.dropna(subset=["Smiles"], inplace=True)
        smiles.reset_index(drop=True, inplace=True)
        return smiles
    else:
        print("Type error")
        returnSMILES = ValueError

def shuffle_smiles(smiles_: str = None) -> Union[str, None]:
    mol = Chem.MolFromSmiles(smiles_)
    if not mol:
        return None
    else:
        atom_n = mol.GetAtoms()
        atom_index = list(range(len(atom_n)))
        random.shuffle(atom_index)
        shuffle_mol = Chem.RenumberAtoms(mol, atom_index)
        new_smiles = Chem.MolToSmiles(shuffle_mol, canonical=False)
        return new_smiles

def file_io(*args, path, io_class: str = "write") -> Union[None, list, np.ndarray]:
    if io_class == "write":
        with open(path, "wb") as file:
            pickle.dump(args, file)
    else:
        with open(path, "rb") as file:
            load_data = pickle.load(file)
        return load_data

def unsupervised_processing(function):
    @wraps(function)
    def wrapper(data_: list = None,
                p_: functools.partial = None,
                mask: bool = False,
                mask_proportion: float = 0.15,
                encoding_length: int = None,
                save_file_name: str = "data/processed_file.pickle",
                re_make: bool = False) -> Union[Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray], list]:
        save_file_path = "{}".format(save_file_name)
        if (os.path.exists(save_file_path) is False) or (re_make is True):
            print("Input Smiles :", len(data_))
            print("Unsupervised processing ->")
            smiles_ = data_
            # smiles_ = p_(smiles=data_)
            # print("Valid Smiles :", len(smiles_))
            results = tqdm.tqdm(map(lambda la: function(la, mask, mask_proportion), smiles_), total=len(smiles_))
            if bool(encoding_length):
                x, y, w, lth = [], [], [], []

                def completion(seq, length) -> list:
                    seq += [c2i["<PAD>"] for _ in range((length - len(seq)))]
                    assert len(seq) == length, "Completion error:{}".format(seq)
                    return seq

                for r in results:
                    x += [completion(r[0], length=encoding_length)]
                    y += [completion(r[1], length=encoding_length)]
                    w += [completion(r[2], length=encoding_length)]
                    lth.append(r[3])
                x, y, w, lth = np.array(x, dtype=int), np.array(y, dtype=int), \
                    np.array(w, dtype=float), np.array(lth, dtype=int)
                file_io(x, y, w, lth, path=save_file_path)
                return x, y, w, lth
            file_io(results, path=save_file_path)
            return list(results)
        else:
            print("Load file ->")
            return file_io(None, path=save_file_path, io_class="read")

    return wrapper

def char2idx(function):
    @wraps(function)
    def wrapper(smiles_: str = None):
        charList = re.findall(PAT, smiles_)
        encoding_ = function(charList)
        # check encoding
        idx2char_ = [i2c[i] for i in encoding_]
        assert idx2char_ == charList, "Char2idx encoding error!"
        return encoding_
    return wrapper

@char2idx
def char2idx(char_list: list = None) -> list:
    idx = [c2i[i] for i in char_list]
    return idx

def supervised_processing(function):
    @wraps(function)
    def wrapper(data_: list = None,
                p_: functools.partial = None,
                encoding_length: int = None,
                save_file_name: str = "data/supervised_processed_file.pickle",
                re_make: bool = False) -> Union[Tuple[list, np.ndarray, np.ndarray, np.ndarray], list]:
        save_file_path = "{}".format(save_file_name)
        if (os.path.exists(save_file_path) is False) or (re_make is True):
            print("Input Smiles :", len(data_))
            print("Supervised processing ->")
            smiles_ = p_(smiles=data_)
            # return pandas data
            print("Valid Smiles :", len(smiles_))

            # pandas ->
            smiles_data = smiles_["Smiles"].values.tolist()
            y = smiles_["Y"].values.tolist()
            results = tqdm.tqdm(map(lambda la: function(la), smiles_data), total=len(smiles_data))
            if bool(encoding_length):
                x, lth = [], []
                def completion(seq, length) -> list:
                    seq += [c2i["<PAD>"] for _ in range((length - len(seq)))]
                    assert len(seq) == length, "Completion error:{}".format(seq)
                    return seq

                for r in results:
                    x += [completion(r[0], length=encoding_length)]
                    lth.append(r[1])
                x, y, lth = np.array(x, dtype=int), np.array(y, dtype=int), np.array(lth, dtype=int)
                file_io(smiles_data, x, y, lth, path=save_file_path)
                return smiles_data, x, y, lth
            file_io(results, path=save_file_path)
            return list(results)
        else:
            print("Load file ->")
            return file_io(None, path=save_file_path, io_class="read")
    return wrapper

@supervised_processing
def supervised_processing(Smiles_: list = None):
    return_c = char2idx(Smiles_)
    x = return_c + [c2i["<SOS>"]]
    lth = len(return_c)
    return x, lth
# File read   => 无监督学习处理， 有监督处理

@unsupervised_processing
def unsupervised_processing(smiles_: str = None,
                            mask: bool = False,
                            mask_proportion: float = 0.2) -> Tuple[list, list, list, int]:
    # ------> length check
    return_c = char2idx(smiles_)
    y = [c2i["<SOS>"]] + return_c
    if mask:
        encoding_index = list(range(len(return_c)))
        random.shuffle(encoding_index)
        lth = len(return_c)
        w = [0.] * len(return_c)
        random_block = [0, 0, 0, 1, 0, 0, 1, 1]
        for i in encoding_index[:int(len(return_c)*mask_proportion)]:
            k = random.choice(random_block)
            if k == 0:
                return_c[i] = c2i["<UNK>"]
            else:
                return_c[i] = c2i[random.choice(ATOM)]
            w[i] = 1
        x = [c2i["<SOS>"]] + return_c
    else:
        lth = len(return_c)
        x = [c2i["<SOS>"]] + return_c
        w = [1.] * len(x)

    return x, y, w, lth

def gen(dir_):
    file_read = pd.read_csv("split/%s/Dataset_In_MACCS/cyp2c9_train_MACCS.csv" % dir_)
    p = partial(molecular_initiation, atom_list=ATOM, smiles_shuffle=True, length_limit=145)
    supervised_processing(file_read.iloc[:, 0:3], p, encoding_length=150,
                                                 save_file_name="split/%s/Dataset_In_MACCS/2c9_train.pkl" % dir_)

    file_read_ = pd.read_csv("split/%s/Dataset_In_MACCS/cyp2c9_test_MACCS.csv" % dir_)
    p = partial(molecular_initiation, atom_list=ATOM, smiles_shuffle=True, length_limit=145)
    supervised_processing(file_read_.iloc[:, 0:3], p, encoding_length=150,
                                                 save_file_name="split/%s/Dataset_In_MACCS/2c9_test.pkl" % dir_)

    file_read_ = pd.read_csv("split/%s/Dataset_In_MACCS/cyp2c9_valid_MACCS.csv" % dir_)
    p = partial(molecular_initiation, atom_list=ATOM, smiles_shuffle=True, length_limit=145)
    supervised_processing(file_read_.iloc[:, 0:3], p, encoding_length=150,
                                                 save_file_name="split/%s/Dataset_In_MACCS/2c9_valid.pkl" % dir_)

if __name__ == "__main__":
    # 输入SMILES
    # smiles = torch.load("500k_smiles.pth")[-1][:11000]

    # read_file = pd.read_csv("Valid_smiles_32.csv")
    # read_file.dropna(subset=["Smiles"], inplace=True)
    # smiles = read_file.Smiles.values.tolist()
    # valid_smiles = []
    # for i in smiles:
    #     if len(i) <= 145:
    #         valid_smiles += [i]

    # -> 无监督学习编码 -> 直接编码即可 -> 包含一个起始字符 ->  最终填充到一定的长度（填充字符）
    # -> 针对一个SMILES, 抽取20%的数据，使用mask掩码操作，当随机数<0.8, mask掉，当随机数为0.8-0.9之间，随机替换有效的数值，0.9-1不变。
    # -> 随机替换的数据，抽取到的数值，权重为1，抽取到的数据，权重为0,
    # p = partial(molecular_initiation, atom_list=ATOM, smiles_shuffle=True, length_limit=145)
    # rx, ry, rw, rl = unsupervised_processing(valid_smiles, p, mask=True, encoding_length=150,
    #                                          save_file_name="data/pad_test.pkl")
    # print(rx.shape, ry.shape, rw.shape)
    # print(rx[10])
    # print(ry[10])
    # print(rw[10])
    # print(rl[10])

    # Fine-tuning process
    file_read = pd.read_csv("split//cyp2c9_train_MACCS.csv")
    p = partial(molecular_initiation, atom_list=ATOM, smiles_shuffle=True, length_limit=145)
    smiles, x, y, length = supervised_processing(file_read.iloc[:, 0:3], p, encoding_length=150,
                                                 save_file_name="data/2c9_train.pkl")

    file_read_= pd.read_csv("split/cyp2c9_test_MACCS.csv")
    p = partial(molecular_initiation, atom_list=ATOM, smiles_shuffle=True, length_limit=145)
    smiles__, x__, y__, length__ = supervised_processing(file_read_.iloc[:, 0:3], p, encoding_length=150,
                                                         save_file_name="data/2c9_test.pkl")

    file_read_ = pd.read_csv("split/cyp2c9_valid_MACCS.csv")
    p = partial(molecular_initiation, atom_list=ATOM, smiles_shuffle=True, length_limit=145)
    smiles_, x_, y_, length_ = supervised_processing(file_read_.iloc[:, 0:3], p, encoding_length=150,
                                                         save_file_name="data/2c9_valid.pkl")
    # file_read_ = pd.read_csv("data/evalid/cyp3a4_MACCS.csv")
    # p = partial(molecular_initiation, atom_list=ATOM, smiles_shuffle=True, length_limit=145)
    # smiles_, x_, y_, length_ = supervised_processing(file_read_.iloc[:, 0:2], p, encoding_length=150,
    #                                                      save_file_name="data/3a4_evalid.pkl")


from django import forms
from .models import Books

class Bookform(forms.ModelForm):

    class Meta:
        model = Books
        fields = ('title', 'author', 'date_published',
                  'number_of_pages', 'type_of_book')
        labels = {
            'date_published': 'Date Published (Format : yyyy-mm-dd)',
            'number_of_pages': 'Number of Pages',
            'type_of_book': 'Genres'
        }

    def __init__(self, *args, **kwargs):
        super(Bookform, self).__init__(*args, **kwargs)
        self.fields['type_of_book'].empty_label = 'Select'
        self.fields['date_published'].required = 'Select'


import vcf
import os
import random
import argparse

parser = argparse.ArgumentParser(description = 'caculate heterozygous windows', add_help = False, usage = '\npython3 -i [input.vcf] -s [step and windows] -o [output.txt]')
required = parser.add_argument_group()
optional = parser.add_argument_group()
required.add_argument('-i', '--input', metavar = '[input_vcf]', help = 'input.vcf', required = True)
required.add_argument('-o', '--output', metavar = '[output]', help = 'output.txt', required = True)
required.add_argument('-s', '--step', metavar = '[step]', help = 'step',default=10000)
optional.add_argument('-h', '--help', action = 'help', help = 'help')
args = parser.parse_args()

samples_name=args.input[:-4]
#filter heterozygous
seed=random.randint(1,1000000)
vcf_reader0 = vcf.Reader(open(args.input, 'r'))
vcf_writer0 = vcf.Writer(open(args.input+str(seed)+'tmp.vcf', 'w'), vcf_reader0)
for record in vcf_reader0:
    gt=record.genotype(samples_name)['GT']
    if gt=='0|1' or gt=='1|0':
        vcf_writer0.write_record(record)
vcf_writer0.close()
#split chromosome

for i in range(1,10):
    vcf_reader1 = vcf.Reader(open(args.input+str(seed)+'tmp.vcf', 'r'))
    vcf_writer1 = vcf.Writer(open(args.input+str(seed)+'chr'+str(i)+'tmp.vcf', 'w'), vcf_reader1)
    for record in vcf_reader1:
        chr=record.CHROM
        gt=record.genotype(samples_name)['GT']
        #print(gt)
        if chr=='chr'+str(i) and gt=='0|1':
            vcf_writer1.write_record(record)
        if chr=='chr'+str(i) and gt=='1|0':
            vcf_writer1.write_record(record)
    vcf_writer1.close()

#caculate het
out=open(args.output,'w')
win=int(args.step)
step=int(args.step)
L=[32266151,33368013,41283659,32876879,48982865,27141800,31662302,30741539,30315542]
for i in range(1,10):
    for each in range(int((L[i-1])/step+1)):
        chr_test='chr'+str(i)
        start=each*step
        end=start+win
        #print(chr_test,start,end)
        count=0
        vcf_reader2 = vcf.Reader(open(args.input+str(seed)+'chr'+str(i)+'tmp.vcf', 'r'))
        for record in vcf_reader2:
            #print(record)
            chr=record.CHROM
            pos=int(record.POS)
            gt=record.genotype(samples_name)['GT']
            #print(pos)
            if not chr==chr_test:
                pass
            elif chr==chr_test and pos<start:
                pass
            elif chr==chr_test and pos>start and pos<end:
                if gt=='0|1' or gt=='1|0':
                    #print(record)
                    count+=1
            elif chr==chr_test and pos>end:
                break
        ll=chr_test+'\t'+str(start)+'\t'+str(end)+'\t'+str(count/step)+'\n'
        out.write(ll)
        #print(chr_test,start,end,count/10000)
out.close()

#delete tmp files
for i in range(1,10):
    pass
    cmd0='rm -f {}'.format(args.input+str(seed)+'tmp.vcf')
    cmd1='rm -f {}'.format(args.input+str(seed)+'chr'+str(i)+'tmp.vcf')
    os.system(cmd0)
    os.system(cmd1)


import cv2

image = cv2.imread("entrada.jpg")

image[0, 0] = (255, 255, 0)
print(image[0,0])
print(image)

#CRIA UM RETANGULO AZUL DE ALTURA ENTRE 30 E 50 E POR TODA A LARGURA DA IMAGEM
image[30:50, :] = (255, 0, 0);#slicing -> altera vários pixels da imagem de uma vez só

#CRIA UM QUADRADO VERMELHO
image[100:150, 50:100] = (0, 0, 255)

#CRIA UMA RETANGULO AMARELO POR TODA A ALTURA DA IMAGEM
image[:, 200:220] = (0, 255, 255)

#CRIA UM RETANGULO VERDE DA LINHA 150 A 300 NAS COLUNAS 250 A 350
image[150:300, 250:350] = (0, 255, 0)

#CRIA UM QUADRADO CIANO DA LINHA 50 A 150 NAS COLUNAS 300 A 400
image[300:400, 50:150] = (255, 255, 0)

#CRIA UM QUADRADO BRANCO
image[250:350, 300:400] = (255, 255, 255)

#CRIA UM QUADRADO PRETO
image[70: 100, 300: 450] = (0,0,0)

cv2.imshow("Imagem alterada", image) #exibe a imagem após a execução do código acima com os retangulos coloridos incluídos pela técnica "slicing"
cv2.waitKey(0);
cv2.destroyAllWindows();


from scrapy import Request
from selenium import webdriver
from selenium_stealth import stealth
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

def create_web_driver_stealth() -> webdriver.Chrome:
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_argument("--headless")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)

    s = Service(ChromeDriverManager().install())
    stealth_driver = webdriver.Chrome(service=s, options=options)

    stealth(stealth_driver,
            languages=["en-US", "en"],
            vendor="Google Inc.",
            platform="Win32",
            webgl_vendor="Intel Inc.",
            renderer="Intel Iris OpenGL Engine",
            fix_hairline=True,
            )

    return stealth_driver

class SeleniumStealthRequest(Request):
    def __init__(self, timeout: int = 0, page_scroll: bool = False, screenshot: bool = False,
                 *args, **kwargs):
        self.timeout = timeout
        self.page_scroll = page_scroll
        self.screenshot = screenshot

        super().__init__(*args, **kwargs)


import socket
import json

from miner import Miner

SOCKET_READ_LENGTH = 4096

class SgMiner(Miner):

    def _get_socket(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        try:
            sock.connect((self._host, self._port))
        except:
            return { 'error': Miner.DOWN }

        return sock

    def _run_command(self, cmd, expect_multiple = False):
        payload = { 'command': cmd }

        sock = self._get_socket()

        if sock is None:
            return {}

        sock.send(
            json.dumps(payload)
        )

        try:
            resp = sock.recv(SOCKET_READ_LENGTH)

            while True:
                next_resp = sock.recv(SOCKET_READ_LENGTH)

                if not next_resp:
                    break
                else:
                    resp += next_resp
        except:
            return { 'error': Miner.BAD_RESPONSE }

        if expect_multiple:
            return json.loads(resp[:-1])[cmd.upper()]
        else:
            return json.loads(resp[:-1])[cmd.upper()][0]

    def host(self):
        host = self._run_command('version')

        if host.get('error'):
            return host

        return {
            'miner': host.get('Miner')
        }

    def config(self):
        config = self._run_command('config')

        if config.get('error'):
            return config

        return {
            'os': config.get('OS'),
            'pool_count': config.get('Pool Count'),
            'gpu_count': config.get('GPU Count')
        }

    def work_summary(self):
        summary = self._run_command('summary')

        if summary.get('error'):
            return summary

        return {
            'difficulty': summary.get('Difficulty Accepted'),
            'hash_rate_5s': summary.get('KHS 5s'),
            'hash_rate_avg': summary.get('KHS av'),
            'rejected_shares_percent': summary.get('Pool Rejected%'),
            'found_blocks': summary.get('Found Blocks'),
            'stale_shares_percent': summary.get('Pool Stale%'),
            'hardware_errors': summary.get('Hardware Errors'),
            'accepted_shares': summary.get('Accepted')
        }

    def gpus(self):
        devs_raw = self._run_command('devs',  expect_multiple = True)
        devs = []

        if devs_raw.get('error'):
            return devs_raw

        for dev in devs_raw:
            devs.append({
                'hash_rate_5s': dev.get('KHS 5s'),
                'temperature': dev.get('Temperature'),
                'clock_freq': dev.get('GPU Clock'),
                'status': dev.get('Status'),
                'fan_percent': dev.get('Fan Percent'),
                'accepted_shares': dev.get('Accepted'),
                'rejected_shares': dev.get('Rejected'),
                'raw_intensity': dev.get('RawIntensity')
            })

        return devs


from googletrans import Translator, constants
import speech_recognition as sr
r = sr.Recognizer()
t = Translator()
m = sr.Microphone()
with m as mic:
  r.adjust_for_ambient_noise(mic)
  print("Say something in English...")
  voice = r.listen(mic)
  print("Thank you for your input...")
  try:
    capture = r.recognize_google(voice) # By default, this will capture English words by the user. Changing arguments in "recognize_google" can change this.
    print("I think you said this: {}".format(capture))
  except Exception as e:
    print("Error: {}".format(e))
  output = translator.translate(capture, dest = "es") # This example uses "es" for Espanol, or Spanish. This can be changed.
  print("English: {} ===> Spanish: {}".format(output.origin, output.text))


# Utility to rebroadcast a data manager stream to InfluxDB

import sys
import time

from influxdb import InfluxDBClient

class InfluxDBWriter(object):
    def __init__(self, address, dbase_name, batch_size=50, write_interval=2):
        self.client = InfluxDBClient(host=address)
        self.batch_size = batch_size
        self.dbase_name = dbase_name
        self.write_interval = write_interval
        if dbase_name not in self.client.get_list_database():
            self.client.create_database(dbase_name)
        self.client.switch_database(dbase_name)
        self.last_write = time.time()

    def run(self, data_gen):
        data = []
        for datum in data_gen():
            if datum is not None:
                data.append(datum)
            if len(data) > self.batch_size or datum is None or time.time() - self.last_write > self.write_interval:
                if data:
                    while True:
                        try:
                            self.client.write_points(data, time_precision='ms', database=self.dbase_name)
                            self.last_write = time.time()
                            break
                        except:
                            time.sleep(5.0)
                            sys.stderr.write("x")
                    sys.stderr.write(".")
                    data = []
        sys.stderr.write('\n')


from fastapi import FastAPI, Header
from typing import Optional, List

app = FastAPI()

@app.get("/header_items/")
async def read_items(user_agent: Optional[str] = Header(None)):
    return {"user-agent":user_agent}

# ----- strange version --------
# 通常header都是以'-'分離的，但python只能允許'_'，所以Header模組提供自動轉換的功能
# 就算參數設user_agent，Header會自動轉換成user-agent

@app.get("/strange_items/") # 如果不想自動轉換 convert_underscores設成False就好
async def read_strange_items(strange_header: Optional[str] = Header(None, convert_underscores=False)):
    return {"strange_header":strange_header}

# Before setting convert_underscores to False, bear in mind that some HTTP proxies and servers disallow the usage of headers with underscores.

@app.get("/duplicate_header_items/")
async def read_duplicate_items(x_token: Optional[List[str]] = Header(None)):
    return {"x-token values":x_token}


import sys

sys.path.append('..')
sys.path.append('.')

import io
import numpy as np
import cv2
import torch
import base64
from io import BytesIO
import PIL

from models.viton import Viton, make_image
from models.keypoint import KeyPointPredictor
from models.segmentation import Segmentator
from models.posenet import DensePosePredictor
from models.segmentation import Segmentator
from network_utils.network_utils import CPDataset
from utils.utils import convert_image
from torchvision import transforms

from flask import Flask, jsonify, request
from scipy.ndimage.filters import gaussian_filter
from PIL import Image

app = Flask(__name__)

transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

transform_1d = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])

def pipeline(img, cloth, cloth_mask):
    img_array = np.array(img)
    img_array = convert_image(img)
    img = Image.fromarray(convert_image(np.array(img)))
    img = transform(img)
    im_255 = cv2.resize(img_array, (255, 255), interpolation = cv2.INTER_AREA)
    dp = DensePosePredictor("./detectron2_repo/projects/DensePose/configs/densepose_rcnn_R_50_FPN_s1x.yaml",
                            "./checkpoint/densepose_rcnn_R_50_FPN_s1x.pkl")
    phead, body = dp.predict(im_255)
    body = cv2.resize(body, (192, 256), interpolation = cv2.INTER_AREA)
    body = Image.fromarray((body*255).astype(np.uint8))
    body = body.resize((192//16, 256//16), Image.BILINEAR)
    body = body.resize((192, 256), Image.BILINEAR)
    phead = cv2.resize(phead, (192, 256), interpolation = cv2.INTER_AREA)
    phead = torch.from_numpy(phead[np.newaxis,:,:])
    head = img * phead - (1 - phead)
    shape = transform_1d(body).type(torch.float32)

    key_model = KeyPointPredictor()
    pose_map, _ = key_model.predict(img_array)

    pose_map_corect = torch.zeros((18, 256, 192)) - 1
    pose_map_corect[0] = pose_map[0]
    pose_map_corect[1] = pose_map[17]
    pose_map_corect[2] = pose_map[6]
    pose_map_corect[3] = pose_map[8]
    pose_map_corect[4] = pose_map[10]
    pose_map_corect[5] = pose_map[5]
    pose_map_corect[6] = pose_map[7]
    pose_map_corect[7] = pose_map[9]
    pose_map_corect[8] = pose_map[12]
    pose_map_corect[11] = pose_map[11]
    pose_map_corect[14] = pose_map[2]
    pose_map_corect[15] = pose_map[1]
    pose_map_corect[16] = pose_map[4]
    pose_map_corect[17] = pose_map[3]
    pose_map = torch.tensor(pose_map_corect).type(torch.float32)

    viton = Viton(gmm_checkpoint_path='./checkpoint/gmm_train_new/gmm_final.pth',
                tom_checkpoint_path='./checkpoint/tom_train_new/tom_final.pth')
    shape = Image.open('./datasets/data/train/cloth/000003_1.jpg') # path to cloth
    shape = transform_1d(shape)
    res = viton.run_viton(head, pose_map, shape, cloth, cloth_mask)

    img_array[np.array(body) == -1] = 0
    cv2.imwrite("body.jpg", img_array)
    cv2.imwrite("body2.jpg", np.array(body))

    return encode((np.array(res[0].cpu().detach()).transpose(1, 2, 0) + 1) * 127.5)

def decode(byte64_str):
    bytes = BytesIO(base64.b64decode(byte64_str))
    pil_img = PIL.Image.open(bytes)
    return pil_img

def encode(img):
    pil_img = Image.fromarray(img.astype('uint8'))
    pil_img = pil_img.convert('RGB')
    buff = BytesIO()
    pil_img.save(buff, format="JPEG")
    return base64.b64encode(buff.getvalue()).decode("utf-8")

@app.route('/style/api/v1.0/segmentate', methods=['POST'])
def predict():
    if request.method == 'POST':
        file = request.values['file']
        cloth_id = int(request.values['cloth'])
        if cloth_id == 0:
            c = Image.open(
                '/home/tema/Projects/Course-work-3/datasets/viton_resize/train/cloth/000736_1.jpg')  # path to cloth
            c = transform(c)
            cm = Image.open(
                '/home/tema/Projects/Course-work-3/datasets/viton_resize/train/cloth-mask/000736_1.jpg')  # path to cloth-mask
            cm_array = np.array(cm)
            cm_array = (cm_array >= 128).astype(np.float32)
            cm = torch.from_numpy(cm_array)
            cm.unsqueeze_(0)

        img = decode(file)
        res = pipeline(img, c, cm)
    return encode(np.array(res[0].cpu().detach()).transpose(1,2,0) * 255)

if __name__ == '__main__':
    app.run(debug=True, port=5000)


"""
igraph library.
"""

__license__ = """
Copyright (C) 2006- The igraph development team

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
"""

from igraph._igraph import (
    ADJ_DIRECTED,
    ADJ_LOWER,
    ADJ_MAX,
    ADJ_MIN,
    ADJ_PLUS,
    ADJ_UNDIRECTED,
    ADJ_UPPER,
    ALL,
    ARPACKOptions,
    BFSIter,
    BLISS_F,
    BLISS_FL,
    BLISS_FLM,
    BLISS_FM,
    BLISS_FS,
    BLISS_FSM,
    DFSIter,
    Edge,
    GET_ADJACENCY_BOTH,
    GET_ADJACENCY_LOWER,
    GET_ADJACENCY_UPPER,
    GraphBase,
    IN,
    InternalError,
    OUT,
    REWIRING_SIMPLE,
    REWIRING_SIMPLE_LOOPS,
    STAR_IN,
    STAR_MUTUAL,
    STAR_OUT,
    STAR_UNDIRECTED,
    STRONG,
    TRANSITIVITY_NAN,
    TRANSITIVITY_ZERO,
    TREE_IN,
    TREE_OUT,
    TREE_UNDIRECTED,
    Vertex,
    WEAK,
    arpack_options as default_arpack_options,
    community_to_membership,
    convex_hull,
    is_bigraphical,
    is_degree_sequence,
    is_graphical,
    is_graphical_degree_sequence,
    set_progress_handler,
    set_random_number_generator,
    set_status_handler,
    umap_compute_weights,
    __igraph_version__,
)
from igraph.adjacency import (
    _get_adjacency,
    _get_adjacency_sparse,
    _get_adjlist,
    _get_biadjacency,
    _get_inclist,
)
from igraph.automorphisms import (
    _count_automorphisms_vf2,
    _get_automorphisms_vf2,
)
from igraph.basic import (
    _add_edge,
    _add_edges,
    _add_vertex,
    _add_vertices,
    _delete_edges,
    _clear,
    _as_directed,
    _as_undirected,
)
from igraph.bipartite import (
    _maximum_bipartite_matching,
    _bipartite_projection,
    _bipartite_projection_size,
)
from igraph.community import (
    _community_fastgreedy,
    _community_infomap,
    _community_leading_eigenvector,
    _community_label_propagation,
    _community_multilevel,
    _community_optimal_modularity,
    _community_edge_betweenness,
    _community_spinglass,
    _community_walktrap,
    _k_core,
    _community_leiden,
    _modularity,
)
from igraph.clustering import (
    Clustering,
    VertexClustering,
    Dendrogram,
    VertexDendrogram,
    Cover,
    VertexCover,
    CohesiveBlocks,
    compare_communities,
    split_join_distance,
    _biconnected_components,
    _cohesive_blocks,
    _connected_components,
    _clusters,
)
from igraph.cut import (
    Cut,
    Flow,
    _all_st_cuts,
    _all_st_mincuts,
    _gomory_hu_tree,
    _maxflow,
    _mincut,
    _st_mincut,
)
from igraph.configuration import Configuration, init as init_configuration
from igraph.drawing import (
    BoundingBox,
    CairoGraphDrawer,
    DefaultGraphDrawer,
    MatplotlibGraphDrawer,
    Plot,
    Point,
    Rectangle,
    plot,
)
from igraph.drawing.colors import (
    Palette,
    GradientPalette,
    AdvancedGradientPalette,
    RainbowPalette,
    PrecalculatedPalette,
    ClusterColoringPalette,
    color_name_to_rgb,
    color_name_to_rgba,
    hsv_to_rgb,
    hsva_to_rgba,
    hsl_to_rgb,
    hsla_to_rgba,
    rgb_to_hsv,
    rgba_to_hsva,
    rgb_to_hsl,
    rgba_to_hsla,
    palettes,
    known_colors,
)
from igraph.drawing.graph import __plot__ as _graph_plot
from igraph.drawing.utils import autocurve
from igraph.datatypes import Matrix, DyadCensus, TriadCensus, UniqueIdGenerator
from igraph.formula import construct_graph_from_formula
from igraph.io import _format_mapping
from igraph.io.files import (
    _construct_graph_from_graphmlz_file,
    _construct_graph_from_dimacs_file,
    _construct_graph_from_pickle_file,
    _construct_graph_from_picklez_file,
    _construct_graph_from_adjacency_file,
    _construct_graph_from_file,
    _write_graph_to_adjacency_file,
    _write_graph_to_dimacs_file,
    _write_graph_to_graphmlz_file,
    _write_graph_to_pickle_file,
    _write_graph_to_picklez_file,
    _write_graph_to_file,
)
from igraph.io.objects import (
    _construct_graph_from_dict_list,
    _export_graph_to_dict_list,
    _construct_graph_from_tuple_list,
    _export_graph_to_tuple_list,
    _construct_graph_from_list_dict,
    _export_graph_to_list_dict,
    _construct_graph_from_dict_dict,
    _export_graph_to_dict_dict,
    _construct_graph_from_dataframe,
    _export_vertex_dataframe,
    _export_edge_dataframe,
)
from igraph.io.adjacency import (
    _construct_graph_from_adjacency,
    _construct_graph_from_weighted_adjacency,
)
from igraph.io.libraries import (
    _construct_graph_from_networkx,
    _export_graph_to_networkx,
    _construct_graph_from_graph_tool,
    _export_graph_to_graph_tool,
)
from igraph.io.random import (
    _construct_random_geometric_graph,
)
from igraph.io.bipartite import (
    _construct_bipartite_graph,
    _construct_bipartite_graph_from_adjacency,
    _construct_full_bipartite_graph,
    _construct_random_bipartite_graph,
)
from igraph.io.images import _write_graph_to_svg
from igraph.layout import (
    Layout,
    _layout,
    _layout_auto,
    _layout_sugiyama,
    _layout_method_wrapper,
    _3d_version_for,
    _layout_mapping,
)
from igraph.matching import Matching
from igraph.operators import (
    disjoint_union,
    union,
    intersection,
    operator_method_registry as _operator_method_registry,
)
from igraph.seq import EdgeSeq, VertexSeq, _add_proxy_methods
from igraph.statistics import (
    FittedPowerLaw,
    Histogram,
    RunningMean,
    mean,
    median,
    percentile,
    quantile,
    power_law_fit,
)
from igraph.structural import (
    _indegree,
    _outdegree,
    _degree_distribution,
    _pagerank,
    _shortest_paths,
)
from igraph.summary import GraphSummary, summary
from igraph.utils import (
    deprecated,
    numpy_to_contiguous_memoryview,
    rescale,
)
from igraph.version import __version__, __version_info__

import os
import sys

class Graph(GraphBase):
    """Generic graph.

    This class is built on top of L{GraphBase}, so the order of the
    methods in the generated API documentation is a little bit obscure:
    inherited methods come after the ones implemented directly in the
    subclass. L{Graph} provides many functions that L{GraphBase} does not,
    mostly because these functions are not speed critical and they were
    easier to implement in Python than in pure C. An example is the
    attribute handling in the constructor: the constructor of L{Graph}
    accepts three dictionaries corresponding to the graph, vertex and edge
    attributes while the constructor of L{GraphBase} does not. This extension
    was needed to make L{Graph} serializable through the C{pickle} module.
    L{Graph} also overrides some functions from L{GraphBase} to provide a
    more convenient interface; e.g., layout functions return a L{Layout}
    instance from L{Graph} instead of a list of coordinate pairs.

    Graphs can also be indexed by strings or pairs of vertex indices or vertex
    names.  When a graph is indexed by a string, the operation translates to
    the retrieval, creation, modification or deletion of a graph attribute:

      >>> g = Graph.Full(3)
      >>> g["name"] = "Triangle graph"
      >>> g["name"]
      'Triangle graph'
      >>> del g["name"]

    When a graph is indexed by a pair of vertex indices or names, the graph
    itself is treated as an adjacency matrix and the corresponding cell of
    the matrix is returned:

      >>> g = Graph.Full(3)
      >>> g.vs["name"] = ["A", "B", "C"]
      >>> g[1, 2]
      1
      >>> g["A", "B"]
      1
      >>> g["A", "B"] = 0
      >>> g.ecount()
      2

    Assigning values different from zero or one to the adjacency matrix will
    be translated to one, unless the graph is weighted, in which case the
    numbers will be treated as weights:

      >>> g.is_weighted()
      False
      >>> g["A", "B"] = 2
      >>> g["A", "B"]
      1
      >>> g.es["weight"] = 1.0
      >>> g.is_weighted()
      True
      >>> g["A", "B"] = 2
      >>> g["A", "B"]
      2
      >>> g.es["weight"]
      [1.0, 1.0, 2]
    """

    # Some useful aliases
    omega = GraphBase.clique_number
    alpha = GraphBase.independence_number
    shell_index = GraphBase.coreness
    cut_vertices = GraphBase.articulation_points
    blocks = GraphBase.biconnected_components
    evcent = GraphBase.eigenvector_centrality
    vertex_disjoint_paths = GraphBase.vertex_connectivity
    edge_disjoint_paths = GraphBase.edge_connectivity
    cohesion = GraphBase.vertex_connectivity
    adhesion = GraphBase.edge_connectivity

    # Compatibility aliases
    shortest_paths = _shortest_paths
    shortest_paths_dijkstra = shortest_paths
    subgraph = GraphBase.induced_subgraph

    def __init__(self, *args, **kwds):
        """__init__(n=0, edges=None, directed=False, graph_attrs=None,
        vertex_attrs=None, edge_attrs=None)

        Constructs a graph from scratch.

        @keyword n: the number of vertices. Can be omitted, the default is
          zero. Note that if the edge list contains vertices with indexes
          larger than or equal to M{n}, then the number of vertices will
          be adjusted accordingly.
        @keyword edges: the edge list where every list item is a pair of
          integers. If any of the integers is larger than M{n-1}, the number
          of vertices is adjusted accordingly. C{None} means no edges.
        @keyword directed: whether the graph should be directed
        @keyword graph_attrs: the attributes of the graph as a dictionary.
        @keyword vertex_attrs: the attributes of the vertices as a dictionary.
          The keys of the dictionary must be the names of the attributes; the
          values must be iterables with exactly M{n} items where M{n} is the
          number of vertices.
        @keyword edge_attrs: the attributes of the edges as a dictionary. The
          keys of the dictionary must be the names of the attributes; the values
          must be iterables with exactly M{m} items where M{m} is the number of
          edges.
        """
        # Pop the special __ptr keyword argument
        ptr = kwds.pop("__ptr", None)

        # Set up default values for the parameters. This should match the order
        # in *args
        kwd_order = (
            "n",
            "edges",
            "directed",
            "graph_attrs",
            "vertex_attrs",
            "edge_attrs",
        )
        params = [0, [], False, {}, {}, {}]

        # Is there any keyword argument in kwds that we don't know? If so,
        # freak out.
        unknown_kwds = set(kwds.keys())
        unknown_kwds.difference_update(kwd_order)
        if unknown_kwds:
            raise TypeError(
                "{0}.__init__ got an unexpected keyword argument {1!r}".format(
                    self.__class__.__name__, unknown_kwds.pop()
                )
            )

        # If the first argument is a list or any other iterable, assume that
        # the number of vertices were omitted
        args = list(args)
        if len(args) > 0 and hasattr(args[0], "__iter__"):
            args.insert(0, params[0])

        # Override default parameters from args
        params[: len(args)] = args

        # Override default parameters from keywords
        for idx, k in enumerate(kwd_order):
            if k in kwds:
                params[idx] = kwds[k]

        # Now, translate the params list to argument names
        nverts, edges, directed, graph_attrs, vertex_attrs, edge_attrs = params

        # When the number of vertices is None, assume that the user meant zero
        if nverts is None:
            nverts = 0

        # When 'edges' is None, assume that the user meant an empty list
        if edges is None:
            edges = []

        # When 'edges' is a NumPy array or matrix, convert it into a memoryview
        # as the lower-level C API works with memoryviews only
        try:
            from numpy import ndarray, matrix

            if isinstance(edges, (ndarray, matrix)):
                edges = numpy_to_contiguous_memoryview(edges)
        except ImportError:
            pass

        # Initialize the graph
        if ptr:
            super().__init__(__ptr=ptr)
        else:
            super().__init__(nverts, edges, directed)

        # Set the graph attributes
        for key, value in graph_attrs.items():
            if isinstance(key, int):
                key = str(key)
            self[key] = value
        # Set the vertex attributes
        for key, value in vertex_attrs.items():
            if isinstance(key, int):
                key = str(key)
            self.vs[key] = value
        # Set the edge attributes
        for key, value in edge_attrs.items():
            if isinstance(key, int):
                key = str(key)
            self.es[key] = value

    #############################################
    # Auxiliary I/O functions
    # Graph libraries
    from_networkx = classmethod(_construct_graph_from_networkx)
    to_networkx = _export_graph_to_networkx

    from_graph_tool = classmethod(_construct_graph_from_graph_tool)
    to_graph_tool = _export_graph_to_graph_tool

    # Files
    Read_DIMACS = classmethod(_construct_graph_from_dimacs_file)
    write_dimacs = _write_graph_to_dimacs_file

    Read_GraphMLz = classmethod(_construct_graph_from_graphmlz_file)
    write_graphmlz = _write_graph_to_graphmlz_file

    Read_Pickle = classmethod(_construct_graph_from_pickle_file)
    write_pickle = _write_graph_to_pickle_file

    Read_Picklez = classmethod(_construct_graph_from_picklez_file)
    write_picklez = _write_graph_to_picklez_file

    Read_Adjacency = classmethod(_construct_graph_from_adjacency_file)
    write_adjacency = _write_graph_to_adjacency_file

    write_svg = _write_graph_to_svg

    Read = classmethod(_construct_graph_from_file)
    Load = Read
    write = _write_graph_to_file
    save = write

    # Various objects
    # list of dict representation of graphs
    DictList = classmethod(_construct_graph_from_dict_list)
    to_dict_list = _export_graph_to_dict_list

    # tuple-like representation of graphs
    TupleList = classmethod(_construct_graph_from_tuple_list)
    to_tuple_list = _export_graph_to_tuple_list

    # dict of sequence representation of graphs
    ListDict = classmethod(_construct_graph_from_list_dict)
    to_list_dict = _export_graph_to_list_dict

    # dict of dicts representation of graphs
    DictDict = classmethod(_construct_graph_from_dict_dict)
    to_dict_dict = _export_graph_to_dict_dict

    # adjacency matrix
    Adjacency = classmethod(_construct_graph_from_adjacency)
    Weighted_Adjacency = classmethod(_construct_graph_from_weighted_adjacency)

    # pandas dataframe(s)
    DataFrame = classmethod(_construct_graph_from_dataframe)
    get_vertex_dataframe = _export_vertex_dataframe
    get_edge_dataframe = _export_edge_dataframe

    # Bipartite graphs
    Bipartite = classmethod(_construct_bipartite_graph)
    Biadjacency = classmethod(_construct_bipartite_graph_from_adjacency)
    Full_Bipartite = classmethod(_construct_full_bipartite_graph)
    Random_Bipartite = classmethod(_construct_random_bipartite_graph)

    # Other constructors
    GRG = classmethod(_construct_random_geometric_graph)

    # Graph formulae
    Formula = classmethod(construct_graph_from_formula)

    #############################################
    # Summary/string representation
    def __str__(self):
        """Returns a string representation of the graph.

        Behind the scenes, this method constructs a L{GraphSummary}
        instance and invokes its C{__str__} method with a verbosity of 1
        and attribute printing turned off.

        See the documentation of L{GraphSummary} for more details about the
        output.
        """
        params = {
            "verbosity": 1,
            "width": 78,
            "print_graph_attributes": False,
            "print_vertex_attributes": False,
            "print_edge_attributes": False,
        }
        return self.summary(**params)

    def summary(self, verbosity=0, width=None, *args, **kwds):
        """Returns the summary of the graph.

        The output of this method is similar to the output of the
        C{__str__} method. If I{verbosity} is zero, only the header line
        is returned (see C{__str__} for more details), otherwise the
        header line and the edge list is printed.

        Behind the scenes, this method constructs a L{GraphSummary}
        object and invokes its C{__str__} method.

        @param verbosity: if zero, only the header line is returned
          (see C{__str__} for more details), otherwise the header line
          and the full edge list is printed.
        @param width: the number of characters to use in one line.
          If C{None}, no limit will be enforced on the line lengths.
        @return: the summary of the graph.
        """
        return str(GraphSummary(self, verbosity, width, *args, **kwds))

    #############################################
    # Commonly used attributes
    def is_named(self):
        """Returns whether the graph is named.

        A graph is named if and only if it has a C{"name"} vertex attribute.
        """
        return "name" in self.vertex_attributes()

    def is_weighted(self):
        """Returns whether the graph is weighted.

        A graph is weighted if and only if it has a C{"weight"} edge attribute.
        """
        return "weight" in self.edge_attributes()

    #############################################
    # Vertex and edge sequence
    @property
    def vs(self):
        """The vertex sequence of the graph"""
        return VertexSeq(self)

    @property
    def es(self):
        """The edge sequence of the graph"""
        return EdgeSeq(self)

    #############################################
    # Basic operations
    add_edge = _add_edge
    add_edges = _add_edges
    add_vertex = _add_vertex
    add_vertices = _add_vertices
    delete_edges = _delete_edges
    clear = _clear
    as_directed = _as_directed
    as_undirected = _as_undirected

    ###################
    # Graph operators
    __iadd__ = _operator_method_registry["__iadd__"]
    __add__ = _operator_method_registry["__add__"]
    __and__ = _operator_method_registry["__and__"]
    __isub__ = _operator_method_registry["__isub__"]
    __sub__ = _operator_method_registry["__sub__"]
    __mul__ = _operator_method_registry["__mul__"]
    __or__ = _operator_method_registry["__or__"]
    disjoint_union = _operator_method_registry["disjoint_union"]
    union = _operator_method_registry["union"]
    intersection = _operator_method_registry["intersection"]

    #############################################
    # Adjacency/incidence
    get_adjacency = _get_adjacency
    get_adjacency_sparse = _get_adjacency_sparse
    get_adjlist = _get_adjlist
    get_biadjacency = _get_biadjacency
    get_inclist = _get_inclist

    #############################################
    # Structural properties
    indegree = _indegree
    outdegree = _outdegree
    degree_distribution = _degree_distribution
    pagerank = _pagerank

    #############################################
    # Flow
    all_st_cuts = _all_st_cuts
    all_st_mincuts = _all_st_mincuts
    gomory_hu_tree = _gomory_hu_tree
    maxflow = _maxflow
    mincut = _mincut
    st_mincut = _st_mincut

    #############################################
    # Connected components
    biconnected_components = _biconnected_components
    clusters = _clusters
    cohesive_blocks = _cohesive_blocks
    connected_components = _connected_components
    blocks = _biconnected_components
    components = _connected_components

    #############################################
    # Community detection/clustering
    community_fastgreedy = _community_fastgreedy
    community_infomap = _community_infomap
    community_leading_eigenvector = _community_leading_eigenvector
    community_label_propagation = _community_label_propagation
    community_multilevel = _community_multilevel
    community_optimal_modularity = _community_optimal_modularity
    community_edge_betweenness = _community_edge_betweenness
    community_spinglass = _community_spinglass
    community_walktrap = _community_walktrap
    k_core = _k_core
    community_leiden = _community_leiden
    modularity = _modularity

    #############################################
    # Layout
    layout = _layout
    layout_auto = _layout_auto
    layout_sugiyama = _layout_sugiyama

    #############################################
    # Plotting
    __plot__ = _graph_plot

    #############################################
    # Bipartite
    maximum_bipartite_matching = _maximum_bipartite_matching
    bipartite_projection = _bipartite_projection
    bipartite_projection_size = _bipartite_projection_size

    #############################################
    # Automorphisms
    count_automorphisms_vf2 = _count_automorphisms_vf2
    get_automorphisms_vf2 = _get_automorphisms_vf2

    ###########################
    # Paths/traversals
    def get_all_simple_paths(self, v, to=None, cutoff=-1, mode="out"):
        """Calculates all the simple paths from a given node to some other nodes
        (or all of them) in a graph.

        A path is simple if its vertices are unique, i.e. no vertex is visited
        more than once.

        Note that potentially there are exponentially many paths between two
        vertices of a graph, especially if your graph is lattice-like. In this
        case, you may run out of memory when using this function.

        @param v: the source for the calculated paths
        @param to: a vertex selector describing the destination for the calculated
          paths. This can be a single vertex ID, a list of vertex IDs, a single
          vertex name, a list of vertex names or a L{VertexSeq} object. C{None}
          means all the vertices.
        @param cutoff: maximum length of path that is considered. If negative,
          paths of all lengths are considered.
        @param mode: the directionality of the paths. C{\"in\"} means to calculate
          incoming paths, C{\"out\"} means to calculate outgoing paths, C{\"all\"} means
          to calculate both ones.
        @return: all of the simple paths from the given node to every other
          reachable node in the graph in a list. Note that in case of mode=C{\"in\"},
          the vertices in a path are returned in reversed order!
        """
        paths = self._get_all_simple_paths(v, to, cutoff, mode)
        prev = 0
        result = []
        for index, item in enumerate(paths):
            if item < 0:
                result.append(paths[prev:index])
                prev = index + 1
        return result

    def path_length_hist(self, directed=True):
        """Returns the path length histogram of the graph

        @param directed: whether to consider directed paths. Ignored for
          undirected graphs.
        @return: a L{Histogram} object. The object will also have an
          C{unconnected} attribute that stores the number of unconnected
          vertex pairs (where the second vertex can not be reached from
          the first one). The latter one will be of type long (and not
          a simple integer), since this can be I{very} large.
        """
        data, unconn = GraphBase.path_length_hist(self, directed)
        hist = Histogram(bin_width=1)
        for i, length in enumerate(data):
            hist.add(i + 1, length)
        hist.unconnected = int(unconn)
        return hist

    # DFS (C version will come soon)
    def dfs(self, vid, mode=OUT):
        """Conducts a depth first search (DFS) on the graph.

        @param vid: the root vertex ID
        @param mode: either C{\"in\"} or C{\"out\"} or C{\"all\"}, ignored
          for undirected graphs.
        @return: a tuple with the following items:
           - The vertex IDs visited (in order)
           - The parent of every vertex in the DFS
        """
        nv = self.vcount()
        added = [False for v in range(nv)]
        stack = []

        # prepare output
        vids = []
        parents = []

        # ok start from vid
        stack.append((vid, self.neighbors(vid, mode=mode)))
        vids.append(vid)
        parents.append(-1)
        added[vid] = True

        # go down the rabbit hole
        while stack:
            vid, neighbors = stack[-1]
            if neighbors:
                # Get next neighbor to visit
                neighbor = neighbors.pop()
                if not added[neighbor]:
                    # Add hanging subtree neighbor
                    stack.append((neighbor, self.neighbors(neighbor, mode=mode)))
                    vids.append(neighbor)
                    parents.append(vid)
                    added[neighbor] = True
            else:
                # No neighbor found, end of subtree
                stack.pop()

        return (vids, parents)

    def spanning_tree(self, weights=None, return_tree=True):
        """Calculates a minimum spanning tree for a graph.

        B{Reference}: Prim, R.C. Shortest connection networks and some
        generalizations. I{Bell System Technical Journal} 36:1389-1401, 1957.

        @param weights: a vector containing weights for every edge in
          the graph. C{None} means that the graph is unweighted.
        @param return_tree: whether to return the minimum spanning tree (when
          C{return_tree} is C{True}) or to return the IDs of the edges in
          the minimum spanning tree instead (when C{return_tree} is C{False}).
          The default is C{True} for historical reasons as this argument was
          introduced in igraph 0.6.
        @return: the spanning tree as a L{Graph} object if C{return_tree}
          is C{True} or the IDs of the edges that constitute the spanning
          tree if C{return_tree} is C{False}.
        """
        result = GraphBase._spanning_tree(self, weights)
        if return_tree:
            return self.subgraph_edges(result, delete_vertices=False)
        return result

    ###########################
    # Dyad/triad census
    def dyad_census(self, *args, **kwds):
        """Calculates the dyad census of the graph.

        Dyad census means classifying each pair of vertices of a directed
        graph into three categories: mutual (there is an edge from I{a} to
        I{b} and also from I{b} to I{a}), asymmetric (there is an edge
        from I{a} to I{b} or from I{b} to I{a} but not the other way round)
        and null (there is no connection between I{a} and I{b}).

        B{Reference}: Holland, P.W. and Leinhardt, S. A Method for Detecting
        Structure in Sociometric Data. I{American Journal of Sociology}, 70,
        492-513, 1970.

        @return: a L{DyadCensus} object.
        """
        return DyadCensus(GraphBase.dyad_census(self, *args, **kwds))

    def triad_census(self, *args, **kwds):
        """Calculates the triad census of the graph.

        B{Reference}: Davis, J.A. and Leinhardt, S. The Structure of
        Positive Interpersonal Relations in Small Groups. In:
        J. Berger (Ed.), Sociological Theories in Progress, Volume 2,
        218-251. Boston: Houghton Mifflin (1972).

        @return: a L{TriadCensus} object.
        """
        return TriadCensus(GraphBase.triad_census(self, *args, **kwds))

    ###########################
    # Other functions
    def transitivity_avglocal_undirected(self, mode="nan", weights=None):
        """Calculates the average of the vertex transitivities of the graph.

        In the unweighted case, the transitivity measures the probability that
        two neighbors of a vertex are connected. In case of the average local
        transitivity, this probability is calculated for each vertex and then
        the average is taken. Vertices with less than two neighbors require
        special treatment, they will either be left out from the calculation
        or they will be considered as having zero transitivity, depending on
        the I{mode} parameter. The calculation is slightly more involved for
        weighted graphs; in this case, weights are taken into account according
        to the formula of Barrat et al (see the references).

        Note that this measure is different from the global transitivity
        measure (see L{transitivity_undirected()}) as it simply takes the
        average local transitivity across the whole network.

        B{References}

          - Watts DJ and Strogatz S: Collective dynamics of small-world
            networks. I{Nature} 393(6884):440-442, 1998.
          - Barrat A, Barthelemy M, Pastor-Satorras R and Vespignani A:
            The architecture of complex weighted networks. I{PNAS} 101, 3747
            (2004). U{http://arxiv.org/abs/cond-mat/0311416}.

        @param mode: defines how to treat vertices with degree less than two.
          If C{TRANSITIVITY_ZERO} or C{"zero"}, these vertices will have zero
          transitivity. If C{TRANSITIVITY_NAN} or C{"nan"}, these vertices
          will be excluded from the average.
        @param weights: edge weights to be used. Can be a sequence or iterable
          or even an edge attribute name.

        @see: L{transitivity_undirected()}, L{transitivity_local_undirected()}
        """
        if weights is None:
            return GraphBase.transitivity_avglocal_undirected(self, mode)

        xs = self.transitivity_local_undirected(mode=mode, weights=weights)
        return sum(xs) / float(len(xs))

    ###########################
    # ctypes support
    @property
    def _as_parameter_(self):
        return self._raw_pointer()

    # Other type functions
    def __bool__(self):
        """Returns True if the graph has at least one vertex, False otherwise."""
        return self.vcount() > 0

    def __coerce__(self, other):
        """Coercion rules.

        This method is needed to allow the graph to react to additions
        with lists, tuples, integers, strings, vertices, edges and so on.
        """
        if isinstance(other, (int, tuple, list, str)):
            return self, other
        if isinstance(other, Vertex):
            return self, other
        if isinstance(other, VertexSeq):
            return self, other
        if isinstance(other, Edge):
            return self, other
        if isinstance(other, EdgeSeq):
            return self, other
        return NotImplemented

    @classmethod
    def _reconstruct(cls, attrs, *args, **kwds):
        """Reconstructs a Graph object from Python's pickled format.

        This method is for internal use only, it should not be called
        directly."""
        result = cls(*args, **kwds)
        result.__dict__.update(attrs)
        return result

    def __reduce__(self):
        """Support for pickling."""
        constructor = self.__class__
        gattrs, vattrs, eattrs = {}, {}, {}
        for attr in self.attributes():
            gattrs[attr] = self[attr]
        for attr in self.vs.attribute_names():
            vattrs[attr] = self.vs[attr]
        for attr in self.es.attribute_names():
            eattrs[attr] = self.es[attr]
        parameters = (
            self.vcount(),
            self.get_edgelist(),
            self.is_directed(),
            gattrs,
            vattrs,
            eattrs,
        )
        return (constructor, parameters, self.__dict__)

    __iter__ = None  # needed for PyPy
    __hash__ = None  # needed for PyPy

    ###########################
    # Deprecated functions

    @classmethod
    def Incidence(cls, *args, **kwds):
        """Deprecated alias to L{Graph.Biadjacency()}."""
        deprecated("Graph.Incidence() is deprecated; use Graph.Biadjacency() instead")
        return cls.Biadjacency(*args, **kwds)

    def get_incidence(self, *args, **kwds):
        """Deprecated alias to L{Graph.get_biadjacency()}."""
        deprecated(
            "Graph.get_incidence() is deprecated; use Graph.get_biadjacency() "
            "instead"
        )
        return self.get_biadjacency(*args, **kwds)

##############################################################
# I/O format mapping
Graph._format_mapping = _format_mapping

##############################################################
# Additional methods of VertexSeq and EdgeSeq that call Graph methods
_add_proxy_methods()

##############################################################
# Layout mapping
Graph._layout_mapping = _layout_mapping

##############################################################
# Making sure that layout methods always return a Layout
for name in dir(Graph):
    if not name.startswith("layout_"):
        continue
    if name in ("layout_auto", "layout_sugiyama"):
        continue
    setattr(Graph, name, _layout_method_wrapper(getattr(Graph, name)))

##############################################################
# Adding aliases for the 3D versions of the layout methods
Graph.layout_fruchterman_reingold_3d = _3d_version_for(
    Graph.layout_fruchterman_reingold
)
Graph.layout_kamada_kawai_3d = _3d_version_for(Graph.layout_kamada_kawai)
Graph.layout_random_3d = _3d_version_for(Graph.layout_random)
Graph.layout_grid_3d = _3d_version_for(Graph.layout_grid)
Graph.layout_sphere = _3d_version_for(Graph.layout_circle)

##############################################################
# Auxiliary global functions
def get_include():
    """Returns the folder that contains the C API headers of the Python
    interface of igraph."""
    import igraph

    paths = [
        # The following path works if igraph is installed already
        os.path.join(
            sys.prefix,
            "include",
            "python{0}.{1}".format(*sys.version_info),
            "igraph",
        ),
        # Fallback for cases when igraph is not installed but
        # imported directly from the source tree
        os.path.join(os.path.dirname(igraph.__file__), "..", "src", "_igraph"),
    ]
    for path in paths:
        if os.path.exists(os.path.join(path, "igraphmodule_api.h")):
            return os.path.abspath(path)
    raise ValueError("cannot find the header files of the Python interface of igraph")

def read(filename, *args, **kwds):
    """Loads a graph from the given filename.

    This is just a convenience function, calls L{Graph.Read} directly.
    All arguments are passed unchanged to L{Graph.Read}

    @param filename: the name of the file to be loaded
    """
    return Graph.Read(filename, *args, **kwds)

load = read

def write(graph, filename, *args, **kwds):
    """Saves a graph to the given file.

    This is just a convenience function, calls L{Graph.write} directly.
    All arguments are passed unchanged to L{Graph.write}

    @param graph: the graph to be saved
    @param filename: the name of the file to be written
    """
    return graph.write(filename, *args, **kwds)

save = write

##############################################################
# Configuration singleton instance
config = init_configuration()
"""The main configuration object of igraph. Use this object to modify igraph's
behaviour, typically when used in interactive mode.
"""

##############################################################
# Remove modular methods from namespace
del (
    construct_graph_from_formula,
    _construct_graph_from_graphmlz_file,
    _construct_graph_from_dimacs_file,
    _construct_graph_from_pickle_file,
    _construct_graph_from_picklez_file,
    _construct_graph_from_adjacency_file,
    _construct_graph_from_file,
    _format_mapping,
    _construct_graph_from_dict_list,
    _construct_graph_from_tuple_list,
    _construct_graph_from_list_dict,
    _construct_graph_from_dict_dict,
    _construct_graph_from_adjacency,
    _construct_graph_from_weighted_adjacency,
    _construct_graph_from_dataframe,
    _construct_random_geometric_graph,
    _construct_bipartite_graph,
    _construct_bipartite_graph_from_adjacency,
    _construct_full_bipartite_graph,
    _construct_random_bipartite_graph,
    _construct_graph_from_networkx,
    _export_graph_to_networkx,
    _construct_graph_from_graph_tool,
    _export_graph_to_graph_tool,
    _export_graph_to_list_dict,
    _export_graph_to_dict_dict,
    _export_graph_to_dict_list,
    _export_graph_to_tuple_list,
    _community_fastgreedy,
    _community_infomap,
    _community_leading_eigenvector,
    _community_label_propagation,
    _community_multilevel,
    _community_optimal_modularity,
    _community_edge_betweenness,
    _community_spinglass,
    _community_walktrap,
    _k_core,
    _community_leiden,
    _modularity,
    _graph_plot,
    _operator_method_registry,
    _add_edge,
    _add_edges,
    _add_vertex,
    _add_vertices,
    _delete_edges,
    _as_directed,
    _as_undirected,
    _layout,
    _layout_auto,
    _layout_sugiyama,
    _layout_method_wrapper,
    _3d_version_for,
    _layout_mapping,
    _count_automorphisms_vf2,
    _get_automorphisms_vf2,
    _get_adjacency,
    _get_adjacency_sparse,
    _get_adjlist,
    _maximum_bipartite_matching,
    _bipartite_projection,
    _bipartite_projection_size,
    _biconnected_components,
    _cohesive_blocks,
    _connected_components,
    _add_proxy_methods,
)

# Re-export from _igraph for API docs
# Because _igraph starts with an underscore, pydoctor skips the whole docs
# except for the objects mentioned down here.
__all__ = (
    "config",
    "AdvancedGradientPalette",
    "BoundingBox",
    "CairoGraphDrawer",
    "ClusterColoringPalette",
    "Clustering",
    "CohesiveBlocks",
    "Configuration",
    "Cover",
    "Cut",
    "DefaultGraphDrawer",
    "Dendrogram",
    "DyadCensus",
    "Edge",
    "EdgeSeq",
    "FittedPowerLaw",
    "Flow",
    "GradientPalette",
    "Graph",
    "GraphBase",
    "GraphSummary",
    "Histogram",
    "InternalError",
    "Layout",
    "Matching",
    "MatplotlibGraphDrawer",
    "Matrix",
    "Palette",
    "Plot",
    "Point",
    "PrecalculatedPalette",
    "RainbowPalette",
    "Rectangle",
    "RunningMean",
    "TriadCensus",
    "UniqueIdGenerator",
    "Vertex",
    "VertexClustering",
    "VertexCover",
    "VertexDendrogram",
    "VertexSeq",
    "autocurve",
    "color_name_to_rgb",
    "color_name_to_rgba",
    "community_to_membership",
    "compare_communities",
    "convex_hull",
    "default_arpack_options",
    "disjoint_union",
    "get_include",
    "hsla_to_rgba",
    "hsl_to_rgb",
    "hsva_to_rgba",
    "hsv_to_rgb",
    "is_bigraphical",
    "is_degree_sequence",
    "is_graphical",
    "is_graphical_degree_sequence",
    "intersection",
    "known_colors",
    "load",
    "mean",
    "median",
    "palettes",
    "percentile",
    "plot",
    "power_law_fit",
    "quantile",
    "read",
    "rescale",
    "rgba_to_hsla",
    "rgb_to_hsl",
    "rgba_to_hsva",
    "rgb_to_hsv",
    "save",
    "set_progress_handler",
    "set_random_number_generator",
    "set_status_handler",
    "split_join_distance",
    "summary",
    "umap_compute_weights",
    "union",
    "write",
    "__igraph_version__",
    "__version__",
    "__version_info__",
    # enums and stuff
    "ADJ_DIRECTED",
    "ADJ_LOWER",
    "ADJ_MAX",
    "ADJ_MIN",
    "ADJ_PLUS",
    "ADJ_UNDIRECTED",
    "ADJ_UPPER",
    "ALL",
    "ARPACKOptions",
    "BFSIter",
    "BLISS_F",
    "BLISS_FL",
    "BLISS_FLM",
    "BLISS_FM",
    "BLISS_FS",
    "BLISS_FSM",
    "DFSIter",
    "GET_ADJACENCY_BOTH",
    "GET_ADJACENCY_LOWER",
    "GET_ADJACENCY_UPPER",
    "IN",
    "OUT",
    "REWIRING_SIMPLE",
    "REWIRING_SIMPLE_LOOPS",
    "STAR_IN",
    "STAR_MUTUAL",
    "STAR_OUT",
    "STAR_UNDIRECTED",
    "STRONG",
    "TRANSITIVITY_NAN",
    "TRANSITIVITY_ZERO",
    "TREE_IN",
    "TREE_OUT",
    "TREE_UNDIRECTED",
    "WEAK",
)

{'ndarray': 'numpy.ndarray', 'matrix': 'numpy.matrix'}._format_mapping

{'ndarray': 'numpy.ndarray', 'matrix': 'numpy.matrix'}._layout_mapping

{'ndarray': 'numpy.ndarray', 'matrix': 'numpy.matrix'}.layout_fruchterman_reingold_3d

{'ndarray': 'numpy.ndarray', 'matrix': 'numpy.matrix'}.layout_kamada_kawai_3d

{'ndarray': 'numpy.ndarray', 'matrix': 'numpy.matrix'}.layout_random_3d

{'ndarray': 'numpy.ndarray', 'matrix': 'numpy.matrix'}.layout_grid_3d

{'ndarray': 'numpy.ndarray', 'matrix': 'numpy.matrix'}.layout_sphere

{'ndarray': 'numpy.ndarray', 'matrix': 'numpy.matrix'}.layout_circle

{'ndarray': 'numpy.ndarray', 'matrix': 'numpy.matrix'}.Read