text:
import datetime
from datetime import datetime
import copy
import pandas as pd
import numpy as np
import math
import ciw
import imp
import re
import statistics
import matplotlib.pyplot as plt

transitions = imp.load_source('transitions', 'src/transitions.py')
custom_ciw = imp.load_source('custom_ciw', 'src/custom_ciw.py')

#===============================================================

def gather_set(data):
    """Gets all days, weeks, year from data."""
    set_dates = set()
    for c in data.columns:
        set_c = set(data[c])
        set_dates = set_dates.union(set_c)
    listed_dates = list(set_dates)
    return listed_dates

def averagePatternArrival(data, activity_codes, headers):
    """Calculates average number recorded per day, for each activity."""
    data_2 = copy.deepcopy(data)
    data_2.replace({' ': pd.NaT}, inplace=True)
    cols_remove = [col for col in data_2.columns if col not in headers]
    data_2.drop(cols_remove, axis = 1, inplace=True)
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

    for column in activity_codes.values():
        data_2[column] = data_2[column].dt.strftime('%A, %U, %y')

    # Get all dates
    list_gather_set_days = gather_set(data_2)

    day_dict = {}
    for day in days:
        list_day_name = 'list_' + day
        list_day = [i for i in list_gather_set_days if day in i]
        list_day = sorted(list_day)
        day_dict[list_day_name] = list_day

    all_average_in_day = {}
    for key, activity in activity_codes.items():
        average_in_day = [data_2[activity].value_counts().reindex(value).mean() for value in day_dict.values()]
        all_average_in_day[key] = average_in_day
    cap_pattern = {activity: [0 if pd.isna(c) == True else int(round(c,0)) for c in cap]  for activity, cap in all_average_in_day.items()}

    return cap_pattern

def getRoutes(sim_type, data, letters):
    """Get raw data actual pathways and convert to node routes."""
    all_actual_pathways = [data[i] for i in range(len(data))]
    split_routes = [re.findall('.',route) for route in all_actual_pathways]
    Routes = [[letters.index(r) + 1 for r in route] for route in split_routes]
    if sim_type == 'Raw Pathways':
        Routes = [[1] + route for route in Routes]
    return Routes

def condense_patterns(letters, capacity_pattern_dict):
    """Combines capacity for multiples of acitvity.

    If data was formatted using double codes - Get capacity and merge to general single code
    """
    pattern_dict = {}
    for letter in letters:
        activity_pattern = []
        pattern = [0 for i in range(7)]
        for code, values in capacity_pattern_dict.items():
            if code[0] == letter:
                activity_pattern.append(values)
        for l in range(len(activity_pattern)):
            pattern = [sum(x) for x in zip(pattern, activity_pattern[l])]
        pattern_dict[letter] = pattern
    return pattern_dict

def define_input_capacity(data, activity_codes, headers, letters, original_name):
    """Define server capacity from average performed each named day."""
    capacity_pattern_dict = averagePatternArrival(data, activity_codes, headers)

    if original_name == 'original_formatted':
        if letters[0] == 'Dummy':
            letters = letters[1:]
        capacity_pattern_dict = condense_patterns(letters, capacity_pattern_dict)

    return capacity_pattern_dict

def define_input_routing(sim_type, data, letters, Matrix_prob):
    """Define routes for simulation.

    + Raw Pathways and Process Medoids: use get routes
    + Full Transitions and Clustered Trnasitions: use transition matrix
    """
    if sim_type == 'Raw Pathways' or sim_type == 'Process Medoids':
        Routes = getRoutes(sim_type, data, letters)
    else:
        Routes = Matrix_prob
    return Routes

#===============================================================

def define_arrivals(sim_type, letters, individuals, arrivals, Routes):
    """Set up arrivals distribution from arrivals table.

    + Raw Pathways: Limited Exponential for dummy node of total arrival rate
    + Full Transitions: Exponential activiy arrival rate
    + Clustered Transitions: Class per cluster with Exponential activity arrival rate
    + Process Medoids: Class per centroid with Exponential activity arrival rate
    """
    if sim_type == 'Raw Pathways':
        Arrival = [custom_ciw.LimitedExponential(arrivals[0][0],individuals)] + [ciw.dists.NoArrivals() for i in letters]

    if sim_type == 'Full Transitions':
        Arrival = [ciw.dists.Exponential(arrivals[0][code]) if arrivals[0][code] != 0 else ciw.dists.NoArrivals() for code in range(len(letters))]

    if sim_type == 'Clustered Transitions':
        Arrival = {}
        for c, class_arrivals in enumerate(arrivals):
            arrival_dist = [ciw.dists.Exponential(class_arrivals[code]) if class_arrivals[code] != 0 else ciw.dists.NoArrivals() for code in range(len(letters))]
            class_name = 'Class '+ str(c)
            Arrival[class_name] = arrival_dist

    if sim_type == 'Process Medoids':
        Arrival = {}
        for c, route in enumerate(Routes):
            arrival_dist = [ciw.dists.Exponential(arrivals[0][c]) if route[0] - 1 == code else ciw.dists.NoArrivals() for code in range(len(letters))]
            class_name = 'Class '+ str(c)
            Arrival[class_name] = arrival_dist

    return Arrival

def define_service(sim_type, letters, service, cluster_k):
    """Set up service rate from service table.

    Either Deterministic or Exponential based on user choice.

    + Raw Pathways: Service rate per activity plus deterministic 0 for dummy arrival node
    + Full Transitions: Service rate per activity
    + Clustered Transitions: Class per cluster with Service rate per activity
    + Process Medoids: Class per centroid with Service rate per activity
    """
    # Det 0.1 as default
    Service_distribution = []
    for code in letters:
        if service[code][0] == 'Deterministic':
            code_service = ciw.dists.Deterministic(service[code][1])
        else:
            code_service = ciw.dists.Exponential(1/service[code][1])
        Service_distribution.append(code_service)

    if sim_type== 'Raw Pathways':
        Service = [ciw.dists.Deterministic(0)] + Service_distribution

    elif sim_type == 'Full Transitions':
        Service = Service_distribution

    else:
        Service = {}
        for c in range(cluster_k):
            class_name = 'Class '+ str(c)
            Service[class_name] = Service_distribution

    return Service

def pattern_server_schedules(cap_pattern, total_arrival_days):
    """Convert the capaity pattern into ciw format."""
    num_weeks = math.ceil(total_arrival_days/7)
    total_arrival_days_cap_pattern = [cap_pattern for i in range(num_weeks)]
    server_number = [0] + [item for sublist in total_arrival_days_cap_pattern for item in sublist]

    server_schedule = [[server, time] for time, server in enumerate(server_number)]
    server_schedule.pop(0)

    return server_schedule

def build_server_schedules(sim_type, cap_pattern, total_arrival_days, week):
    """Build the 5 day or 7 day weekly capacity pattern, for the desired number of total days."""
    weekly_pattern = [pattern + [0, 0] if len(pattern) == 5 else pattern for pattern in cap_pattern.values()]

    all_server_schedules = []
    for pattern in weekly_pattern:
        server_schedule = pattern_server_schedules(pattern, total_arrival_days)
        all_server_schedules.append(server_schedule)

    if sim_type == 'Raw Pathways':
        if week == '5 days':
            dummy_pattern = [total_arrival_days for _ in range(5)] + [0, 0]
            dummy_server = [pattern_server_schedules(dummy_pattern, total_arrival_days)]
        else:
            dummy_server = [[[total_arrival_days, i] for i in range(1, total_arrival_days)]]
        all_server_schedules = dummy_server + all_server_schedules

    return all_server_schedules

def define_servers(sim_type, warm_type, letters, overall_period, capacity, week, warm):
    """Define server schedules dependant on warm start type.

    + Itterative: increase total arrival days to cover period
    + Warm Start: reduce capacity to 0 for specified days per activity
    """
    if warm_type == 'Itterative':
        total_arrival_days = int(warm*overall_period)
    else:
        total_arrival_days = int(1.5*overall_period)
    all_server_schedules = build_server_schedules(sim_type, capacity, total_arrival_days, week)

    if warm_type == 'Warm Start':
        if sim_type == 'Raw Pathways':
            warm = [0] + warm
        all_server_schedules_WS = copy.deepcopy(all_server_schedules)
        for i, wait in enumerate(warm):
            for w in range(wait):
                all_server_schedules_WS[i][w][0] = 0
        Schedules = all_server_schedules_WS
    else:
        Schedules = all_server_schedules

    return Schedules

#================= Plots =========================

def subplot_shape(size):
    """
    Get the most square shape for the plot axes values.
    If only one factor, increase size and allow for empty slots.
    """
    facts = [[i, size//i] for i in range(1, int(size**0.5) + 1) if size % i == 0]
    # re-calc for prime numbers larger than 3 to get better shape
    while len(facts) == 1:
        size += 1
        facts = [[i, size//i] for i in range(1, int(size**0.5) + 1) if size % i == 0]

    sum_facts = [abs(pair[0]-pair[1]) for pair in facts]
    smallest = min(sum_facts)
    shape = facts[sum_facts.index(smallest)]
    return shape

#---------------- Wait times ------------------

def plot_totaltime(save_location, simulation_name, df_all):
    """Plot histogram of total time in system."""
    fig, ax = plt.subplots(1, 1, figsize=[10, 10])

    # data for plot
    waits = [df_all['totaltime'][i] for i in range(len(df_all))]
    if int(max(waits)) < 20:
        bins = [i for i in range(0,int(max(waits)), 1)]
    else:
        bins = [i for i in range(0,int(max(waits)), 5)]

    # create plot
    ax.hist(waits, bins=bins)
    ax.set_title('Total Time in System - ' + simulation_name)
    ax.set_ylabel('Frequency')
    ax.set_xlabel('Days')
    if int(max(waits)) <= 40:
        ax.set_xticks(bins)
    elif int(max(waits)) <= 50:
        ax.set_xticks([i for i in range(0,int(max(waits)),10)])
        ax.set_xticks(bins)
    elif int(max(waits)) <= 100:
        ax.set_xticks([i for i in range(0,int(max(waits)),25)])
    else:
        ax.set_xticks([i for i in range(0,int(max(waits)),50)])

    # save
    plot_name = 'TotalTime_' + simulation_name
    fig.savefig(save_location + 'Plots/Simulation/' + plot_name + '.png', bbox_inches='tight', facecolor="None")
    plt.close()

def plot_activity_waittimes(save_location, simulation_name, df_all, letters):
    """Plot histogram of waiting time per activity."""
    if letters[0] == '':
        letters = letters[1:]

    size = len(letters)
    shape = subplot_shape(size)
    fig, ax = plt.subplots(shape[1], shape[0], figsize=[shape[0]*5, shape[1]*5])
    pos = 0

    for l, letter in enumerate(letters):
        # data for plot
        waits = [df_all[letter][i] for i in range(len(df_all))]
        if simulation_name == 'original':
            waits = [x for x in waits if str(x) != 'nan']
        elif simulation_name == 'original_formatted':
            waits = [y for x in waits for y in x if str(y) != 'nan']
        else:
            waits = [y for x in waits for y in x if y != ' ']

        # if no waits, plot empty
        if sum(waits) != 0:
            if int(max(waits)) < 20:
                bins = [i for i in range(0,int(max(waits)), 1)]
            else:
                bins = [i for i in range(0,int(max(waits)), 5)]

            # create plot
            locy = int(pos/shape[0])
            locx = pos%shape[0]
            ax[locy, locx].hist(waits, bins=bins)
            ax[locy,locx].set_title(letter)
            ax[locy,locx].set_ylabel('Frequency', fontsize=14)
            ax[locy,locx].set_xlabel('Wait days', fontsize=14)
            if int(max(waits)) <= 30:
                ax[locy,locx].set_xticks(bins)
            elif int(max(waits)) <= 50:
                ax[locy,locx].set_xticks([i for i in range(0,int(max(waits)),10)])
                ax[locy,locx].set_xticks(bins)
            elif int(max(waits)) <= 100:
                ax[locy,locx].set_xticks([i for i in range(0,int(max(waits)),25)])
            else:
                ax[locy,locx].set_xticks([i for i in range(0,int(max(waits)),50)])
        else:
            locy = int(pos/shape[0])
            locx = pos%shape[0]
            ax[locy,locx].set_title(letter + ' - Always First Activity')
        pos+=1

    # remove blank subplots
    for i in range(shape[0]*shape[1] - size):
        fig.delaxes(ax.flatten()[size+i])
    fig.subplots_adjust(wspace=0.5, hspace=0.5)
    fig.suptitle('Wait Time per Activity - ' + simulation_name, y=0.94, fontsize=14)

    plot_name = 'Activity_Waits_' + simulation_name
    fig.savefig(save_location + 'Plots/Simulation/' + plot_name + '.png', bbox_inches='tight', facecolor="None")
    plt.close()

#---------------- Utilisation ------------------

def cap_utilisation(sim_recs, letters, min_date, max_date):
    """Gets capacity used per simulation day.

    Number remaining in queue at the end of each day recorded.
    """

    all_cap_used = {}
    all_queue_remaining = {}
    for i, activity in enumerate(letters):
        n = i+2
        sim_recs_n = sim_recs[sim_recs.node == n]

        cap_used = []
        queue_remaining = []
        for day in range(min_date, max_date+1): # changed
            start_day = day - 1
            finish_day = day
            sec = sim_recs_n.loc[(sim_recs_n.service_start_date >= start_day) &
                                         (sim_recs_n.service_start_date < finish_day)]
            # cpacity
            cap_used_day = len(sec)
            cap_used.append(cap_used_day)

            # queue remaining
            service_dates = [i for i in sec.service_end_date]
            if len(set(service_dates)) == 1:
                size = min(sec.queue_size_at_departure)
            else:
                last_date = sec.service_end_date.max()
                size = sec.loc[sec.service_end_date == last_date].queue_size_at_departure.max()
            queue_remaining.append(size)

        all_cap_used[activity] = cap_used
        all_queue_remaining[activity] = queue_remaining

    return(all_cap_used, all_queue_remaining)

def actual_cap_reduced(server_schedule, all_cap_used, letters, min_date, max_date):
    """Gets capacity per day over server schedule period."""
    server_schedule = copy.deepcopy(server_schedule)
    # remove dummy server schedule
    if len(server_schedule) != len(letters):
        server_schedule.pop(0)

    all_actual_cap = [[i[0] for i in j] for j in server_schedule]
    actual_cap = [i[min_date-1:max_date] for i in all_actual_cap] # changed to collect from min to max date

    # added to account for ciw schedules loop
    repeat = max_date - len(all_actual_cap[0])
    if repeat > 0:
        repeat_extra = [i[0:repeat] for i in all_actual_cap]
        actual_cap = [actual + extra for actual, extra in zip(actual_cap, repeat_extra)]

    actual_cap_dict = {letter: actual_cap[i] for i,letter in enumerate(letters)}
    return actual_cap_dict

def cap_util_percentage(server_schedule, all_cap_used, letters, actual_cap):
    """Calculates percentage used of capacity avaliable each simulation day."""
    all_cap_perc_used = {}
    for i, activity in enumerate(letters):
        all_cap_used_n = all_cap_used[activity]
        actual_cap_n = actual_cap[activity]

        cap_perc_used = []
        for used, avaliable in zip(all_cap_used_n, actual_cap_n):
            if avaliable != 0:
                cap_perc_used.append(used/avaliable)
            else:
                cap_perc_used.append(np.nan)

        all_cap_perc_used[activity] = cap_perc_used

    return all_cap_perc_used

def plot_Perc_Cap_Used(all_cap_perc_used, save_location, simulation_name, min_date, max_date):
    """Plot the percentage of capacity used per simulation day."""
    plot_max = max_date - min_date + 1 # added to find the plot range
    x_days = [i for i in range(plot_max)] # changed to plot over plot_max range

    size = len(all_cap_perc_used)
    shape = subplot_shape(size)
    fig, ax = plt.subplots(shape[1], shape[0], figsize=[shape[0]*5, shape[1]*5])
    pos = 0

    for code, cap_perc in all_cap_perc_used.items():
        # create plot
        locy = int(pos/shape[0])
        locx = pos%shape[0]
        pos+=1
        ax[locy,locx].plot(x_days,cap_perc, marker='.') # added marker
        ax[locy,locx].set_title(code)
        ax[locy,locx].set_ylabel('Percentage', fontsize=14)
        ax[locy,locx].set_xlabel('Day', fontsize=14)

    # remove blank subplots
    for i in range(shape[0]*shape[1] - size):
        fig.delaxes(ax.flatten()[size+i])
    fig.suptitle('Percentage of Capacity Used - ' + simulation_name, y=0.9, fontsize=14)
    fig.subplots_adjust(wspace=0.5, hspace=0.5)

    plot_name = 'Utilisation_Percent_' + simulation_name
    fig.savefig(save_location + 'Plots/Simulation/' + plot_name + '.png', bbox_inches='tight', facecolor="None")
    plt.close()

def plot_queue_remaining(all_queue_remaining, save_location, simulation_name, min_date, max_date):
    """Plot the queue remaining at the end of each simulation day."""
    plot_max = max_date - min_date + 1 # added to find the plot range
    x_days = [i for i in range(plot_max)] # changed to plot over plot_max range

    size = len(all_queue_remaining)
    shape = subplot_shape(size)
    fig, ax = plt.subplots(shape[1], shape[0], figsize=[shape[0]*5, shape[1]*5])
    pos = 0

    for code, queue in all_queue_remaining.items(): # changed variable name
        # create plot
        locy = int(pos/shape[0])
        locx = pos%shape[0]
        pos+=1
        ax[locy,locx].plot(x_days,queue, marker='.') # changed variable name, added marker
        ax[locy,locx].set_title(code)
        ax[locy,locx].set_ylabel('Number of Individuals', fontsize=14)
        ax[locy,locx].set_xlabel('Day', fontsize=14)

    # remove blank subplots
    for i in range(shape[0]*shape[1] - size):
        fig.delaxes(ax.flatten()[size+i])
    fig.suptitle('Number in Queue at End of Day - ' + simulation_name, y=0.9, fontsize=14)
    fig.subplots_adjust(wspace=0.5, hspace=0.5)

    plot_name = 'Utilisation_Queue_' + simulation_name
    fig.savefig(save_location + 'Plots/Simulation/' + plot_name + '.png', bbox_inches='tight', facecolor="None")
    plt.close()

def util_results(all_cap_perc_used, week_type, min_date):
    """Reports total average percentage capacity used and average percentage capacity used per named day."""

    starting_day_adjust = (min_date-1)%7 # added for itterative to adjust for not starting on monday

    util_results_dict = {}
    for activity, cap_perc_used in all_cap_perc_used.items():
        possible_days = [cap_perc for cap_perc in cap_perc_used if np.isnan(cap_perc) != True] # added - no. possible days
        all_util_100 = [round((len([i for i in cap_perc_used if i == 1.0])/len(possible_days))*100,2)] # percent of possible days run use 100% of capacity

        cap_perc_used = [np.nan]*starting_day_adjust +  cap_perc_used # added for itterative to adjust for not starting on monday

        day_perc = []
        for day in range(week_type):
            average_perc_day_list = [cap_per for i,cap_per in enumerate(cap_perc_used) if i % 7 == day] # collect all percent for named day
            average_perc_day_list = [average for average in average_perc_day_list if np.isnan(average) != True] # added for warm start and itterative - remove all nan
            if len(average_perc_day_list) == 0: # if never run on day
                day_perc.append(np.nan)
            else:
                average_perc_day = (sum(average_perc_day_list)/len(average_perc_day_list))*100
                day_perc.append(round(average_perc_day,2))

        util_results_dict[activity] = all_util_100 + day_perc

    df_util_results = pd.DataFrame.from_dict(util_results_dict, orient='index')
    columns_names = ['Percentage Util 100%'] + ['Average Percent Util Day_' + str(day) for day in range(week_type)]
    df_util_results.columns = columns_names

    return df_util_results

def run_utilisation_results(sim_recs, letters, server_schedule, week_type, save_location, simulation_name):
    """Run utilisation results, including results table, percentage utilisation plot and remaining queue plot.

    Utilisation defined as percentage used of capacity avaliable.
    """
    # remove dummy node letter
    if letters[0] == '':
        letters = letters[1:]

    max_date = math.ceil(sim_recs.exit_date.max()) # added
    min_date = math.ceil(sim_recs.exit_date.min()) # added

    all_cap_used, all_queue_remaining = cap_utilisation(sim_recs, letters, min_date, max_date)
    actual_cap = actual_cap_reduced(server_schedule, all_cap_used, letters, min_date, max_date)
    all_cap_perc_used = cap_util_percentage(server_schedule, all_cap_used, letters, actual_cap)

    plot_Perc_Cap_Used(all_cap_perc_used, save_location, simulation_name, min_date, max_date)
    plot_queue_remaining(all_queue_remaining, save_location, simulation_name, min_date, max_date)

    df_util_results = util_results(all_cap_perc_used, week_type, min_date)

    return df_util_results

#-------------------- Trials ---------------------

def list_convert_results(raw_results, orient):
    """Convert dataframe results to list for confidence interval calculation preperation."""
    if orient == 'row':
        results = [r if isinstance(r, list) != True else r[1] for r in raw_results.iloc[0]]
        results = results[1:]
    if orient == 'column':
        results = [r for r in raw_results.iloc[:,1]]
    return results

def Confidence_Interval(results):
    """Calculate the mean and confidence interval."""
    pop_mean = float(statistics.mean(results))
    pop_std = float(statistics.pstdev(results))
    sq_n = float(math.sqrt(len(results)))
    z_95 = 1.96

    CI_L = pop_mean - z_95*(pop_std/sq_n)
    CI_U = pop_mean + z_95*(pop_std/sq_n)

    results = [round(pop_mean,2), [round(CI_L,2), round(CI_U,2)]]
    return results

def calculate_CI(All_Results):
    """Perform confidence interval calculation"""
    all_CI_results = []
    for result in range(len(All_Results[0])):
        converted_results = [row[result] for row in All_Results]
        results_CI = Confidence_Interval(converted_results)
        all_CI_results.append(results_CI)
    return all_CI_results

def T1_CI_results(T1, dataframe_T1, simulation_name, target):
    """Summarise trials results into single entry for table 1."""
    all_CI_results = calculate_CI(T1)
    all_CI_results = [simulation_name] + all_CI_results
    all_CI_results[3] = [target, all_CI_results[3]]
    # add to dataframe_T1
    all_results_T1_series = pd.Series(all_CI_results, index = dataframe_T1.columns)
    dataframe_T1 = dataframe_T1.append(all_results_T1_series, ignore_index=True)
    return dataframe_T1

def T2_T4_CI_results(T_2_4, dataframe_T2_T4, letters, model_name):
    """Summarise trial runs results into single entry for table 2 or 4."""
    all_CI_results = calculate_CI(T_2_4)

    # add to dataframe
    all_waiting_results = pd.DataFrame([[activity, all_CI_results[i]] for i,activity in enumerate(letters)])
    all_waiting_results.columns = ['Activity', model_name]
    dataframe_T2_T4 = dataframe_T2_T4.join(all_waiting_results.set_index('Activity'), on='Activity')
    return dataframe_T2_T4

def Standard_Deviation(All_Results, pos):
    """Calculate the standard deviation to evaluate number of trial runs required."""
    converted = [row[pos] for row in All_Results]
    if isinstance(converted[0], list) == True:
        converted = [row[1] for row in converted]

    stdev = float(statistics.pstdev(converted))

    return stdev

def plot_stdev(save_location, simulation_name, table_name, All_StDev, dataframe_T, runs, letters, orient):
    """Plot Standard deviation results per run.

    Figure not callable within GUI.
    """
    if orient == 'columns':
        titles = [column for column in dataframe_T.columns if column != 'Name']
    if orient == 'letters':
        titles = letters

    x_values = [i for i in range(runs)]

    size = len(titles)
    shape = subplot_shape(size)
    fig, ax = plt.subplots(shape[1], shape[0], figsize=[shape[0]*5, shape[1]*5])
    for result in range(size):
        locx = int(result%shape[0])
        locy = int(result/shape[0])
        list_results = [row[result] for row in All_StDev]
        ax[locy,locx].plot(x_values,list_results)
        ax[locy,locx].set_title(titles[result])
        ax[locy,locx].set_ylabel('Population Standard Deviation')
        ax[locy,locx].set_xlabel('Run')

    # remove blank subplots
    for i in range(shape[0]*shape[1] - size):
        fig.delaxes(ax.flatten()[size+i])
    fig.subplots_adjust(wspace=0.5, hspace=0.5)
    plot_name = simulation_name + '_' + table_name
    fig.suptitle(plot_name, y=0.94, fontsize=14)
    fig.savefig(save_location + 'Plots/Simulation/Trials/' + plot_name + '.png', bbox_inches='tight', facecolor="None")
    plt.close()


text:
import re
import subprocess
from astropy.io import fits
from collections import deque

from gempy.eti_core.eti import ExternalTaskInterface as ETI
from .sextractoretifile import SExtractorETIFile
from .sextractoretiparam import SExtractorETIParam

__SYS_COMMAND__ = "sex"
__MIN_VERSION__ = [2, 8, 6]
__VERSION_COMMAND__ = [__SYS_COMMAND__, "--version"]
__REGEXP_GROUP_NAME__ = "v1"
__VERSION_REGEXP__ = ''.join(["^.*version (?P<", __REGEXP_GROUP_NAME__,
                              r">[\d+\.]+) .*$"])

class SExtractorETI(ETI):
    """This class coordinates the ETI as is relates to SExtractor"""
    def __init__(self, primitives_class=None, inputs=None, params=None,
                 mask_dq_bits=None, getmask=False):
        """
        Parameters
        ----------
        primitives_class: a PrimitivesBASE object
        inputs: list of AstroData objects
            AD objects to run through SExtractor
        params: dict
            A dict of command-line parameters
        mask_dq_bits: int/bool/None
            DQ(mask) bits which, if any is set, should have the data replaced
            by the median of the good data. This avoids the need to run files
            through deepcopy and applyDQPlane. Use "True" if the mask is a
            boolean array rather than integer
        getmask: bool
            make SExtractor produce an object mask and attach it to the outputs
        """
        super().__init__(primitives_class, inputs=inputs)
        self.add_param(SExtractorETIParam(params))
        self._mask_dq_bits = mask_dq_bits
        self._getmask = getmask

    def _version_regexp(self):
        """Compile a regular expression for matching the version
        number from SExtractor output"""
        return (re.compile(__VERSION_REGEXP__), __REGEXP_GROUP_NAME__)

    def check_version(self, command=__VERSION_COMMAND__,
                      minimum_version=__MIN_VERSION__):
        """
        Returns True if the installed SExtractor is OK to use
        """
        stdoutdata = self._execute(command)

        version_regexp, group_names = self._version_regexp()
        if isinstance(group_names, list):
            if len(group_names) != len(minimum_version):
                errmsg = ("Length of regexp groups {} is not equal to length"
                          " of minimum_version {}".format(len(group_names),
                                                          len(minimum_version)))
                raise OSError(errmsg)
            version = ".".join([version_regexp.match(str(stdoutdata)).group(name)
                                for name in group_names])
        else:
            version = version_regexp.match(str(stdoutdata)).group(group_names)

        if version is None:
            raise Exception("Unable to determine SExtractor version")

        match_values = [int(i) for i in version.split(".")]
        if match_values < minimum_version:
            raise Exception("Version {} of SExtractor is required. Version "
                      "{} installed".format('.'.join([str(i) for i in
                                                minimum_version]), version))
        return True

    def run(self):
        # Make a list of all the extensions in all the inputs
        self.file_objs = []
        # self.inputs is a list, but its items might be single slices
        # or sliceable AD objects, so cater for both
        for ad in self.inputs:
            try:
                [self.add_file(SExtractorETIFile(ext,
                        mask_dq_bits=self._mask_dq_bits)) for ext in ad]
            except TypeError:
                self.add_file(SExtractorETIFile(ad,
                              mask_dq_bits=self._mask_dq_bits))
        # Run the ETI
        try:
            self.prepare()
            self.execute()
            objdata = self.recover()
            # Attach the OBJCATs and OBJMASKs to each extension in each input
            # This is kind of ugly, so maybe want to look at it
            i = 0
            for ad in self.inputs:
                try:
                    for ext in ad:
                        ext.OBJCAT, objmask = objdata[i]
                        if self._getmask:
                            ext.OBJMASK = fits.getdata(objmask, ext=0)
                        i += 1
                except TypeError:
                    ad.OBJCAT, objmask = objdata[i]
                    if self._getmask:
                        ad.OBJMASK = fits.getdata(objmask, ext=0)
        finally:
            self.clean()
        return self.inputs

    def execute(self):
        cmd = [__SYS_COMMAND__]
        param_dict = self.param_objs[0].params

        # Parameters that have been sent as lists, with different values
        # for different inputs: stored as deques so we can popleft()
        list_params = {}

        # Add the config (.sex) file as first parameter
        cmd.extend(['-c', param_dict['config']])
        # Add all the other command-line arguments
        for parameter, value in param_dict.items():
            if isinstance(value, list):
                list_params.update({parameter: deque(value)})
            elif parameter != 'config':
                cmd.extend(['-'+parameter, str(value)])

        # Run SExtractor for each input file
        for file_obj in self.file_objs:
            files = ['-CATALOG_NAME', file_obj._catalog_file]
            if self._getmask:
                files.extend(['-CHECKIMAGE_TYPE', 'SEGMENTATION',
                              '-CHECKIMAGE_NAME', file_obj._objmask_file])
            if file_obj._dq_image is not None:
                files.extend(['-FLAG_IMAGE', file_obj._dq_image])

            # Add all the input-specific command-line arguments
            [files.extend([param, value.popleft()]) for param, value in
             list_params.items()]
            files.append(file_obj._sci_image)
            self._execute(cmd+files)

    def recover(self):
        for par in self.param_objs:
            par.recover()
        # Return a list of OBJCATs
        return [fil.recover() for fil in self.file_objs]

    def _execute(self, command):
        if self.inQueue is not None:
            self.inQueue.put(command)
            result = self.outQueue.get()
            if isinstance(result, subprocess.CalledProcessError):
                raise RuntimeError("CalledProcessError from: {}".
                                   format(result.output.decode("utf-8")))
            elif isinstance(result, Exception):
                raise result
        else:
            pipe_out = subprocess.Popen(command,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE,
                                        universal_newlines=True)
            (result, stderrdata) = pipe_out.communicate()
            if pipe_out.returncode != 0:
                errmsg = ("SExtractor returned an error:\n"
                          "{}{}".format(result, stderrdata))
                raise Exception(errmsg)
        return result

ext: py

max_stars_repo_name: DBerke/DRAGONS

max_issues_repo_name: DBerke/DRAGONS

max_forks_repo_name: DBerke/DRAGONS

reason: from astropy


text:
import cv2
import numpy as np
import os
import sys

size_image = 50
num_col = 4
class_id = None

def onMouse(event, x, y, flags, param):
    global class_id
    if event == cv2.EVENT_LBUTTONDOWN:
        print('x = %d, y = %d' % (x, y))
        for index in range(7):
            id_next = index + 1
            if index * size_image < y < id_next * size_image:
                for i in range(num_col):
                    i_next = i + 1
                    if i * size_image < x < i_next * size_image:
                        print('class id = ', i + index * num_col)
                        class_id = i + index * num_col

def load_image_bbgt_to_list():
    path_image = './bbgt/'
    list_files_image = []
    # r=root, d=directories, f = files
    for r, d, f in os.walk(path_image):
        for file in f:
            if '.png' in file:
                list_files_image.append(os.path.join(r, file))
    return list_files_image

def label_window():
    list_files_image = load_image_bbgt_to_list()
    list_files_image.sort()
    main_title_window = 'Test'

    flag_run_file_label = True
    while flag_run_file_label:
        img = cv2.imread(list_files_image[0])
        img = cv2.resize(img, (size_image, size_image), interpolation=cv2.INTER_AREA)
        numpy_horizontal_concat = img
        for i in range(1, len(list_files_image)):
            img = cv2.imread(list_files_image[i])
            img = cv2.resize(img, (size_image, size_image), interpolation=cv2.INTER_AREA)
            if i < num_col:
                numpy_horizontal_concat = np.concatenate((numpy_horizontal_concat, img), axis=1)
            if i == num_col:
                numpy_horizontal_concat1 = img
            if i > num_col and (i < num_col * 2):
                numpy_horizontal_concat1 = np.concatenate((numpy_horizontal_concat1, img), axis=1)
            if i == num_col * 2:
                numpy_horizontal_concat2 = img
            if i > num_col * 2 and (i < num_col * 3):
                numpy_horizontal_concat2 = np.concatenate((numpy_horizontal_concat2, img), axis=1)
            if i == num_col * 3:
                numpy_horizontal_concat3 = img
            if i > num_col * 3 and (i < num_col * 4):
                numpy_horizontal_concat3 = np.concatenate((numpy_horizontal_concat3, img), axis=1)
            if i == num_col * 4:
                numpy_horizontal_concat4 = img
            if i > num_col * 4 and (i < num_col * 5):
                numpy_horizontal_concat4 = np.concatenate((numpy_horizontal_concat4, img), axis=1)
            if i == num_col * 5:
                numpy_horizontal_concat5 = img
            if i > num_col * 5 and (i < num_col * 6):
                numpy_horizontal_concat5 = np.concatenate((numpy_horizontal_concat5, img), axis=1)
            if i == num_col * 6:
                numpy_horizontal_concat6 = img
            if i > num_col * 6:
                numpy_horizontal_concat6 = np.concatenate((numpy_horizontal_concat6, img), axis=1)

            if i == 26:
                img = np.zeros_like(img)
                # img = cv2.resize(img, (size_image, size_image), interpolation=cv2.INTER_AREA)
                numpy_horizontal_concat6 = np.concatenate((numpy_horizontal_concat6, img), axis=1)

        combine_image = np.concatenate((numpy_horizontal_concat, numpy_horizontal_concat1, numpy_horizontal_concat2
                                        , numpy_horizontal_concat3, numpy_horizontal_concat4
                                        , numpy_horizontal_concat5, numpy_horizontal_concat6), axis=0)
        cv2.namedWindow(main_title_window, cv2.WINDOW_AUTOSIZE)
        cv2.imshow(main_title_window, combine_image)
        cv2.setMouseCallback(main_title_window, onMouse)
        cv2.waitKey()
        if class_id is not None:
            cv2.destroyWindow(main_title_window)
            break

if __name__ == "__main__":
    label_window()

    '''
    + This module return toa do cua cac anh bien bao dung cho chon loai bien bao.
    + click image => toa do => save id class
    '''

reason: import numpy


text:
import dataclasses
from typing import Any, Dict, List, Optional, Tuple

import numpy as np
import pyproj
import shapely.ops
import shapely.speedups
import shapely.vectorized
from descartes import PolygonPatch
from shapely.coords import CoordinateSequence
from shapely.geometry import LineString, Point, Polygon, box
from shapely.geometry import mapping as geojson_mapping

from .exceptions import CoordSystemInconsistency, NotAPolygon, OutsideSupportedArea

if shapely.speedups.available:
    shapely.speedups.enable()

BLUE = "#6699cc"
GRAY = "#999999"
RED = "#8B0000"
GREEN = "#32CD32"

# all mappings are FROM WGS84, epsg:4326
# all mappings are to coordinate systems measured in meters.
global_proj = pyproj.Proj("epsg:4326")
TRANSFORMER_MAPPING: Dict[str, pyproj.Transformer] = {
    "epsg:3035": pyproj.Transformer.from_proj(
        global_proj, pyproj.Proj("epsg:3035")
    ),  # most of europe
    "epsg:5634": pyproj.Transformer.from_proj(
        global_proj, pyproj.Proj("epsg:5634")
    ),  # canaries
    "epsg:6269": pyproj.Transformer.from_proj(
        global_proj, pyproj.Proj("epsg:8826")
    ),  # contiguous usa and canada
}

REVERSE_TRANSFORMER_MAPPING: Dict[str, pyproj.Transformer] = {
    "epsg:3035": pyproj.Transformer.from_proj(
        pyproj.Proj("epsg:3035"), global_proj
    ),  # most of europe
    "epsg:5634": pyproj.Transformer.from_proj(
        pyproj.Proj("epsg:5634"), global_proj
    ),  # canaries
    "epsg:6269": pyproj.Transformer.from_proj(
        pyproj.Proj("epsg:8826"), global_proj
    ),  # contiguous usa and canada
}
# BOUNDING BOXES IN WGS84
BOUNDING_BOX_MAP = {
    "epsg:3035": Polygon(
        [(-16.1, 32.88), (-16.1, 84.17), (40.18, 84.17), (40.18, 32.88)]
    ),
    "epsg:5634": Polygon(
        [(-21.73, 24.6), (-21.73, 32.76), (-11.75, 32.76), (-11.75, 24.6)]
    ),
}

# maximum supported size is 15 hectares.
MAX_SUPPORTED_SIZE = 1.5e5  # in m^2.

@dataclasses.dataclass
class CalculationResult:
    n_humans: int
    points: List[Point]
    inner_polygon: Polygon
    outer_polygon: Optional[Polygon] = None

def polygon_from_geosjon_feature(feature: Dict[str, Any]) -> Polygon:
    """Construct a polygon from a geojson feature

    :param feature: feature
    :return: Polygon
    """
    coordinates = feature.get("geometry", {}).get("coordinates", [])
    if not coordinates:
        raise NotAPolygon("No coordinates supplied")
    # geojson nests stuff even more.
    if len(coordinates[0]) < 3:
        raise NotAPolygon("Cannot construct a polygon with less than 3 tuples.")
    return Polygon(coordinates[0])

def polygons_from_geojson_features(
    main_polygon_feature: Dict[str, Any], hole_polygon_features: List[Dict[str, Any]]
) -> List[Polygon]:
    """Generate coordinate sequences from a main feature and hole features.

    :param main_polygon_feature: the geojson feature that contains the main polygon.
    :param hole_polygon_features: List of geojson features that contain hole polygons.
    :return: list of coordinate sequences, the first is always the main feature,
        all remaining are the holes
    """
    return [polygon_from_geosjon_feature(main_polygon_feature)] + [
        polygon_from_geosjon_feature(f) for f in hole_polygon_features
    ]

def convert_wgs84_to_meter_system(polygon: Polygon) -> Tuple[str, Polygon]:
    """Convert a polygon in WGS84 to a polygon in a meter-based
        coordinate system.

    :param polygon: the polygon to be converted

    :return: name of coordinate system used, another polygon.
    """
    # first check canaries
    if polygon.within(BOUNDING_BOX_MAP["epsg:5634"]):
        return (
            "epsg:5634",
            shapely.ops.transform(TRANSFORMER_MAPPING["epsg:5634"].transform, polygon),
        )
    # then europe as whole
    if polygon.within(BOUNDING_BOX_MAP["epsg:3035"]):
        return (
            "epsg:3035",
            shapely.ops.transform(TRANSFORMER_MAPPING["epsg:3035"].transform, polygon),
        )

    raise OutsideSupportedArea("Could not convert to a meter-based coordinate system")

def multi_convert_to_meter_system(polygons: List[Polygon]) -> Tuple[str, List[Polygon]]:
    """Convert mutiple polygons to a meter based system

    :param polygons: polygons to be converted
    :raises: CoordSystemInconsistency: when the individually
        detected coordinate systems are different.
    :raises: NotAPolygon, when items is < 1
    :raises: OutsideSupportedArea, when any of the items is outside supported area.
    :return: name of coordinate system used, converted polygons
    """
    if not polygons:
        raise NotAPolygon("Must supply at least 1 item")

    first_coord, first_conv = convert_wgs84_to_meter_system(polygons[0])

    if len(polygons) == 1:
        return first_coord, [first_conv]

    other_coords, other_conv = [], []

    for other_polygon in polygons[1:]:
        other_co, other_cv = convert_wgs84_to_meter_system(other_polygon)
        other_coords.append(other_co)
        other_conv.append(other_cv)

    if not all(o == first_coord for o in other_coords):
        raise CoordSystemInconsistency("Detected multiple coordinate systems.")

    all_conv = [first_conv] + other_conv

    return first_coord, all_conv

def create_composite_polygon(polygons: List[Polygon]):
    """Create composite polygons out of a list of polygons.

    :param polygons: Polygons. Must have at least one item.
        All other items are considered to be holes.

    :return: Composite polygon.
    """
    # Get the "bounding" polygon
    bounding_poly = polygons[0]
    # Get holes / obstacles
    if len(polygons) > 1:
        # Generate union of holes
        holes = polygons[1:]
        holes = shapely.ops.unary_union(holes)
        bounding_poly = bounding_poly.difference(holes)
    return bounding_poly

def metered_points_to_geojson(
    points: List[Point], coordinate_system: str, polygon_id: int
) -> List[Dict[str, Any]]:
    """List of metered points to geojson object

    :param points: list of points
    :param coordinate_system: coordinate system
    :param: polygon id this point refers to.
    :return: geojson in WGS84.
    """
    return [
        {
            "type": "Feature",
            "geometry": geojson_mapping(
                shapely.ops.transform(
                    REVERSE_TRANSFORMER_MAPPING[coordinate_system].transform, point
                )
            ),
            "properties": {"polygon_id": polygon_id, "type": "marker"},
        }
        for point in points
    ]

def polygon_to_geojson(
    polygon: Polygon, coordinate_system: str, polygon_id: int, inner=True
):
    return {
        "type": "Feature",
        "geometry": geojson_mapping(
            shapely.ops.transform(
                REVERSE_TRANSFORMER_MAPPING[coordinate_system].transform, polygon
            )
        ),
        "properties": {"type": "inner" if inner else "outer", "polygon_id": polygon_id},
    }

def plot_line(ax, ob, color=BLUE):
    """Function to plot Shapely line object"""
    # Get objects x and y boundary as line
    x, y = ob.xy
    ax.plot(x, y, color=color, linewidth=3, solid_capstyle="round", zorder=1, alpha=0.5)

def plot_coords(ax, ob, color=GRAY):
    """Function to plot Shapely object coordinates"""
    # Get Shapely object point coords (e.g. polygon vertexes)
    coords = np.asarray(ob)
    #  If multiple points, scatter plot
    if coords.size > 2:
        ax.scatter(coords[:, 0], coords[:, 1], color=color)
    # If single, point plot
    else:
        ax.scatter(coords[0], coords[1], color=color)

def correct_line_intersection(coords: CoordinateSequence) -> Polygon:
    """Function that converts a list of 2D cartesian coordinates
    (importable into Shapely) into a closed Polygon.

     Coordinates are assumed to be in meters.
     It accounts for lines that potentially intersects.
    """
    # Generate LineString obj from coordinates
    polyline = LineString(coords)

    # Create tiny buffer region around polyline
    polyline_buffer = polyline.buffer(0.1)

    # Create bounding box of polyline_buffer
    bbox = box(*polyline_buffer.bounds)

    # Obtain a list of unconnected polygons by difference of bbox and polyline_buffer
    list_polygons = bbox.difference(polyline_buffer)

    # Valid polygon is the one with highest intersection area with original polygon
    polygon = max(
        list_polygons, key=lambda a: a.intersection(Polygon(polyline).buffer(0)).area
    )

    return polygon

def random_disk_insertion(
    n_iter: int, pts: np.ndarray, filtered: np.ndarray, r: float
) -> Tuple[int, np.ndarray]:
    """Random disk insertion.

    May at one point be optimized with numba, but couldn't get it to work for now.

    :param n_iter:
    :param pts:
    :param filtered:
    :param r:
    :return:
    """
    accept = 1
    r_square = r ** 2
    for i in range(min(n_iter, filtered.shape[0])):
        #  Generate random point inside ob
        pts_temp = filtered[i, :]
        # Calculate cartesian difference
        #  between pts_temp and all existing points in pts array
        pts_diff = pts[:accept] - pts_temp
        # Perform overlap boolean check with all existing points in pts array
        euclid_bool = (pts_diff * pts_diff).sum(1) > 4 * r_square
        # euclid_bool = random_disk_insertion(pts_diff, r)
        # Check that pts_temp doesn't overlap with any other
        if np.all(euclid_bool):
            # If all checks are positive, add point
            pts[accept, :] = pts_temp
            accept += 1

    return accept, pts

def populate_square(
    polygon: Polygon, iters: int = 1000, r: float = 1.0, fudge_factor: int = 2,
) -> np.ndarray:
    """Function to populate a polygon "polygon" with disks of radius "r".
    It performs "iters" attemps of disk insertion.
    It returns an array of the coordinates of the inserted disk centers.

    :returns: numpy nx2-array of floats.
    """

    # Define array of points
    minx, miny, maxx, maxy = polygon.bounds
    n_random = iters * fudge_factor
    random_arr_x = np.random.uniform(minx, maxx, n_random)
    random_arr_y = np.random.uniform(miny, maxy, n_random)
    random_arr = np.stack([random_arr_x, random_arr_y], axis=1)

    mask = shapely.vectorized.contains(polygon, random_arr_x, random_arr_y)

    filtered = random_arr[mask, :]
    pts = np.zeros(shape=filtered.shape)
    accept, pts = random_disk_insertion(iters, pts, filtered, r)
    return pts[:accept, :]

def calculate(
    polygon: Polygon,
    social_distance: float = 1.5,
    buffer_zone_size: Optional[float] = None,
) -> CalculationResult:
    """Do the math

    :param polygon: Polygon with coordinates in meters.
    :param social_distance: social distance in meters
    :param buffer_zone_size: size of buffer zone in meters.
    :return: n_points, coordinates
    """
    # If buffer zone is activated, generate buffer zone and substract it
    #  to initial polygon
    if buffer_zone_size is not None:
        outer_polygon = polygon.boundary.buffer(buffer_zone_size)
        inner_polygon = polygon.difference(outer_polygon)
    else:
        inner_polygon = polygon
        outer_polygon = None

    # Estimate n_iters
    n_iters = round(
        min(20 * (inner_polygon.area / (social_distance * social_distance)), 500000)
    )
    # Random insertion of disks in polygon -- returns disks' centers coordinates
    disk_centers = populate_square(inner_polygon, iters=n_iters, r=social_distance)

    # Convert to disk polygons
    disks = [Point(i[0], i[1]) for i in disk_centers]

    return CalculationResult(len(disks), disks, inner_polygon, outer_polygon)

def calc_result_to_serializable(
    calc_result: CalculationResult, coord_system: str, polygon_id: int
) -> Dict[str, Any]:
    points = metered_points_to_geojson(calc_result.points, coord_system, polygon_id)
    inner_boundary = polygon_to_geojson(
        calc_result.inner_polygon, coord_system, polygon_id
    )

    features = points + [inner_boundary]

    if calc_result.outer_polygon is not None:
        features.append(
            polygon_to_geojson(
                calc_result.outer_polygon, coord_system, polygon_id, inner=False
            )
        )

    return {
        "type": "FeatureCollection",
        "features": features,
        "properties": {"n_humans": calc_result.n_humans},
    }

if __name__ == "__main__":
    import matplotlib.pyplot as plt

    # Generate polygon
    pts_ext = [(0, 0), (0, 50), (50, 50), (50, 0), (0, 0)]
    pts_int = [(25, 0), (12.5, 12.5), (25, 25), (37.5, 12.5), (25, 0)][::-1]
    polygon = Polygon(pts_ext, [pts_int])

    # Random insertion of disks in polygon -- returns disks' centers coordinates
    disk_centers = populate_square(polygon, iters=30000)
    # Convert to disk polygons
    disks = [Point(i[0], i[1]).buffer(1) for i in disk_centers]

    # Plotting
    fig = plt.figure(1, figsize=(10, 4), dpi=180)
    ax = fig.add_subplot(121, aspect="equal")

    plot_coords(ax, polygon.interiors[0])
    plot_coords(ax, polygon.exterior)
    patch = PolygonPatch(polygon, facecolor=BLUE, edgecolor=GRAY, alpha=0.5, zorder=2)
    ax.add_patch(patch)
    ax.set_title("No boundary")

    for i, disk in enumerate(disks):
        # plot_coords(ax, disk_centers[i])
        patch = PolygonPatch(disk, facecolor=RED, edgecolor=GRAY, alpha=0.5, zorder=2)
        ax.add_patch(patch)

    # Generate polygon boundary
    boundary = polygon.boundary.buffer(5)

    # Random insertion of disks in polygon with boundary zone substracted
    #  -- returns disks' centers coordinates
    disk_centers = populate_square(polygon.difference(boundary), iters=30000)
    # Convert to disk polygons
    disks = [Point(i[0], i[1]).buffer(1) for i in disk_centers]

    # Plotting
    fig = plt.figure(1, figsize=(10, 4), dpi=180)
    ax = fig.add_subplot(122, aspect="equal")

    plot_coords(ax, polygon.interiors[0])
    plot_coords(ax, polygon.exterior)
    patch = PolygonPatch(polygon, facecolor=BLUE, edgecolor=GRAY, alpha=0.5, zorder=2)
    ax.add_patch(patch)
    patch_b = PolygonPatch(
        boundary.intersection(polygon),
        facecolor=GREEN,
        edgecolor=GRAY,
        alpha=0.5,
        zorder=2,
    )
    ax.add_patch(patch_b)

    for i, disk in enumerate(disks):
        # plot_coords(ax, disk_centers[i])
        patch = PolygonPatch(disk, facecolor=RED, edgecolor=GRAY, alpha=0.5, zorder=2)
        ax.add_patch(patch)
    ax.set_title("With Boundary")
    plt.show()

max_stars_repo_name: TomaLaPlazaConCabeza/web-app

max_issues_repo_name: TomaLaPlazaConCabeza/web-app

max_forks_repo_name: TomaLaPlazaConCabeza/web-app


text:
from torch.utils.data import Subset
from PIL import Image
from torchvision.datasets import MNIST
from base.torchvision_dataset import TorchvisionDataset
import glob,os,torchvision
import numpy as np
from .preprocessing import get_target_label_idx, global_contrast_normalization
import cv2
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import torchvision.transforms as transforms
root_ = ['/home/mhkim/oc_datatsets/classifier_OC/train_oc',
         '/home/mhkim/oc_datatsets/classifier_OC/test_oc_',
         '/home/mhkim/oc_datatsets/classifier_OC/clear_person_ssim']

def Check_32pixel_images(path_full):
    list_path = []
    for _path in path_full:
        img = cv2.imread(_path)
        if min(img.shape[0], img.shape[1]) < 32:
            pass
        else:
            list_path.append(_path)
    return list_path

class MNIST_Dataset(TorchvisionDataset):
    def __init__(self, root: str, normal_class=1):
        super().__init__(root)
        self.n_classes = 2  # 0: normal, 1: outlier
        self.normal_classes = tuple([normal_class])
        self.outlier_classes = list(range(0, 2))
        self.outlier_classes.remove(normal_class)
        target_transform = transforms.Lambda(lambda x: int(x in self.outlier_classes))
        self.train_set = CustumDataset(root_dir=root_[2],target_transform = target_transform,normal_class = normal_class,
                                   input_size=128,
                                   train=True,
                                   padding=False, inbalance=False, aug_falldown=False)
        print("self.normal_classes : ",self.normal_classes)
        train_idx_normal = get_target_label_idx(self.train_set.labels, self.normal_classes)
        #self.train_set = Subset(train_set, train_idx_normal) # 사람 데이터만 train_set으로 지정
        print("train_set.__getitem__(train_idx_normal[0])" , self.train_set.__getitem__(train_idx_normal[0]))
        self.test_set = CustumDataset(root_dir=root_[1],target_transform = target_transform,normal_class = normal_class,
                                 input_size=128,
                                 train=False,
                                 padding=False, inbalance=True, aug_falldown=False)

class CustumDataset(Dataset):
    def __init__(self, root_dir, input_size, target_transform = None,normal_class= 0,train = True, padding = True, inbalance = False, aug_falldown = False,
                 bright_ness = 0.2, hue = 0.15, contrast = 0.15, random_Hflip = 0.3, rotate_deg = 20,temp=False):
        min_max = [(-1.4646, 1.6818),(-3.8436, 3.3011)]
                   #(-34.924463588638204, 14.419298165027628)]
        self.train = train
        self.target_transform = target_transform
        self.orig_normal_paths, self.orig_back_paths = None,None
        if train :
           self.orig_normal_paths = glob.glob(root_dir + "/*.jpg")
        else :
           self.orig_back_paths = glob.glob(os.path.join(root_dir, "background") + "/*.jpg") # 0
           self.orig_normal_paths = glob.glob(os.path.join(root_dir, "normal") + "/*.jpg") # 1
        if self.orig_back_paths:
            self.back_paths = []
            self.back_paths = Check_32pixel_images(self.orig_back_paths)
        self.normal_paths = []
        self.normal_paths = Check_32pixel_images(self.orig_normal_paths)

        self.labels = []
        self.total_paths=[]
        if not train :
            self.labels = [-1] * len(self.back_paths) + [1] *len(self.normal_paths)
            for p in self.back_paths:self.total_paths.append(p)
        else :
            self.labels = [1] * len(self.normal_paths)
        for p in self.normal_paths:self.total_paths.append(p)
        np.ravel(self.total_paths)
        transform = []
        transform.append(torchvision.transforms.ToTensor())
        transform.append(transforms.Normalize([min_max[normal_class][0]] * 3,
                             [min_max[normal_class][1] - min_max[normal_class][0]] * 3))
        transform.append(torchvision.transforms.Resize((input_size, input_size)))
        self.transform = torchvision.transforms.Compose(transform)
        print(self.transform)

    def __len__(self):
        return len(self.total_paths)

    def __getitem__(self, index):
        img = Image.open(self.total_paths[index])
        img = self.transform(img)
        img, target = img, self.labels[index]
        return img, target, index  # only line changed


text:
# -*- coding: utf-8 -*-
#
# Copyright 2018 Amir Hadifar. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

import numpy as np
import scipy.io
import tensorflow as tf

from helper import utils

# VGG-19 parameters file
VGG_DOWNLOAD_LINK = 'http://www.vlfeat.org/matconvnet/models/imagenet-vgg-verydeep-19.mat'
VGG_FILENAME = 'imagenet-vgg-verydeep-19.mat'
EXPECTED_BYTES = 534904783

class VGG(object):
    def __init__(self, input_img):
        utils.download_vgg19(VGG_DOWNLOAD_LINK, VGG_FILENAME, EXPECTED_BYTES)
        self.vgg_layers = scipy.io.loadmat(VGG_FILENAME)['layers']
        self.input_img = input_img
        self.mean_pixels = np.array([123.68, 116.779, 103.939]).reshape((1, 1, 1, 3))

    def _weights(self, layer_idx, expected_layer_name):
        """ Return the weights and biases at layer_idx already trained by VGG
        """
        W = self.vgg_layers[0][layer_idx][0][0][2][0][0]
        b = self.vgg_layers[0][layer_idx][0][0][2][0][1]
        layer_name = self.vgg_layers[0][layer_idx][0][0][0][0]
        assert layer_name == expected_layer_name
        return W, b.reshape(b.size)

    def conv2d_relu(self, prev_layer, layer_idx, layer_name):
        """ Create a convolution layer with RELU using the weights and
        biases extracted from the VGG model at 'layer_idx'. You should use
        the function _weights() defined above to extract weights and biases.
        _weights() returns numpy arrays, so you have to convert them to TF tensors.
        Don't forget to apply relu to the output from the convolution.
        Inputs:
            prev_layer: the output tensor from the previous layer
            layer_idx: the index to current layer in vgg_layers
            layer_name: the string that is the name of the current layer.
                        It's used to specify variable_scope.
        Hint for choosing strides size:
            for small images, you probably don't want to skip any pixel
        """
        W, b = self._weights(layer_idx, layer_name)
        W, b = tf.convert_to_tensor(W), tf.convert_to_tensor(b)
        conv2d = tf.nn.conv2d(prev_layer, W, strides=[1, 1, 1, 1], padding="SAME")
        out = tf.nn.relu(tf.nn.bias_add(conv2d, b))
        setattr(self, layer_name, out)

    def avgpool(self, prev_layer, layer_name):
        """ Create the average pooling layer. The paper suggests that
        average pooling works better than max pooling.

        Input:
            prev_layer: the output tensor from the previous layer
            layer_name: the string that you want to name the layer.
                        It's used to specify variable_scope.
        Hint for choosing strides and kszie: choose what you feel appropriate
        """
        out = tf.nn.avg_pool(prev_layer, [1, 2, 2, 1], [1, 2, 2, 1], padding="SAME")
        setattr(self, layer_name, out)

    def load(self):
        self.conv2d_relu(self.input_img, 0, 'conv1_1')
        self.conv2d_relu(self.conv1_1, 2, 'conv1_2')
        self.avgpool(self.conv1_2, 'avgpool1')
        self.conv2d_relu(self.avgpool1, 5, 'conv2_1')
        self.conv2d_relu(self.conv2_1, 7, 'conv2_2')
        self.avgpool(self.conv2_2, 'avgpool2')
        self.conv2d_relu(self.avgpool2, 10, 'conv3_1')
        self.conv2d_relu(self.conv3_1, 12, 'conv3_2')
        self.conv2d_relu(self.conv3_2, 14, 'conv3_3')
        self.conv2d_relu(self.conv3_3, 16, 'conv3_4')
        self.avgpool(self.conv3_4, 'avgpool3')
        self.conv2d_relu(self.avgpool3, 19, 'conv4_1')
        self.conv2d_relu(self.conv4_1, 21, 'conv4_2')
        self.conv2d_relu(self.conv4_2, 23, 'conv4_3')
        self.conv2d_relu(self.conv4_3, 25, 'conv4_4')
        self.avgpool(self.conv4_4, 'avgpool4')
        self.conv2d_relu(self.avgpool4, 28, 'conv5_1')
        self.conv2d_relu(self.conv5_1, 30, 'conv5_2')
        self.conv2d_relu(self.conv5_2, 32, 'conv5_3')
        self.conv2d_relu(self.conv5_3, 34, 'conv5_4')
        self.avgpool(self.conv5_4, 'avgpool5')

max_stars_repo_name: hadifar/tensorflow-starter

max_issues_repo_name: Ehsan-Yaghoubi/tensorflow-starter

max_forks_repo_name: Ehsan-Yaghoubi/tensorflow-starter

reason: import numpy,import scipy


text:
#!/usr/bin/env python3
import time
import urllib.request
import sys

import cv2
import numpy as np

import object_count

def input_vid(vid_path):
    print("object counting...")
    object_count.process(vid_path)

if __name__ == "__main__":
    input_vid(vid_path=sys.argv[1])


text:
import numpy as np
from scipy.stats import mstats
from sklearn.datasets import load_boston
import RidgeRegression as RR
from utils import split_data

def get_k_sets(train_data, target_data, k, offset=0):
    n = train_data.shape[0]
    s = int(n/k)
    k_set_data = train_data[(k*offset):(k*offset + s)]
    k_set_target = target_data[(k*offset):(k*offset + s)]

    if offset > 0:
        rest_data = train_data.copy()
        rest_data_target = target_data.copy()
        rest_data = np.delete(rest_data, range((s*offset), (s*offset + s + 1)), axis = 0)
        rest_data_target = np.delete(rest_data_target, range((s*offset), (s*offset + s + 1)), axis=0)
    else:
        rest_data = train_data[(s*offset + s + 1):]
        rest_data_target = target_data[(s*offset + s + 1):]

    return k_set_data, k_set_target.reshape((k_set_target.shape[0], 1)), rest_data, rest_data_target.reshape(rest_data_target.shape[0], 1)

def main():
    data_set = load_boston()
    train_data, train_target, test_data, test_target = split_data(data_set)

    print "\n"
    print "K-Fold Ridge Regression, K = 10"
    print "{:^15}|{:^15}".format("Lambda", "CVE")
    print "-"*30

    k = 10
    err = 0
    ls = [0.001, 0.01, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    res = []

    for l in ls:
        err = 0
        for i in range(k):
            k_set, k_set_target, rest_set, rest_set_target = get_k_sets(train_data, train_target, k, offset=i)
            rr = RR.RidgeRegression()
            rr.fit(rest_set, rest_set_target, l)
            err += rr.mse(k_set, k_set_target)
        res.append([l, err/k])
        print "{:^15}|{:^15.7}".format(l, (err/k))

    min_l = min(res, key=lambda z: z[1])[0]

    print "\n"
    print "Select L = {} with min CVE".format(min_l)

    rr = RR.RidgeRegression()
    rr.fit(train_data, train_target, min_l)
    mse_test = rr.mse(test_data, test_target)
    mse_train = rr.mse(train_data, train_target)

    print "{:^15}|{:^15}|{:^15}".format("Input Data", "Lambda", "MSE")
    print "-"*45
    print "{:^15}|{:^15}|{:^15.7}".format("test_data", min_l, mse_test)
    print "{:^15}|{:^15}|{:^15.7}".format("train_data", min_l, mse_train)
    print "\n"

if __name__ == "__main__":
    main()

reason: import numpy,from scipy


text:
from typing import Tuple

import jax.numpy as jnp

State = Tuple[jnp.array, ...]

max_stars_repo_name: nicholasjng/ode-explorer

reason: import jax