text:
import os
import re
import numpy as np
import matplotlib.pylab as plt

from scipy.spatial     import ConvexHull
from itertools         import chain
from scipy.interpolate import interp1d
from collections       import defaultdict

from plot      import *
from plot_util import *

# ------------------------------------------------------------------------------
def plot_time_recall(chosen_top_k, methods, input_folder, output_folder):
    '''
    draw the querytime-recall curve for all methods on all datasets

    :params chosen_top_k: top_k value for drawing figure (integer)
    :params methods: a list of method (list)
    :params input_folder: input folder (string)
    :params output_folder: output folder (string)
    :returns: None
    '''
    fig_width, fig_height = calc_width_and_height(len(datasets), 1)
    plt_helper = PlotHelper(plt, fig_width, fig_height)
    plt_helper.plot_subplots_adjust()       # define a window for a figure

    method_labels = [method_labels_map[method] for method in methods]
    for di, (dataset, dataset_label) in enumerate(zip(datasets, dataset_labels)):
        # set up each sub-figure
        ax = plt.subplot(1, len(datasets), di+1)
        plt.title(dataset_label)            # title
        plt.xlim(0, 100)                    # limit (or range) of x-axis
        plt.xlabel('Recall (%)')            # label of x-axis
        if di == 0:                         # add label of y-axis at 1st dataset
            plt.ylabel('Query Time (ms)')

        miny = 1e9
        maxy = -1e9
        for method_idx, method, method_label, method_color, method_marker in \
            zip(count(), methods, method_labels, method_colors, method_markers):

            # get file name for this method on this dataset
            filename = get_filename(input_folder, dataset, method)
            if filename is None: continue
            print(filename)

            # get time-recall results
            time_recalls = []
            for _,res in parse_res(filename, chosen_top_k):
                time_recalls += [[gettime(res), getrecall(res)]]

            time_recalls = np.array(time_recalls)
            # print(time_recalls)

            # get the time-recall curve by convex hull and interpolation, where
            # lower_recalls -> x, lower_times -> y
            lower_recalls, lower_times = lower_bound_curve(time_recalls)
            miny = min(miny, np.min(lower_times))
            maxy = max(maxy, np.max(lower_times))
            ax.semilogy(lower_recalls, lower_times, '-', color=method_color,
                marker=method_marker, label=method_label if di==0 else "",
                markevery=10, markerfacecolor='none', markersize=7,
                zorder=len(methods)-method_idx)

        # set up the limit (or range) of y-axis
        plt_helper.set_y_axis_log10(ax, miny, maxy)

    # plot legend and save figure
    plt_helper.plot_fig_legend(ncol=len(methods))
    plt_helper.plot_and_save(output_folder, 'time_recall')
    plt.show()

# ------------------------------------------------------------------------------
def plot_fraction_recall(chosen_top_k, methods, input_folder, output_folder):
    '''
    draw the fraction-recall curve for all methods on all datasets

    :params chosen_top_k: top_k value for drawing figure (integer)
    :params methods: a list of method (list)
    :params input_folder: input folder (string)
    :params output_folder: output folder (string)
    :returns: None
    '''
    fig_width, fig_height = calc_width_and_height(len(datasets), 1)
    plt_helper = PlotHelper(plt, fig_width, fig_height)
    plt_helper.plot_subplots_adjust()       # define a window for a figure

    method_labels = [method_labels_map[method] for method in methods]
    for di, (dataset, dataset_label) in enumerate(zip(datasets, dataset_labels)):
        # set up each sub-figure
        ax = plt.subplot(1, len(datasets), di+1)
        plt.title(dataset_label)            # title
        plt.xlim(0, 100)                    # limit (or range) of x-axis
        plt.xlabel('Recall (%)')            # label of x-axis
        if di == 0:                         # add label of y-axis at 1st dataset
            plt.ylabel('Fraction (%)')

        miny = 1e9
        maxy = -1e9
        for method_idx, method, method_label, method_color, method_marker in \
            zip(count(), methods, method_labels, method_colors, method_markers):

            # get file name for this method on this dataset
            filename = get_filename(input_folder, dataset, method)
            if filename is None: continue
            print(filename)

            # get fraction-recall results
            fraction_recalls = []
            for _,res in parse_res(filename, chosen_top_k):
                fraction_recalls += [[getfraction(res), getrecall(res)]]

            fraction_recalls = np.array(fraction_recalls)
            # print(fraction_recalls)

            # get the fraction-recall curve by convex hull and interpolation, where
            # lower_recalls -> x, lower_times -> y
            # print('fraction_recall!!!!\n', fraction_recalls)
            lower_recalls, lower_fractions = lower_bound_curve(fraction_recalls)
            miny = min(miny, np.min(lower_fractions))
            maxy = max(maxy, np.max(lower_fractions))
            ax.semilogy(lower_recalls, lower_fractions, '-', color=method_color,
                marker=method_marker, label=method_label if di==0 else "",
                markevery=10, markerfacecolor='none', markersize=7,
                zorder=len(methods)-method_idx)

        # set up the limit (or range) of y-axis
        plt_helper.set_y_axis_log10(ax, miny, maxy)

    # plot legend and save figure
    plt_helper.plot_fig_legend(ncol=len(methods))
    plt_helper.plot_and_save(output_folder, 'fraction_recall')
    plt.show()

# ------------------------------------------------------------------------------
def plot_precision_recall(chosen_top_k, methods, input_folder, output_folder):
    '''
    draw the precision-recall curve for all methods on all datasets

    :params chosen_top_k: top_k value for drawing figure (integer)
    :params methods: a list of method (list)
    :returns: None
    '''
    fig_width, fig_height = calc_width_and_height(len(datasets), 1)
    plt_helper = PlotHelper(plt, fig_width, fig_height)
    plt_helper.plot_subplots_adjust()       # define a window for a figure

    method_labels = [method_labels_map[method] for method in methods]
    for di, (dataset, dataset_label) in enumerate(zip(datasets, dataset_labels)):
        # set up each sub-figure
        ax = plt.subplot(1, len(datasets), di+1)
        plt.title(dataset_label)            # title
        plt.xlim(0, 100)                    # limit (or range) of x-axis
        plt.xlabel('Recall (%)')            # label of x-axis
        if di == 0:                         # add label of y-axis for the 1st dataset
            plt.ylabel('Precision (%)')

        miny = 1e9
        maxy = -1e9
        for method_idx, method, method_label, method_color, method_marker in \
            zip(count(), methods, method_labels, method_colors, method_markers):

            # get file name for this method on this dataset
            filename = get_filename(input_folder, dataset, method)
            if filename is None: continue
            print(filename)

            # get precision-recall results
            precision_recalls = []
            for _,res in parse_res(filename, chosen_top_k):
                precision = getprecision(res)
                recall    = getrecall(res)
                if (recall > 0 and precision > 0):
                    precision_recalls += [[precision, recall]]

            precision_recalls = np.array(precision_recalls)
            # print(precision_recalls)

            # get the time-recall curve by convex hull and interpolation, where
            upper_recalls, upper_precisions = upper_bound_curve(precision_recalls, 1.0, True)
            if len(upper_recalls) > 0:
                miny = min(miny, np.min(upper_precisions))
                maxy = max(maxy, np.max(upper_precisions))
                ax.semilogy(upper_recalls, upper_precisions, '-',
                    color=method_color, marker=method_marker,
                    label=method_label if di==0 else "", markevery=10,
                    markerfacecolor='none', markersize=7,
                    zorder=len(methods)-method_idx)

        # set up the limit (or range) of y-axis
        plt_helper.set_y_axis_log10(ax, miny, maxy)

    # plot legend and save figure
    plt_helper.plot_fig_legend(ncol=len(methods))
    plt_helper.plot_and_save(output_folder, 'precision_recall')
    plt.show()

# ------------------------------------------------------------------------------
def plot_time_recall_ratio(chosen_top_k, methods, input_folder, output_folder):
    '''
    draw the querytime-recall curves and querytime-ratio curves for all methods
    on all datasets

    :params chosen_top_k: top_k value for drawing figure (integer)
    :params methods: a list of method (list)
    :params input_folder: input folder (string)
    :params output_folder: output folder (string)
    :returns: None
    '''
    n_datasets = len(datasets)
    fig_width, fig_height = calc_width_and_height(n_datasets, 2)
    plt_helper = PlotHelper(plt, fig_width, fig_height)
    plt_helper.plot_subplots_adjust()       # define a window for a figure

    method_labels = [method_labels_map[method] for method in methods]
    for di, (dataset, dataset_label) in enumerate(zip(datasets, dataset_labels)):
        # set up two sub-figures
        ax_recall = plt.subplot(2, n_datasets, di+1)
        plt.title(dataset_label)            # title
        plt.xlabel('Recall (%)')            # label of x-axis
        plt.xlim(0, 100)

        ax_ratio = plt.subplot(2, n_datasets, n_datasets+di+1)
        plt.xlabel('Ratio')
        plt.xlim(1.0, 11.0)
        plt.xticks([1.0, 3.0, 5.0, 7.0, 9.0, 11.0])

        if di == 0:
            ax_recall.set_ylabel('Query Time (ms)')
            ax_ratio.set_ylabel('Query Time (ms)')

        miny = 1e9
        maxy = -1e9
        for method_idx, method, method_label, method_color, method_marker in \
            zip(count(), methods, method_labels, method_colors, method_markers):

            # get file name for this method on this dataset
            filename = get_filename(input_folder, dataset, method)
            if filename is None: continue
            print(filename)

            # get querytime-recall and querytime-ratio results from disk
            time_recalls = []
            time_ratios = []
            for _,res in parse_res(filename, chosen_top_k):
                time_recalls += [[gettime(res), getrecall(res)]]
                time_ratios  += [[gettime(res), getratio(res)]]

            time_recalls = np.array(time_recalls)
            time_ratios  = np.array(time_ratios)
            # print(time_recalls, time_ratios)

            # get the querytime-recall curve by convex hull and interpolation
            lower_recalls, lower_times = lower_bound_curve(time_recalls)
            ax_recall.semilogy(lower_recalls, lower_times, '-',
                color=method_color, marker=method_marker,
                label=method_label if di==0 else "", markevery=10,
                markerfacecolor='none', markersize=10)

            miny = min(miny, np.min(lower_times))
            maxy = max(maxy, np.max(lower_times))

            # get the querytime-ratio curve by convex hull
            upper_ratios, upper_times = upper_bound_curve(time_ratios, 0.2, False)
            ax_ratio.semilogy(upper_ratios, upper_times, '-',
                color=method_color, marker=method_marker, label="",
                markevery=5, markerfacecolor='none', markersize=10,
                zorder=len(methods)-method_idx)

            miny = min(miny, np.min(upper_times))
            maxy = max(maxy, np.max(upper_times))

        # set up the limit (or range) of y-axis
        plt_helper.set_y_axis_log10(ax_recall, miny, maxy)
        plt_helper.set_y_axis_log10(ax_ratio,  miny, maxy)

    # plot legend and save figure
    plt_helper.plot_fig_legend(ncol=len(methods))
    plt_helper.plot_and_save(output_folder, 'time_recall_ratio')

# ------------------------------------------------------------------------------
def plot_time_index(chosen_top_k, recall_level, methods, input_folder, output_folder):
    '''
    draw the querytime-indexsize curves and querytime-indexingtime curves for
    all methods on all datasets

    :params chosen_top_k: top_k value for drawing figure (integer)
    :params recall_level: recall value for drawing figure (integer)
    :params methods: a list of method (list)
    :params input_folder: input folder (string)
    :params output_folder: output folder (string)
    :returns: None
    '''
    n_datasets = len(datasets)
    fig_width, fig_height = calc_width_and_height(n_datasets, 2)
    plt_helper = PlotHelper(plt, fig_width, fig_height)
    plt_helper.plot_subplots_adjust()       # define a window for a figure

    method_labels = [method_labels_map[method] for method in methods]
    for di, (dataset, dataset_label) in enumerate(zip(datasets, dataset_labels)):
        # set up two sub-figures
        ax_size = plt.subplot(2, n_datasets, di+1)
        plt.title(dataset_label)            # title
        plt.xlabel('Index Size (MB)')       # label of x-axis

        ax_time = plt.subplot(2, n_datasets, n_datasets+di+1)
        plt.xlabel('Indexing Time (Seconds)')   # label of x-axis

        if di == 0:
            ax_size.set_ylabel('Query Time (ms)')
            ax_time.set_ylabel('Query Time (ms)')

        min_size_y = 1e9; max_size_y = -1e9
        min_time_y = 1e9; max_time_y = -1e9
        for method_idx, method, method_label, method_color, method_marker in \
            zip(count(), methods, method_labels, method_colors, method_markers):

            # get file name for this method on this dataset
            filename = get_filename(input_folder, dataset, method)
            if filename is None: continue
            print(filename)

            # get all results from disk
            chosen_ks_dict = defaultdict(list)
            for _,res in parse_res(filename, chosen_top_k):
                query_time = gettime(res)
                recall     = getrecall(res)
                index_time = getindexingtime(res)
                index_size = getindexsize(res)

                chosen_ks_dict[(index_time, index_size)] += [[recall, query_time]]

            # get querytime-indexsize and querytime-indexingtime results if its
            # recall is higher than recall_level
            index_times, index_sizes, querytimes_at_recall = [], [], []
            for (index_time, index_size), recall_querytimes_ in chosen_ks_dict.items():
                # add [[0, 0]] for interpolation
                recall_querytimes_ = np.array([[0, 0]] + recall_querytimes_)

                recalls, query_times = lower_bound_curve2(recall_querytimes_)
                if np.max(recalls) > recall_level:
                    # get the estimated time at recall level by interpolation
                    f = interp1d(recalls, query_times)
                    querytime_at_recall = f(recall_level)

                    # update results
                    index_times += [index_time]
                    index_sizes += [index_size]
                    querytimes_at_recall += [querytime_at_recall]
                    print('interp, ', querytime_at_recall, index_size, index_time)

            index_times = np.array(index_times)
            index_sizes = np.array(index_sizes)
            querytimes_at_recall = np.array(querytimes_at_recall)

            # get the querytime-indexsize curve by convex hull
            isize_qtime = np.zeros(shape=(len(index_sizes), 2))
            isize_qtime[:, 0] = index_sizes
            isize_qtime[:, 1] = querytimes_at_recall

            lower_isizes, lower_qtimes = lower_bound_curve2(isize_qtime)
            if len(lower_isizes) > 0:
                # print(method, lower_isizes, lower_qtimes)
                min_size_y = min(min_size_y, np.min(lower_qtimes))
                max_size_y = max(max_size_y, np.max(lower_qtimes))
                ax_size.semilogy(lower_isizes, lower_qtimes, '-', color=method_color,
                    marker=method_marker, label=method_label if di==0 else "",
                    markerfacecolor='none', markersize=10)

                # get the querytime-indextime curve by convex hull
                itime_qtime = np.zeros(shape=(len(index_times), 2))
                itime_qtime[:, 0] = index_times
                itime_qtime[:, 1] = querytimes_at_recall

                lower_itimes, lower_qtimes = lower_bound_curve2(itime_qtime)
                # print(method, lower_itimes, lower_qtimes)
                min_time_y = min(min_time_y, np.min(lower_qtimes))
                max_time_y = max(max_time_y, np.max(lower_qtimes))
                ax_time.semilogy(lower_itimes, lower_qtimes, '-', color=method_color,
                    marker=method_marker, label="", markerfacecolor='none',
                    markersize=10, zorder=len(methods)-method_idx)

        # set up the limit (or range) of y-axis
        plt_helper.set_y_axis_log10(ax_size, min_size_y, max_size_y)
        plt_helper.set_y_axis_log10(ax_time, min_time_y, max_time_y)

    # plot legend and save figure
    plt_helper.plot_fig_legend(ncol=len(methods))
    plt_helper.plot_and_save(output_folder, 'time_index')

# ------------------------------------------------------------------------------
def plot_time_indextime(chosen_top_k, recall_level, methods, input_folder,
    output_folder):
    '''
    draw the querytime-indexsize curves and querytime-indexingtime curves for
    all methods on all datasets

    :params chosen_top_k: top_k value for drawing figure (integer)
    :params recall_level: recall value for drawing figure (integer)
    :params methods: a list of method (list)
    :params input_folder: input folder (string)
    :params output_folder: output folder (string)
    :returns: None
    '''
    n_datasets = len(datasets)
    fig_width, fig_height = calc_width_and_height(n_datasets, 1)
    plt_helper = PlotHelper(plt, fig_width, fig_height)
    plt_helper.plot_subplots_adjust()       # define a window for a figure

    method_labels = [method_labels_map[method] for method in methods]
    for di, (dataset, dataset_label) in enumerate(zip(datasets, dataset_labels)):
        # set up sub-figure
        ax_time = plt.subplot(1, n_datasets, di+1)
        plt.title(dataset_label)            # title
        plt.xlabel('Indexing Time (Seconds)') # label of x-axis

        if di == 0:
            ax_time.set_ylabel('Query Time (ms)')

        miny = 1e9; maxy = -1e9
        minx = 1e9; maxx = -1e9
        for method_idx, method, method_label, method_color, method_marker in \
            zip(count(), methods, method_labels, method_colors, method_markers):

            # get file name for this method on this dataset
            filename = get_filename(input_folder, dataset, method)
            if filename is None: continue
            print(filename)

            # get all results from disk
            chosen_ks_dict = defaultdict(list)
            for _,res in parse_res(filename, chosen_top_k):
                query_time = gettime(res)
                recall     = getrecall(res)
                index_time = getindexingtime(res)

                chosen_ks_dict[index_time] += [[recall, query_time]]

            # get querytime-indexsize and querytime-indexingtime results if its
            # recall is higher than recall_level
            index_times, querytimes_at_recall = [], []
            for index_time, recall_querytimes_ in chosen_ks_dict.items():
                # add [[0, 0]] for interpolation
                recall_querytimes_ = np.array([[0, 0]] +recall_querytimes_)
                recalls, query_times = lower_bound_curve2(recall_querytimes_)

                if np.max(recalls) > recall_level:
                    # get the estimated time at recall level by interpolation
                    f = interp1d(recalls, query_times)
                    querytime_at_recall = f(recall_level)

                    # update results
                    index_times += [index_time]
                    querytimes_at_recall += [querytime_at_recall]
                    # print('interp, ', querytime_at_recall, index_time)

            index_times = np.array(index_times)
            querytimes_at_recall = np.array(querytimes_at_recall)

            # get the querytime-indextime curve by convex hull
            itime_qtimes = np.zeros(shape=(len(index_times), 2))
            itime_qtimes[:, 0] = index_times
            itime_qtimes[:, 1] = querytimes_at_recall

            lower_itimes, lower_qtimes = lower_bound_curve2(itime_qtimes)
            if len(lower_itimes) > 0:
                # print(method, lower_itimes, lower_qtimes)
                minx = min(minx, np.min(lower_itimes))
                maxx = max(maxx, np.max(lower_itimes))
                miny = min(miny, np.min(lower_qtimes))
                maxy = max(maxy, np.max(lower_qtimes))

                ax_time.semilogy(lower_itimes, lower_qtimes, '-', color=method_color,
                    marker=method_marker, label=method_label if di==0 else "",
                    markerfacecolor='none', markersize=10, zorder=len(methods)-method_idx)

        # set up the limit (or range) of x-axis and y-axis
        if dataset == "Msong":
            plt_helper.set_x_axis(ax_time, minx, 0.02*maxx)
        else:
            plt_helper.set_x_axis(ax_time, minx, 0.22*maxx)
        plt_helper.set_y_axis_log10(ax_time, miny, maxy)

    # plot legend and save figure
    plt_helper.plot_fig_legend(ncol=len(methods))
    plt_helper.plot_and_save(output_folder, 'time_indextime')

# ------------------------------------------------------------------------------
def plot_time_k(chosen_top_ks, recall_level, methods, input_folder,
    output_folder):
    '''
    draw the querytime-indexsize curves and querytime-indexingtime curves for
    all methods on all datasets

    :params chosen_top_ks: top_k value for drawing figure (list)
    :params recall_level: recall value for drawing figure (integer)
    :params methods: a list of method (list)
    :params input_folder: input folder (string)
    :params output_folder: output folder (string)
    :returns: None
    '''
    n_datasets = len(datasets)
    fig_width, fig_height = calc_width_and_height(n_datasets, 1)
    plt_helper = PlotHelper(plt, fig_width, fig_height)
    plt_helper.plot_subplots_adjust()       # define a window for a figure

    method_labels = [method_labels_map[method] for method in methods]
    for di, (dataset, dataset_label) in enumerate(zip(datasets, dataset_labels)):
        # set up sub-figure
        ax_k = plt.subplot(1, n_datasets, di+1)
        plt.title(dataset_label)            # title
        plt.xlabel('$k$')                   # label of x-axis

        if di == 0:
            ax_k.set_ylabel('Query Time (ms)')

        miny = 1e9; maxy = -1e9
        for method_idx, method, method_label, method_color, method_marker in \
            zip(count(), methods, method_labels, method_colors, method_markers):

            # get file name for this method on this dataset
            filename = get_filename(input_folder, dataset, method)
            if filename is None: continue
            print(filename)

            # get all results from disk
            chosen_ks_dict = defaultdict(list)
            for chosen_top_k in chosen_top_ks:
                for _,res in parse_res(filename, chosen_top_k):
                    query_time = gettime(res)
                    recall     = getrecall(res)
                    chosen_ks_dict[chosen_top_k] += [[recall, query_time]]

            # get querytime-indexsize and querytime-indexingtime results if its
            # recall is higher than recall_level
            chosen_ks, querytimes_at_recall = [], []
            for chosen_k, recall_querytimes_ in chosen_ks_dict.items():
                # add [[0, 0]] for interpolation
                recall_querytimes_ = np.array([[0, 0]] + recall_querytimes_)

                recalls, query_times = lower_bound_curve2(recall_querytimes_)
                if np.max(recalls) > recall_level:
                    # get the estimated time at recall level by interpolation
                    f = interp1d(recalls, query_times)
                    querytime_at_recall = f(recall_level)

                    # update results
                    chosen_ks += [chosen_k]
                    querytimes_at_recall += [querytime_at_recall]

            chosen_ks = np.array(chosen_ks)
            querytimes_at_recall = np.array(querytimes_at_recall)

            miny = min(miny, np.min(querytimes_at_recall))
            maxy = max(maxy, np.max(querytimes_at_recall))
            ax_k.semilogy(chosen_ks, querytimes_at_recall, '-',
                    color=method_color, marker=method_marker,
                    label=method_label if di==0 else "",
                    markerfacecolor='none', markersize=10,
                    zorder=len(methods)-method_idx)

        # set up the limit (or range) of y-axis
        plt_helper.set_y_axis_log10(ax_k, miny, maxy)

    # plot legend and save figure
    plt_helper.plot_fig_legend(ncol=len(methods))
    plt_helper.plot_and_save(output_folder, 'time_k')

# ------------------------------------------------------------------------------
def plot_nh_t(chosen_top_k, datasets, input_folder, output_folder, \
    fig_width=6.5, fig_height=6.0):

    plt_helper = PlotHelper(plt, fig_width, fig_height)
    plt_helper.plot_subplots_adjust(top_space=1.2, hspace=0.37)

    method = 'NH'
    for di, dataset in enumerate(datasets):
        ax = plt.subplot(1, len(datasets), di+1)
        ax.set_xlabel(r'Recall (%)')
        if di == 0:
            ax.set_ylabel(r'Query Time (ms)')
        ax.set_title('%s' % dataset_labels_map[dataset])

        # get file name for this method on this dataset
        filename = get_filename(input_folder, dataset, method)
        print(filename, method, dataset)

        fix_s=2
        data = []
        for record in parse_res(filename, chosen_top_k):
            # print(record)
            m      = get_m(record)
            s      = get_s(record)
            cand   = get_cand(record)
            time   = get_time(record)
            recall = get_recall(record)

            if s == fix_s:
                print(m, s, cand, time, recall)
                data += [[m, s, cand, time, recall]]
        data = np.array(data)

        ms = [8, 16, 32, 64, 128, 256]
        maxy = -1e9
        miny = 1e9
        for color, marker, m in zip(method_colors, method_markers, ms):
            data_mp = data[data[:, 0]==m]
            # print(m, data_mp)

            plt.semilogy(data_mp[:, -1], data_mp[:, -2], marker=marker,
                label='$t=%d$'%(m) if di==0 else "", c=color,
                markerfacecolor='none', markersize=7)
            miny = min(miny, np.min(data_mp[:,-2]) )
            maxy = max(maxy, np.max(data_mp[:,-2]) )
        plt.xlim(0, 100)
        # print(dataset, distance, miny, maxy)
        plt_helper.set_y_axis_log10(ax, miny, maxy)

    plt_helper.plot_fig_legend(ncol=3)
    plt_helper.plot_and_save(output_folder, 'varying_nh_t')

# ------------------------------------------------------------------------------
def plot_fh_m(chosen_top_k, datasets, input_folder, output_folder, \
    fig_width=6.5, fig_height=6.0):

    plt_helper = PlotHelper(plt, fig_width, fig_height)
    plt_helper.plot_subplots_adjust(top_space=1.2, hspace=0.37)

    method = 'FH'
    for di, dataset in enumerate(datasets):
        ax = plt.subplot(1, len(datasets), di+1)
        ax.set_xlabel(r'Recall (%)')
        if di == 0:
            ax.set_ylabel(r'Query Time (ms)')
        ax.set_title('%s' % dataset_labels_map[dataset])

        # get file name for this method on this dataset
        filename = get_filename(input_folder, dataset, method)
        print(filename, method, dataset)

        fix_l=4
        fix_s=2
        data = []
        for record in parse_res(filename, chosen_top_k):
            # print(record)
            m      = get_m(record)
            l      = get_l(record)
            s      = get_s(record)
            cand   = get_cand(record)
            time   = get_time(record)
            recall = get_recall(record)

            if l == fix_l and s == fix_s:
                print(m, l, s, cand, time, recall)
                data += [[m, l, s, cand, time, recall]]
        data = np.array(data)

        ms = [8, 16, 32, 64, 128, 256]
        maxy = -1e9
        miny = 1e9
        for color, marker, m in zip(method_colors, method_markers, ms):
            data_mp = data[data[:, 0]==m]
            # print(m, data_mp)

            plt.semilogy(data_mp[:, -1], data_mp[:, -2], marker=marker,
                label='$m=%d$'%(m) if di==0 else "", c=color,
                markerfacecolor='none', markersize=7)
            miny = min(miny, np.min(data_mp[:,-2]) )
            maxy = max(maxy, np.max(data_mp[:,-2]) )
        plt.xlim(0, 100)
        # print(dataset, distance, miny, maxy)
        plt_helper.set_y_axis_log10(ax, miny, maxy)

    plt_helper.plot_fig_legend(ncol=3)
    plt_helper.plot_and_save(output_folder, 'varying_fh_m')

# ------------------------------------------------------------------------------
def plot_fh_l(chosen_top_k, datasets, input_folder, output_folder, \
    fig_width=6.5, fig_height=6.0):

    plt_helper = PlotHelper(plt, fig_width, fig_height)
    plt_helper.plot_subplots_adjust(top_space=0.8, hspace=0.37)

    method = 'FH'
    for di, dataset in enumerate(datasets):
        ax = plt.subplot(1, len(datasets), di+1)
        ax.set_xlabel(r'Recall (%)')
        if di == 0:
            ax.set_ylabel(r'Query Time (ms)')
        ax.set_title('%s' % dataset_labels_map[dataset])

        # get file name for this method on this dataset
        filename = get_filename(input_folder, dataset, method)
        print(filename, method, dataset)

        fix_m=16
        fix_s=2
        data = []
        for record in parse_res(filename, chosen_top_k):
            # print(record)
            m      = get_m(record)
            l      = get_l(record)
            s      = get_s(record)
            cand   = get_cand(record)
            time   = get_time(record)
            recall = get_recall(record)

            if m == fix_m and s == fix_s:
                print(m, l, s, cand, time, recall)
                data += [[m, l, s, cand, time, recall]]
        data = np.array(data)

        ls = [2, 4, 6, 8, 10]
        maxy = -1e9
        miny = 1e9
        for color, marker, l in zip(method_colors, method_markers, ls):
            data_mp = data[data[:, 1]==l]
            # print(m, data_mp)

            plt.semilogy(data_mp[:, -1], data_mp[:, -2], marker=marker,
                label='$l=%d$'%(l) if di==0 else "", c=color,
                markerfacecolor='none', markersize=7)
            miny = min(miny, np.min(data_mp[:,-2]) )
            maxy = max(maxy, np.max(data_mp[:,-2]) )
        plt.xlim(0, 100)
        # print(dataset, distance, miny, maxy)
        plt_helper.set_y_axis_log10(ax, miny, maxy)

    plt_helper.plot_fig_legend(ncol=5)
    plt_helper.plot_and_save(output_folder, 'varying_fh_l')

# ------------------------------------------------------------------------------
def plot_fh_s(chosen_top_k, datasets, input_folder, output_folder, \
    fig_width=6.5, fig_height=6.0):

    plt_helper = PlotHelper(plt, fig_width, fig_height)
    plt_helper.plot_subplots_adjust(top_space=0.8, hspace=0.37)

    method = 'FH'
    for di, dataset in enumerate(datasets):
        ax = plt.subplot(1, len(datasets), di+1)
        ax.set_xlabel(r'Recall (%)')
        if di == 0:
            ax.set_ylabel(r'Query Time (ms)')
        ax.set_title('%s' % dataset_labels_map[dataset])

        # get file name for this method on this dataset
        filename = get_filename(input_folder, dataset, method)
        print(filename, method, dataset)

        fix_m=16
        fix_l=4
        data = []
        for record in parse_res(filename, chosen_top_k):
            # print(record)
            m      = get_m(record)
            l      = get_l(record)
            s      = get_s(record)
            cand   = get_cand(record)
            time   = get_time(record)
            recall = get_recall(record)

            if m == fix_m and l == fix_l:
                print(m, l, s, cand, time, recall)
                data += [[m, l, s, cand, time, recall]]
        data = np.array(data)

        ss = [1, 2, 4, 8]
        maxy = -1e9
        miny = 1e9
        for color, marker, s in zip(method_colors, method_markers, ss):
            data_mp = data[data[:, 2]==s]
            # print(m, data_mp)

            plt.semilogy(data_mp[:, -1], data_mp[:, -2], marker=marker,
                label='$\lambda=%d d$'%(s) if di==0 else "", c=color,
                markerfacecolor='none', markersize=7)
            miny = min(miny, np.min(data_mp[:,-2]) )
            maxy = max(maxy, np.max(data_mp[:,-2]) )
        plt.xlim(0, 100)
        # print(dataset, distance, miny, maxy)
        plt_helper.set_y_axis_log10(ax, miny, maxy)

    plt_helper.plot_fig_legend(ncol=4)
    plt_helper.plot_and_save(output_folder, 'varying_fh_s')

# ------------------------------------------------------------------------------
def plot_nh_s(chosen_top_k, datasets, input_folder, output_folder, \
    fig_width=6.5, fig_height=6.0):

    plt_helper = PlotHelper(plt, fig_width, fig_height)
    plt_helper.plot_subplots_adjust(top_space=0.8, hspace=0.37)

    method = 'NH'

    for di, dataset in enumerate(datasets):
        ax = plt.subplot(1, len(datasets), di+1)
        ax.set_xlabel(r'Recall (%)')
        if di == 0:
            ax.set_ylabel(r'Query Time (ms)')
        ax.set_title('%s' % dataset_labels_map[dataset])

        # get file name for this method on this dataset
        filename = get_filename(input_folder, dataset, method)
        print(filename, method, dataset)

        fix_m=256
        data = []
        for record in parse_res(filename, chosen_top_k):
            # print(record)
            m      = get_m(record)
            s      = get_s(record)
            cand   = get_cand(record)
            time   = get_time(record)
            recall = get_recall(record)

            if m == fix_m:
                print(m, s, cand, time, recall)
                data += [[m, s, cand, time, recall]]
        data = np.array(data)

        ss = [1, 2, 4, 8]
        maxy = -1e9
        miny = 1e9
        for color, marker, s in zip(method_colors, method_markers, ss):
            data_mp = data[data[:, 1]==s]
            # print(m, data_mp)

            plt.semilogy(data_mp[:, -1], data_mp[:, -2], marker=marker,
                label='$\lambda=%d d$'%(s) if di==0 else "", c=color,
                markerfacecolor='none', markersize=7)
            miny = min(miny, np.min(data_mp[:,-2]) )
            maxy = max(maxy, np.max(data_mp[:,-2]) )
        plt.xlim(0, 100)
        # print(dataset, distance, miny, maxy)
        plt_helper.set_y_axis_log10(ax, miny, maxy)

    plt_helper.plot_fig_legend(ncol=4)
    plt_helper.plot_and_save(output_folder, 'varying_nh_s')

# ------------------------------------------------------------------------------
if __name__ == '__main__':

    chosen_top_k  = 10
    input_folder  = "../results/"
    output_folder = "../figures/param/"
    datasets = ['Yelp', 'GloVe100']

    plot_nh_t(chosen_top_k, datasets, input_folder, output_folder, fig_height=3.4)
    plot_nh_s(chosen_top_k, datasets, input_folder, output_folder, fig_height=3.0)
    plot_fh_m(chosen_top_k, datasets, input_folder, output_folder, fig_height=3.4)
    plot_fh_l(chosen_top_k, datasets, input_folder, output_folder, fig_height=3.0)
    plot_fh_s(chosen_top_k, datasets, input_folder, output_folder, fig_height=3.0)

reason: import numpy,from scipy


text:
import sys
import threading
from statistics import mean

import numpy as np
import pytest
from numpy.random import RandomState
from numpy.testing import assert_allclose

from rlai.utils import (
    IncrementalSampleAverager,
    sample_list_item,
    import_function,
    StdStreamTee,
    RunThreadManager,
    get_nearest_positive_definite_matrix,
    is_positive_definite
)

def test_incremental_averager():

    rng = RandomState(1234)

    sample = [
        float(value)
        for value in rng.randint(0, 1000, 100)
    ]

    averager = IncrementalSampleAverager()

    for value in sample:
        averager.update(value)

    assert averager.get_value() == mean(sample)
    assert str(averager) == str(mean(sample))
    assert not (averager != averager)

    with pytest.raises(ValueError, match='Cannot pass a weight to an unweighted averager.'):
        averager.update(1.0, 1.0)

    weighted_averager = IncrementalSampleAverager(weighted=True)
    with pytest.raises(ValueError, match='The averager is weighted'):
        weighted_averager.update(1.0)

    with pytest.raises(ValueError, match='alpha must be > 0'):
        IncrementalSampleAverager(alpha=-1)

    with pytest.raises(ValueError, match='Cannot supply alpha and per-value weights.'):
        IncrementalSampleAverager(alpha=0.1, weighted=True)

def test_sample_list_item():

    x = [1, 2, 3]
    p = np.array([0.1, 0.3, 0.6])

    rng = RandomState(12345)
    x_samples = [
        sample_list_item(x, p, rng)
        for _ in range(10000)
    ]

    xs, cnts = np.unique(x_samples, return_counts=True)

    x_cnt = {
        x: cnt
        for x, cnt in zip(xs, cnts)
    }

    total = sum(x_cnt.values())
    x_p = [
        x_cnt[x] / total
        for x in x
    ]

    assert_allclose(p, x_p, atol=0.01)

    with pytest.raises(ValueError, match='Expected cumulative probabilities to sum to 1'):
        sample_list_item([1, 2, 3], np.array([0.2, 0.3, 0.4]), rng)

def test_import_function():

    assert import_function(None) is None

def test_stdstream_tee():

    tee = StdStreamTee(sys.stdout, 10, True)
    sys.stdout = tee

    for i in range(20):
        print(f'{i}')

    sys.stdout.flush()

    assert len(tee.buffer) == 10

    sys.stdout = sys.__stdout__

def test_run_thread_manager_initially_blocked():

    run_manager = RunThreadManager(False)

    wait_return = None

    def thread_target():
        nonlocal wait_return
        wait_return = run_manager.wait(2)

    t = threading.Thread(target=thread_target)
    t.start()
    t.join()

    assert not wait_return

def test_neareset_pd():

    diag = np.array([[0, 0], [0, 0]])
    np.fill_diagonal(diag, 1)
    assert np.array_equal(diag, get_nearest_positive_definite_matrix(diag))

    for i in range(10):
        for j in range(2, 100):
            A = np.random.randn(j, j)
            B = get_nearest_positive_definite_matrix(A)
            assert is_positive_definite(B)

ext: py

max_stars_repo_name: MatthewGerber/rl

max_issues_repo_name: MatthewGerber/rl

max_forks_repo_name: MatthewGerber/rl

reason: import numpy,from numpy


text:
'''
vec_adjust_test.py

Copyright (c) 2017 Satoshi Otani

This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
'''
#! -*- coding:utf-8 -*-
import math
import numpy as np
import tensorflow as tf

class AdjustVector:
    def __init__(self, embedding_size=300, epoch_count=20000, learning_rate=0.5, batch_size=10):
        self._embedding_size = embedding_size
        self._epoch_count = epoch_count
        self._learning_rate = learning_rate
        self._batch_size = batch_size

    def get_adjust_vector(self, target_vector, other_vector_and_cos):
        target_vector = np.array(target_vector)
        other_vectors = np.array([v for v, c in other_vector_and_cos])
        other_vectors_cos = np.array([[c] for v, c in other_vector_and_cos])

        # other_vector = {vector: cos, ...}
        other_vec_size = len(other_vector_and_cos)
        # 正規化
        target_vector = target_vector / np.linalg.norm(target_vector)
        other_vectors = other_vectors / np.linalg.norm(other_vectors, axis=1).reshape(other_vec_size, 1)

        # tensorflow用の入力を準備
        x1 = np.array([target_vector for i in range(len(other_vectors))])
        x2 = other_vectors
        t = other_vectors_cos

        input_1 = tf.placeholder(tf.float32, shape=[None, self._embedding_size])
        input_2 = tf.placeholder(tf.float32, shape=[None, self._embedding_size])
        input_t = tf.placeholder(tf.float32, shape=[None, 1])

        w = tf.Variable(tf.ones([self._embedding_size]))
        b = tf.Variable(tf.zeros([1]))
        y = tf.matmul((input_2), tf.transpose(input_1*w+b))

        init = tf.global_variables_initializer()
        sess = tf.Session()
        sess.run(init)

        loss = tf.reduce_mean(tf.square((y-input_t)))
        train_step = tf.train.GradientDescentOptimizer(self._learning_rate).minimize(loss)
        for epoch in range(self._epoch_count):
            rand_index = np.arange(0, len(x2), 1, dtype=np.int)
            np.random.shuffle(rand_index)
            for i in range(math.ceil(float(len(x2))/self._batch_size)):
                begin_index = i * self._batch_size
                end_index = min(i * self._batch_size + self._batch_size, len(x2))
                index_for_this_batch = rand_index[begin_index:end_index]
                batch_x1 = x1[index_for_this_batch]
                batch_x2 = x2[index_for_this_batch]
                batch_t = t[index_for_this_batch]
                feed_dict={
                    input_1: batch_x1,
                    input_2: batch_x2,
                    input_t: batch_t
                }

                sess.run(train_step, feed_dict=feed_dict)

            if epoch % 1000 == 0:
                feed_dict={
                    input_1: x1,
                    input_2: x2,
                    input_t: t
                }
                l = sess.run(loss, feed_dict=feed_dict)
                print('{:.8f}'.format(l))

        return_w = sess.run(w)
        return_b = sess.run(b)

        return return_w, return_b

if __name__ == '__main__':
    other_vec_size = 20
    embedding_size = 300

    my_vec = np.random.rand(embedding_size)*40-20
    other_vecs = [np.random.rand(embedding_size)*40-20 for i in range(other_vec_size)]

    my_vec = my_vec / np.linalg.norm(my_vec)
    other_vecs = other_vecs / np.linalg.norm(other_vecs, axis=1).reshape(other_vec_size, 1)

    cos = np.dot(other_vecs, my_vec.T)
    print(cos)
    for i, c in enumerate(cos):
        cos[i] -= np.random.rand(1)*2-1
    print(cos)

    vec_and_cos = [(v, c) for v, c in zip(other_vecs, cos)]

    adjust_vector = AdjustVector(embedding_size=embedding_size, epoch_count=20000)
    w, b = adjust_vector.get_adjust_vector(my_vec, vec_and_cos)
    print('answer: {}'.format(cos))
    print('result: {}'.format(np.dot(other_vecs, (my_vec*w+b).T)))

reason: import numpy


text:
"""
script to collect training data for the SVM classifying with using
picamea on the Raspberry Pi to continuous capturing images.

MIT License

Copyright (c) 2019 JinJie Chen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

"""

# import the necessary packages
import argparse
import cv2
import numpy as np
import os  # import os to set environmental variable
import picamera
import sys
import time
from picamera.array import PiRGBArray
from PIL import Image
from PIL import ImageDraw

"""
collecting num_img number of image using picamera, save image user the
user_id folderdirectory
"""
def collect_images(num_img, index, user_id):
    with picamera.PiCamera() as camera:
        camera.resolution = (720, 720)
        camera.framerate = 30
        # allow the camera to warmup
        time.sleep(1)
        # frame buffer
        rawCapture = PiRGBArray(camera, size=camera.resolution)
        time.sleep(0.5)
        i = index
        # capture frames from the camera
        for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): #, resize=(width, height)):
            # obtain the frame array
            frame = frame.array
            cv2.imshow("Identification", frame)
            # trying saving the captured image
            try:
                print(cv2.imwrite('raw_image_data/'+ str(user_id) +'/'+str(i)+'.jpg', frame))
            except Exception as e:
                print ('[Error] image write: ',e)
            i += 1
            if i-index >= num_img:
                break
            # clear the stream in preparation for the next frame
            rawCapture.truncate(0)

parser = argparse.ArgumentParser()
parser.add_argument(
    '--n_img', help='number of images to collect.', default=1)
parser.add_argument(
    '--idx', help='Start index of image file name.', default=0)
parser.add_argument(
    '--user', help='user id')
args = parser.parse_args()
print(args)

collect_images(int(args.n_img), int(args.idx), args.user)


text:
import pickle
import matplotlib.pyplot as plt
import numpy as np
from sklearn.pipeline import Pipeline
import joblib as jl
from .utils import generate_video_from_images, join_video_and_audio
import os
#root_dir = os.path.dirname(os.path.realpath(__file__))

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from scipy.spatial.transform import Rotation as R

def pre_process_motion_data(clip, init_rot=None, init_trans=None, fps=30, axis_scale=50, elev=45, azim=45):
    rot = init_rot if init_rot is not None else R.from_quat([0,0,0,1])
    translation = init_trans if init_trans is not None else np.array([[0,0,0]])
    translations = np.zeros((clip.shape[0],3))

    joints, root_dx, root_dz, root_dr = clip[:,:-3], clip[:,-3], clip[:,-2], clip[:,-1]
    joints = joints.reshape((len(joints), -1, 3))
    for i in range(len(joints)):
        joints[i,:,:] = rot.apply(joints[i])
        joints[i,:,0] = joints[i,:,0] + translation[0,0]
        joints[i,:,2] = joints[i,:,2] + translation[0,2]
        rot = R.from_rotvec(np.array([0,-root_dr[i],0])) * rot
        translation = translation + rot.apply(np.array([root_dx[i], 0, root_dz[i]]))
        translations[i,:] = translation

    return joints, translation, rot, translations

def generate_video_from_moglow_loc(data, control, output_folder, seq_id, audio_file, fps, trim_audio=0):
    # import pdb;pdb.set_trace()
    clip = np.concatenate([data,control], axis=1)

    joints, translation, rot, translations = pre_process_motion_data(clip, fps=20, axis_scale=50, elev=45, azim=45)

    fig = plt.figure()
    plt.ion()
    plt.show()
    ax = Axes3D(fig)
    keypoints3d = joints

    ax.scatter(keypoints3d[0,:,0],keypoints3d[0,:,1],keypoints3d[0,:,2])

    imgs_folder="analysis/visualization/img/"+seq_id
    if not os.path.exists(imgs_folder):
        os.mkdir(imgs_folder)

    plt.xlim([-10,10])
    plt.ylim([0,20])
    ax.set_zlim([-10,10])
    ax.view_init(90,-90)
    plt.draw()
    plt.pause(0.001)
    plt.savefig(imgs_folder+"/img_"+str(0)+".png")

    for i in range(1,len(keypoints3d)):
        print(i)
        ax.clear()
        ax.scatter(keypoints3d[i,:,0], keypoints3d[i,:,1], keypoints3d[i,:,2])
        plt.xlim([-10,10])
        plt.ylim([0,20])
        ax.set_zlim([-10,10])
        ax.view_init(90,-90)
        plt.draw()
        plt.pause(0.001)
        plt.savefig(imgs_folder+"/img_"+str(i)+".png")

    video_file = f'{output_folder}/{seq_id}.mp4'
    generate_video_from_images(imgs_folder, video_file, fps)
    if audio_file is not None:
        join_video_and_audio(video_file, audio_file, trim_audio)

max_stars_repo_name: guillefix/mt-lightning

max_issues_repo_name: guillefix/mt-lightning

max_forks_repo_name: guillefix/transflower-lightning


text:
import numpy
import sys
import velocity
from math import sqrt

def sin_wave(t, prop=0.0):
  global A
  return A * numpy.sin(2 * numpy.pi * t / T - prop)

def variables(Nx, Ny, Lx, Ly, c, g_, H, Tin, A0):
  global A, T, g
  g = g_
  A = A0
  T = Tin
  x = numpy.linspace(0, Lx, Nx + 3)
  y = numpy.linspace(0, Ly, Ny + 2)
  dx = Lx / Nx
  dy = Ly / Ny
  dt = round(sqrt(2) * dx * dy / c / (dx + dy))
  print('Calculated Dx: {}'.format(dx))
  print('Calculated Dy: {}'.format(dy))
  print('Calculated Dt: {}'.format(dt))
  if not dt <= numpy.sqrt(2*dx/(g*H)) and dt <= numpy.sqrt(2*dy/(g*H)):
    print("CFL criterion not meet and the solution will be unstable")
    f = numpy.sqrt(2*dx/(g*H))
    s = numpy.sqrt(2*dx/(g*H))
    print(dt, f, s)

  if (T / dt) < 10:  # CFL Condition
    print('The calculated Dt is not at least an order\n'
      'of magnitude less than the tidal wave period.\n'
      'Please change Nx and/or Ny')
    sys.exit()
  return x, y, dx, dy, dt

def calculate_water_level(Lx, Ly, Nx, Ny, dx, dy, dt, c, days, P=None):
  # P : tuple of a point's coord to get the time series
  print('Calculating water level profile for {} days...'.format(days))
  # Useful variables
  Cxs = (c * dt / dx) ** 2  # Courant number for X - squared
  Cys = (c * dt / dy) ** 2  # Courant number for Y - squared

  # Instantiating arrays
  # numpy.zeros(rows, columns)
  u = numpy.zeros((Ny + 2, Nx + 3))  # u(n+1)(i,j) -> solution array
  u_1 = numpy.zeros((Ny + 2, Nx + 3))# u(n)(i,j)   -> solution at t-1
  u_2 = numpy.zeros((Ny + 2, Nx + 3))# u(n-1)(i,j) -> solution at t-2

  # For time series plot
  u_ = []
  uvel_ = []
  vvel_ = []
  t_ = []

  t = 0
  n = 0
  # Loop Start
  while True:
    t = t + dt
    n = n + 1
    for i in range(1, Ny + 1):
      for j in range(1, Nx + 2):
        u_xx = u_1[i + 1, j] - 2 * u_1[i, j] + u_1[i - 1, j]
        u_yy = u_1[i, j + 1] - 2 * u_1[i, j] + u_1[i, j - 1]
        u[i, j] = 2 * u_1[i, j] - u_2[i, j] + Cxs * u_xx + Cys * u_yy

    # Open sea boundary
    for j in range(Nx + 2):
      if t-dt > dy/c:
        u1 = u_1[0, j] - sin_wave(t - dt)
        u2 = u_1[1, j] - sin_wave(t - dt, dy / Ly)
        u1 = u1 + dt / dy * c * (u2 - u1)
      else:
        u1 = 0
      u[0, j] = u1 + sin_wave(t)

    # Closed boundary with ghost cells
    # Fully reflective boundaries (Neumann Conditions)
    # * top (i = Ny)
    u[-1, :] = u[-2, :]
    # * left (j = Nx)
    u[:, -1] = u[:, -2]
    # * right (j = 0)
    u[:, 0] = u[:, 1]

    if P is not None:
      xp = P[0]
      yp = P[1]
      u_.append(u[xp, yp])
      t_.append(t)

    u_2 = numpy.copy(u_1)
    u_1 = numpy.copy(u)

    u_vel, v_vel = velocity.velocity(dx, dy, dt, g, u, Nx, Ny)
    if P is not None:
      xp = P[0]
      yp = P[1]
      uvel_.append(u_vel[xp, yp])
      vvel_.append(v_vel[xp, yp])

    if t > 3600 * 24 * days:
      break

  return u, u_vel, v_vel, u_, uvel_, vvel_, t_

max_stars_repo_name: nikolisan/Diploma-Thesis

max_issues_repo_name: nikolisan/Diploma-Thesis

max_forks_repo_name: nikolisan/Diploma-Thesis


text:
from __init__ import OUTPUT

from tqdm import tqdm
import pandas as pd
from rdkit import Chem
import os, sys
import numpy as np
import joblib

print("CLASSIFIER HIGH ACTIVE")

df=pd.read_csv(os.path.join(OUTPUT, "data_7.csv"))

smiles = list(df["Smiles"])

ml_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "highpredictor", "results")

from mordred import Calculator, descriptors

sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "predictor"))

from descriptors.utils import impute, normalize

IGNORE_3D = False

class Mordred(object):

    def __init__(self):
        pass

    def calc(self, mols):
        calc = Calculator(descriptors, ignore_3D=IGNORE_3D)
        df = calc.pandas(mols)
        return np.array(df, dtype=np.float)

desc = Mordred()

print("Loading transformation data")

with open(os.path.join(ml_folder, "medians.npy"), "rb") as f:
    medians = np.load(f)

with open(os.path.join(ml_folder, "mus.npy"), "rb") as f:
    mus = np.load(f)

with open(os.path.join(ml_folder, "sigmas.npy"), "rb") as f:
    sigmas = np.load(f)

with open(os.path.join(ml_folder, "sel1.npy"), "rb") as f:
    sel_idxs = np.load(f)

def chunker(lst, n):
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

print("Loading classifier model")
sel0 = joblib.load(os.path.join(ml_folder, "sel0.pkl"))
mdl = joblib.load(os.path.join(ml_folder, "classifier.pkl"))

yp = []
done = 0
print("Total", len(smiles))
for chunk in chunker(smiles, 1000):
    print("Calculating descriptors")
    mols = [Chem.MolFromSmiles(smi) for smi in tqdm(chunk)]
    X = desc.calc(mols)
    print(X.shape)
    X = impute(X, medians)
    X = normalize(X, mus, sigmas)
    print("Predicting")
    X = sel0.transform(X)
    print(X.shape)
    X = X[:,sel_idxs]
    print(X.shape)
    yp += list(mdl.predict_proba(X)[:,1])
    done += len(chunk)
    print("Done", done)

yp = np.array(yp)

print(df.shape)
df["HighClassifier"] = yp

print("Not very trustable predictor, only removing lowest quartile")
p25 = np.percentile(yp, 25)
print(p25)
df = df[df["HighClassifier"] > p25]

df.to_csv(os.path.join(OUTPUT, "data_8.csv"), index=False)


text:
import numpy as np
import cv2 as cv
import cmath

#cap = cv.VideoCapture(0)
cap = cv.VideoCapture('udpsrc port=5004 ! application/x-rtp,encoding-name=H264,payload=96 ! rtph264depay ! avdec_h264 ! videoconvert ! appsink', cv.CAP_GSTREAMER)

PI = 3.14159

while(1):
    # read the video capture frame
    _, frame = cap.read()
    #cv.imshow('frame',frame)
    #break
    # blur for better edge finding
    blur = cv.GaussianBlur(frame,(5,5),0)
    frameGray = cv.cvtColor(blur, cv.COLOR_BGR2GRAY)
    # create threshold for edge finding
    ret, thresh = cv.threshold(frameGray, 120, 255, cv.THRESH_BINARY)
    contours, _ = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
    count = 0
    tri = 0
    sqr = 0
    rect = 0
    circ = 0
    for contour in contours:
        area = cv.contourArea(contour)
        if area > 1000 and area < 30000:
            M = cv.moments(contour)
            cX = int(M["m10"]/M["m00"])
            cY = int(M["m01"]/M["m00"])
            if(frame[cY,cX][0] < 50 and frame[cY,cX][1] < 50 and frame[cY,cX][2] < 50):
                cv.circle(frame, (cX,cY), 7, (255,255,0), -1)
                #cv.drawContours(frame, contour, -1, (0,255,0), 3)
                count += 1

                (x,y), (MA, ma), angle = cv.fitEllipse(contour)
                areaEllipse = PI/4 * MA * ma
                if(abs(areaEllipse - area) < 100):
                    #is circle
                    circ += 1
                    cv.drawContours(frame, contour, -1, (0,255,255), 3)
                else:
                    ((x,y), (w,h), rot) = cv.minAreaRect(contour)
                    if(float(w) > 0.0 and float(h) > 0.0):
                        ratio = w / float(h)
                        #font = cv.FONT_HERSHEY_COMPLEX_SMALL
                        #cv.putText(frame, str(ratio), (cX, cY - 40), font, 2, (0, 0, 255), 2, cv.LINE_AA)

                        if ratio <= 0.6  or ratio >= 2.8:
                            #is rect
                            cv.drawContours(frame, contour, -1, (0,255,0), 3)
                            rect += 1
                        else:
                            #peri = cv.arcLength(contour, True)
                            #approx = cv.approxPolyDP(contour, 0.04 * peri, True)
                            #if len(approx) == 3:

                            areaAdj = 1400
                            #font = cv.FONT_HERSHEY_COMPLEX_SMALL
                            #cv.putText(frame, str(int(area)), (cX, cY - 40), font, 2, (0, 0, 255), 2, cv.LINE_AA)
                            #cv.putText(frame, str(int(w*h/2)), (cX, cY - 60), font, 2, (0, 0, 255), 2, cv.LINE_AA)
                            if(w*h/2 > area - areaAdj and w*h/2 < area + areaAdj):
                                #is triangle

                                cv.drawContours(frame, contour, -1, (255,0,0), 3)
                                tri += 1
                            else:
                                #is square
                                sqr += 1
                                cv.drawContours(frame, contour, -1, (0,0,255), 3)

    cv.circle(frame, (70, 300), 20, (0,0,255), -1)
    pts = np.array([[70, 330], [50, 360], [90, 360]], np.int32)
    pts = pts.reshape((-1,1,2))
    cv.fillPoly(frame, [pts], (0, 0, 255))
    cv.rectangle(frame, (50, 381), (90, 389), (0,0,255), -1)
    cv.rectangle(frame, (50, 410), (90, 450), (0,0,255), -1)
    font = cv.FONT_HERSHEY_COMPLEX_SMALL
    cv.putText(frame, str(circ), (10, 310), font, 2, (0, 0, 255), 2, cv.LINE_AA)
    cv.putText(frame, str(tri), (10, 355), font, 2, (0, 0, 255), 2, cv.LINE_AA)
    cv.putText(frame, str(rect), (10, 400), font, 2, (0, 0, 255), 2, cv.LINE_AA)
    cv.putText(frame, str(sqr), (10, 445), font, 2, (0, 0, 255), 2, cv.LINE_AA)

    cv.imshow('frame',frame)
    #cv.imshow('thresh', thresh)

    k = cv.waitKey(5) & 0xFF
    if k == 27:
        break

cv.destroyAllWindows()
cap.release()


text:
import numpy as np
import os, yaml, zarr
from astropy.wcs import WCS
from scipy.io import readsav
from tqdm import tqdm

def memmap_crisp_cube(path):
    """
    This function memory maps a legacy La Palma data cube pulling metainformation from appropriate files. The function first looks for an ``assoc.pro`` file which contains important information about the first dimension. If this does not exist then the user will have to untangle the first dimension themselves. The first dimension is a multiplicative combination of time and wavelength (and Stokes) with the ``assoc.pro`` providing the necessary information to split these into distinct axes. When unable to access this information, the first dimension will be the multiplicative combination of time and wavelength (and Stokes) with the ordering of one time for all wavelengths sampled. N.B. when Stokes is present, the first axis takes the form of one time for wavelengths sampled of Stokes I, wavelengths sampled of Stokes Q etc.

    Parameters
    ----------
    path : str
        The path to the legacy La Palma cube.
    """

    if not os.path.exists(path):
        raise ValueError(f"No path found at {path}")

    folder, item = os.path.split(path)
    files = os.listdir(folder)
    assoc_name = ".".join(item.split(".")[:-1]) + ".assoc.pro" #assoc file contains some meta information and the name usually follows similar convention to the La Palma cube without the file suffix

    if assoc_name in files:
        # Read out of the assoc file so no need to find meta information!!
        with open(folder+"/"+assoc_name) as f:
            assoc_pro = f.readlines() #read the file into variable
        dims = {}
        keys = ["nx", "ny", "nw", "nt"] #assoc file will contain at least the number of x points, y points, wavelength points ("nw") and time points

        for a in assoc_pro:
            if any(k in a for k in keys) and "=" in a and "assoc" not in a:
                key_val = a.split("=")
                dims[key_val[0]] = int(key_val[1])
        shape = [dims["nt"], dims["nw"], dims["ny"], dims["nx"]] #this is how the data is stored
        if "stokes" in item:
            shape.insert(1, 4)
    else:
        # Read directly from the cube (it is really good if there is an assoc pro file but this works *just* fine)
        header = np.memmap(path, np.uint8, mode="r")[:512].tostring() #this first 512 bytes of the La Palma cube are header information....
        entries = str(header).split()
        keys = ["nx", "ny", "nt"] #this meta information stores wavelength and time multiplied as the same axis which is why the assoc pro file is really useful
        dims = {}
        for e in entries:
            if any(k in e for k in keys) and "=" in e:
                key_val = e.stip(",").split("=")
                dims[key_val[0]] = key_val[1]

        shape = [dims["nt"], dims["ny"], dims["nx"]]

    if "icube" in item:
        dtype = np.int16
    elif "fcube" in item:
        dtype = np.float
    else:
        raise ValueError("Unknown cube type.")

    return np.memmap(path, offset=512, dtype=dtype, mode="r", shape=tuple(shape))

def zarr_header_to_wcs(zarr_header, nonu=False):
    """
    This function takes the zarr header information and converts it to a wcs object.

    Parameters
    ----------
    zarr_header : dict or yaml or JSON
        The header information from the zarr observation file.
    nonu : bool, optional
        Whether or not the spectral points are sampled non-uniformly. Default is False.
    """

    wcs_dict = {}

    wcs_dict["TELESCOP"] = zarr_header["telescope"]
    wcs_dict["INSTRUME"] = zarr_header["instrument"]
    wcs_dict["EXT_NAME"] = zarr_header["element"]

    if len(zarr_header["dimensions"]) == 4:
        # FITS axes are in the opposite order from numpy axes so the dimensions are read in reverse
        wcs_dict["NAXIS1"] = zarr_header["dimensions"][-1]
        wcs_dict["NAXIS2"] = zarr_header["dimensions"][-2]
        wcs_dict["NAXIS3"] = zarr_header["dimensions"][-3]
        wcs_dict["NAXIS4"] = zarr_header["dimensions"][-4]

        wcs_dict["CTYPE1"] = "HPLN-TAN"
        wcs_dict["CTYPE2"] = "HPLT-TAN"
        wcs_dict["CTYPE3"] = "WAVE"
        wcs_dict["CTYPE4"] = "STOKES"

        wcs_dict["CUNIT1"] = "arcsec"
        wcs_dict["CUNIT2"] = "arcsec"
        wcs_dict["CUNIT3"] = "Angstrom"

        wcs_dict["CRPIX1"] = zarr_header["crpix"][-1]
        wcs_dict["CRPIX2"] = zarr_header["crpix"][-2]
        wcs_dict["CRPIX3"] = zarr_header["crpix"][-3]
        wcs_dict["CRPIX4"] = zarr_header["crpix"][-4]

        wcs_dict["CRVAL1"] = zarr_header["crval"][-1]
        wcs_dict["CRVAL2"] = zarr_header["crval"][-2]
        wcs_dict["CRVAL3"] = zarr_header["crval"][-3]
        wcs_dict["CRVAL4"] = 1.0

        wcs_dict["CDELT1"] = zarr_header["pixel_scale"]
        wcs_dict["CDELT2"] = zarr_header["pixel_scale"]
        if nonu:
            wcs_dict["CDELT3"] = 0.1
        else:
            wcs_dict["CDELT3"] = zarr_header["wavel_scale"]
        wcs_dict["CDELT4"] = 1.0
    elif len(zarr_header["dimensions"]) == 3:
        wcs_dict["NAXIS1"] = zarr_header["dimensions"][-1]
        wcs_dict["NAXIS2"] = zarr_header["dimensions"][-2]
        wcs_dict["NAXIS3"] = zarr_header["dimensions"][-3]

        wcs_dict["CTYPE1"] = "HPLN-TAN"
        wcs_dict["CTYPE2"] = "HPLT-TAN"
        wcs_dict["CTYPE3"] = "WAVE"

        wcs_dict["CUNIT1"] = "arcsec"
        wcs_dict["CUNIT2"] = "arcsec"
        wcs_dict["CUNIT3"] = "Angstrom"

        wcs_dict["CRPIX1"] = zarr_header["crpix"][-1]
        wcs_dict["CRPIX2"] = zarr_header["crpix"][-2]
        wcs_dict["CRPIX3"] = zarr_header["crpix"][-3]

        wcs_dict["CRVAL1"] = zarr_header["crval"][-1]
        wcs_dict["CRVAL2"] = zarr_header["crval"][-2]
        wcs_dict["CRVAL3"] = zarr_header["crval"][-3]

        wcs_dict["CDELT1"] = zarr_header["pixel_scale"]
        wcs_dict["CDELT2"] = zarr_header["pixel_scale"]
        if nonu:
            wcs_dict["CDELT3"] = 0.1
        else:
            wcs_dict["CDELT3"] = zarr_header["wavel_scale"]
    elif len(zarr_header["dimensions"]) == 2:
        wcs_dict["NAXIS1"] = zarr_header["dimensions"][-1]
        wcs_dict["NAXIS2"] = zarr_header["dimensions"][-2]

        wcs_dict["CTYPE1"] = "HPLN-TAN"
        wcs_dict["CTYPE2"] = "HPLT-TAN"

        wcs_dict["CUNIT1"] = "arcsec"
        wcs_dict["CUNIT2"] = "arcsec"

        wcs_dict["CRPIX1"] = zarr_header["crpix"][-1]
        wcs_dict["CRPIX2"] = zarr_header["crpix"][-2]

        wcs_dict["CRVAL1"] = zarr_header["crval"][-1]
        wcs_dict["CRVAL2"] = zarr_header["crval"][-2]

        wcs_dict["CDELT1"] = zarr_header["pixel_scale"]
        wcs_dict["CDELT2"] = zarr_header["pixel_scale"]

    return WCS(wcs_dict)

def la_palma_cube_to_hdf5(cube_path, tseries_path, spectfile, date_obs, telescope, instrument, pixel_scale, cadence, element, pointing, mu=None, start_idx=0, save_dir="./"):
    """
    This is a function to save a La Palma legacy cube as zarr files.

    Parameters
    ----------
    cube_path : str
        The path to the legacy La Palma data cube.
    tseries_path : str
        The path to the ``tseries`` file containing the information on the times of the observations.
    spectfile : str
        The path to the file containing the spectral positions sampled.
    data_obs : str
        The observation date.
    telescope : str
        The telescope used.
    instrument : str
        The instrument used.
    pixel_scale : float
        The size of a detector pixel in arcseconds.
    cadence : float
        The time sampling of the observations.
    element : str
        The transition observed.
    pointing : tuple[float]
        The pointing of the centre of the images in the Helioprojective plane.
    mu : float or None, optional
        The direction cosine of the observations. Default is None.
    start_idx : int, optional
        The time index to start making files from. Default is 0.
    save_dir : str, optional
        The directory to save the hdf5 files in. Default is the current directory.
    """

    header = {}
    header["date_obs"] = date_obs
    header["telescope"] = telescope
    header["instrument"] = instrument
    header["pixel_scale"] = pixel_scale
    header["cadence"] = cadence
    header["element"] = element
    if mu is not None:
        header["mu"] = mu

    mem_cube = memmmap_crisp_cube(cube_path)
    times = readsav(tseries_path)["time"]

    if "wb" not in cube_path and mem_cube.ndim == 5:
        #this means there is Stokes
        header["ctype"] = ("STOKES", "WAVE", "HPLT-TAN", "HPLN-TAN")
        header["cunit"] = ("Angstrom", "arcsec", "arcsec")
        header["spect_pos"] = readsav(spectfile)["spect_pos"]
    elif "wb" not in cube_path and mem_cube.ndim == 4:
        header["ctype"] = ("WAVE", "HPLT-TAN", "HPLN-TAN")
        header["cunit"] = ("Angstrom", "arcsec", "arcsec")
        header["spect_pos"] = readsav(spectfile)["spect_pos"]
    else:
        header["ctype"] = ("HPLT-TAN", "HPLN-TAN")
        header["cunit"] = ("arcsec", "arcsec")

    for jj, frame in enumerate(tqdm(mem_cube)):
        header["time_obs"] = times[jj].deconde("utf-8")
        if "wb" not in cube_path and frame.ndim == 4:
            header["crpix"] = (1, np.median(np.arange(frame.shape[-3])), np.median(np.arange(frame.shape[-2])), np.median(np.arange(frame.shape[-1])))
            header["crval"] = (1, np.median(header["spect_pos"]), pointing[-2], pointing[-1])
            f = zarr.open(save_dir+str(start_idx).zfill(5)+".zarr", mode="w")
            data = f.array("data", frame, chunks=(1,1,frame.shape[-2],frame.shape[-1]))
            for k, v in header.items():
                data.attrs[k] = v
            start_idx += 1
        elif "wb" not in cube_path and frame.ndim == 3:
            header["crpix"] = (np.median(np.arange(frame.shape[-3])), np.median(np.arange(frame.shape[-2])), np.median(np.arange(frame.shape[-1])))
            header["crval"] = (np.median(header["spect_pos"]), pointing[-2], pointing[-1])
            f = zarr.open(save_dir+str(start_idx).zfill(5)+".zarr", mode="w")
            data = f.array("data", frame, chunks=(1,frame.shape[-2],frame.shape[-1]))
            for k, v in header.items():
                data.attrs[k] = v
            start_idx += 1
        else:
            header["crpix"] = (np.median(np.arange(frame.shape[-2])), np.median(np.arange(frame.shape[-1])))
            header["crval"] = (pointing[-2], pointing[-1])
            f = zarr.open(save_dir+str(start_idx).zfill(5)+".zarr", mode="w")
            data = f.array("data", frame, chunks=(1,frame.shape[-2],frame.shape[-1]))
            for k, v in header.items():
                data.attrs[k] = v
            start_idx += 1

reason: import numpy,from numpy,from scipy,from astropy


text:
#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
# 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 math
import paddle
import numpy as np
from PIL import Image

irange = range
def make_grid(tensor, nrow=8, normalize=False, range=None, scale_each=False):
    """Make a grid of images.
    Args:
        tensor (Tensor or list): 4D mini-batch Tensor of shape (B x C x H x W)
            or a list of images all of the same size.
        nrow (int, optional): Number of images displayed in each row of the grid.
            The final grid size is ``(B / nrow, nrow)``. Default: ``8``.
        normalize (bool, optional): If True, shift the image to the range (0, 1),
            by the min and max values specified by :attr:`range`. Default: ``False``.
        range (tuple, optional): tuple (min, max) where min and max are numbers,
            then these numbers are used to normalize the image. By default, min and max
            are computed from the tensor.
        scale_each (bool, optional): If ``True``, scale each image in the batch of
            images separately rather than the (min, max) over all images. Default: ``False``.
    """
    if not (isinstance(tensor, paddle.Tensor) or
            (isinstance(tensor, list) and all(isinstance(tensor, t) for t in tensor))):
        raise TypeError('tensor or list of tensors expected, got {}'.format(type(tensor)))

    # if list of tensors, convert to a 4D mini-batch Tensor
    if isinstance(tensor, list):
        tensor = paddle.stack(tensor, 0)

    if tensor.dim() == 2:  # single image H x W
        tensor = tensor.unsqueeze(0)
    if tensor.dim() == 3:  # single image
        if tensor.shape[0] == 1:  # if single-channel, convert to 3-channel
            tensor = paddle.concat([tensor, tensor, tensor], 0)
        tensor = tensor.unsqueeze(0)

    if tensor.dim() == 4 and tensor.shape[1] == 1:  # single-channel images
        tensor = paddle.concat([tensor, tensor, tensor], 1)

    if normalize is True:
        tensor = tensor.astype(tensor.dtype)  # avoid modifying tensor in-place
        if range is not None:
            assert isinstance(range, tuple), \
                "range has to be a tuple (min, max) if specified. min and max are numbers"

        def norm_ip(img, min, max):
            img[:] = img.clip(min=min, max=max)
            img[:] = (img - min) / (max - min + 1e-5)

        def norm_range(t, range):
            if range is not None:
                norm_ip(t, range[0], range[1])
            else:
                norm_ip(t, float(t.min()), float(t.max()))

        if scale_each is True:
            for t in tensor:  # loop over mini-batch dimension
                norm_range(t, range)
        else:
            norm_range(tensor, range)

    if tensor.shape[0] == 1:
        return tensor.squeeze(0)

    # make the mini-batch of images into a grid
    nmaps = tensor.shape[0]
    xmaps = min(nrow, nmaps)
    ymaps = int(math.ceil(float(nmaps) / xmaps))
    height, width = int(tensor.shape[2]), int(tensor.shape[3])
    num_channels = tensor.shape[1]
    canvas = paddle.zeros((num_channels, height * ymaps, width * xmaps), dtype=tensor.dtype)
    k = 0
    for y in irange(ymaps):
        for x in irange(xmaps):
            if k >= nmaps:
                break
            canvas[:, y * height:(y + 1) * height, x * width:(x + 1) * width] = tensor[k]
            k = k + 1
    return canvas

def tensor2img(input_image, min_max=(-1., 1.), imtype=np.uint8):
    """"Converts a Tensor array into a numpy image array.

    Parameters:
        input_image (tensor) --  the input image tensor array
        imtype (type)        --  the desired type of the converted numpy array
    """
    if not isinstance(input_image, np.ndarray):
        image_numpy = input_image.numpy()  # convert it into a numpy array
        if len(image_numpy.shape) == 4:
            image_numpy = image_numpy[0]
        if image_numpy.shape[0] == 1:  # grayscale to RGB
            image_numpy = np.tile(image_numpy, (3, 1, 1))
        image_numpy = image_numpy.clip(min_max[0], min_max[1])
        image_numpy = (image_numpy - min_max[0]) / (min_max[1] - min_max[0])
        image_numpy = (np.transpose(
            image_numpy,
            (1, 2, 0))) * 255.0  # post-processing: tranpose and scaling
    else:  # if it is a numpy array, do nothing
        image_numpy = input_image
    return image_numpy.astype(imtype)

def save_image(image_numpy, image_path, aspect_ratio=1.0):
    """Save a numpy image to the disk

    Parameters:
        image_numpy (numpy array) -- input numpy array
        image_path (str)          -- the path of the image
    """

    image_pil = Image.fromarray(image_numpy)
    h, w, _ = image_numpy.shape

    if aspect_ratio > 1.0:
        image_pil = image_pil.resize((h, int(w * aspect_ratio)), Image.BICUBIC)
    if aspect_ratio < 1.0:
        image_pil = image_pil.resize((int(h / aspect_ratio), w), Image.BICUBIC)
    image_pil.save(image_path)

def mask2image(mask: np.array, format="HWC"):
    H, W = mask.shape

    canvas = np.zeros((H, W, 3), dtype=np.uint8)
    for i in range(int(mask.max())):
        color = np.random.rand(1, 1, 3) * 255
        canvas += (mask == i)[:, :, None] * color.astype(np.uint8)
    return canvas

max_stars_repo_name: JetHong/PaddleGAN


text:
# -*- coding:utf8 -*-
import argparse
import csv

import matplotlib.font_manager as mfm
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import numpy as np
font_path = './STHeitiLight.ttc'
prop = mfm.FontProperties(fname=font_path)

parser = argparse.ArgumentParser(description='')
parser.add_argument('--plt_name', default='')
parser.add_argument('--data', default='')
args = parser.parse_args()
print(args)

datafile = args.data
plt_name = args.plt_name

def sortedDictValues1(adict):
    items = adict.items()
    sorted(items)
    return [key for key, _ in items], [value for _, value in items]

def load_data():
    csvfile = csv.DictReader(open(datafile), delimiter='\t')
    datas = {}
    for row in csvfile:
        row['dealdate'] = row['dealdate'][:7]
        row['totalPrice'] = row['totalPrice'].split('-')[0]
        row['unitPrice'] = row['unitPrice'].split('-')[0]
        count = float(row['count'])
        datas[row['dealdate']] = datas.get(row['dealdate'], np.array([0., 0., 0., 0.])) + np.array(
            [float(row['totalPrice']) * count, float(row['unitPrice']) * count, float(row['square'])*count, count])

    datas = sortedDictValues1(datas)

    dates = datas[0]
    AverageTotalPrice = [item[0]/item[-1] for item in datas[1]]
    AverageUnitPrice = [item[1]/item[-1] for item in datas[1]]
    AverageSquare = [item[2]/item[-1] for item in datas[1]]
    DealCounts = [item[-1] for item in datas[1]]
    return dates, AverageTotalPrice, AverageUnitPrice, DealCounts, AverageSquare

if "__main__" == __name__:
    dates, AverageTotalPrice, AverageUnitPrice, DealCounts, AverageSquare = load_data()

    fig = plt.figure(figsize=(14, 8))
    plt.title(fontproperties=prop, label=plt_name)
    plt.xticks(range(len(dates)), dates, rotation=90, fontsize=6)
    plt.yticks([])

    host = host_subplot(111, axes_class=AA.Axes)
    plt.subplots_adjust(left=0.08, right=0.8)

    par1 = host.twinx()
    par2 = host.twinx()
    par3 = host.twinx()

    new_fixed_axis = par2.get_grid_helper().new_fixed_axis
    par2.axis["right"] = new_fixed_axis(loc="right", axes=par2, offset=(60, 0))

    new_fixed_axis = par3.get_grid_helper().new_fixed_axis
    par3.axis["right"] = new_fixed_axis(loc="right", axes=par3, offset=(120, 0))

    par1.axis["right"].toggle(all=True)
    par2.axis["right"].toggle(all=True)
    par3.axis["right"].toggle(all=True)

    host.set_xlim(0, len(dates)-1)
    host.set_ylim(0, max(AverageTotalPrice) * 1.2)

    host.set_ylabel(u"AverageTotalPrice")
    par1.set_ylabel("AverageUnitPrice")
    par2.set_ylabel("DealCounts")
    par3.set_ylabel("AverageSquare")

    p0, = host.plot(dates, AverageTotalPrice, label=u"AverageTotalPrice", linestyle=':', marker='*')
    p1, = par1.plot(range(len(dates)), AverageUnitPrice, label="AverageUnitPrice", linestyle='--', marker='+')
    p2, = par2.plot(range(len(dates)), DealCounts, label="DealCounts", linestyle='-', marker='o')
    p3, = par3.plot(range(len(dates)), AverageSquare, label="AverageSquare", linestyle=':', marker='+')

    par1.set_ylim(0, max(AverageUnitPrice) * 1.2)
    par2.set_ylim(0, max(DealCounts) * 1.2)
    par3.set_ylim(0, max(AverageSquare) * 1.8)

    host.legend(loc='upper left')

    host.set_xticklabels(host.get_xticklabels(), rotation=90, fontsize=6)
    host.axis["left"].label.set_color(p0.get_color())
    par1.axis["right"].label.set_color(p1.get_color())
    par2.axis["right"].label.set_color(p2.get_color())
    par3.axis["right"].label.set_color(p3.get_color())

    plt.show()