Esempio n. 1
0
def show_spec_in_graph(graph, vertex, spec, pos, weight, file_name):
    dist = 1.0 - squareform(pdist(spec.T, 'cosine'))

    plt.figure()
    plt.stem(dist[vertex, :], markerfmt=' ')

    rim = graph.new_vertex_property('vector<double>')
    rim.set_2d_array(np.array([0, 0, 0, 1]))
    rim[graph.vertex(vertex)] = [0.8941176471, 0.1019607843, 0.1098039216, 1]
    rim_width = graph.new_vertex_property('float', vals=0.5)
    rim_width.a[vertex] = 2
    shape = graph.new_vertex_property('int', vals=0)
    shape[graph.vertex(vertex)] = 2
    size = graph.new_vertex_property('double', vals=10)
    size.a[vertex] = 15
    correlation = graph.new_vertex_property('double', vals=2)
    correlation.a = dist[vertex, :]
    vorder = graph.new_vertex_property('int', vals=0)
    vorder.a[vertex] = 1

    palette = sns.cubehelix_palette(256)
    cmap = colors.ListedColormap(palette)
    gt_draw.graph_draw(graph, pos=pos, vertex_color=rim, vorder=vorder,
                       vertex_pen_width=rim_width,
                       vertex_shape=shape, vertex_fill_color=correlation,
                       vcmap=cmap, vertex_size=size, edge_color=[0, 0, 0, 0.7],
                       edge_pen_width=weight, output=file_name + '.png',
                       output_size=(1200, 1200))

    plt.figure()
    utils.plot_colorbar(cmap, np.arange(0, 1.01, 0.2), file_name)
Esempio n. 2
0
    def make_polarization_map(self):



        self.logger.info('Requested the creation of a polarization image.')

        output_folder=os.path.dirname(os.path.realpath(__file__))+'/'


        rescaled_flux=np.sqrt( np.power( self.stokes_Q,2) + np.power( self.stokes_U,2))


        colormap=sns.cubehelix_palette(start=2.3, light=1,gamma=0.8, as_cmap=True,
                                                                           reverse=True)
        self.logger.info('Constructing the background of the polarization map')
        # CONSTRUCTION OF THE BACKGROUND

        #Get the figure axes
        figure_axes=plt.gca()
        #Construct the flux map as an array image
        flux_map_background=figure_axes.imshow(rescaled_flux,origin='lower',aspect=None
                                               ,cmap=colormap)

        self.logger.info('Constructing the lines of the polarization map')

        #  CONSTRUCTION OF THE VECTOR PLOT

        #Get the polarization lines from the polarization angle

        lines =construct_lines_from_angle(self.pol_angle,self.total_flux)

        # Add the lines to the matplotlib collection lines with a good widths

        lc = plt_collections.LineCollection(lines, linewidths=.5,colors='white')

        # Add the lines to the current figure axes and scale

        figure_axes.add_collection(lc)
        figure_axes.autoscale()
        # figure_axes.margins(0)

        self.logger.info('Constructing the color bar of the polarization map')
        # #  CONSTRUCTION OF THE COLOR BAR
        #Construct the colorbar and place it on the top
        divider = make_axes_locatable(figure_axes)
        cax = divider.append_axes("top", size="4%", pad=0)
        image_colorbar=plt.colorbar(flux_map_background,orientation="horizontal",cax=cax)
        image_colorbar.ax.xaxis.set_ticks_position('top')

        #Get the figure, adjust it and save it in the folder

        fig = plt.gcf()
        fig.set_size_inches(18.5, 10.5)
        plt.savefig(output_folder+'results/Polarization_map.png', dpi=100)

        self.logger.info('Polarization map correctly constructed.')

        plt.close()
        warnings.filterwarnings("always")
Esempio n. 3
0
    def make_polarization_background(self):

        self.logger.info('Requested the creation of a background image.')

        output_folder=os.path.dirname(os.path.realpath(__file__))+'/'


        rescaled_flux=self.total_flux*1e7


        colormap=sns.cubehelix_palette(start=2.3, light=1,gamma=0.8, as_cmap=True,reverse=True)
        self.logger.info('Constructing the background.')
        # CONSTRUCTION OF THE BACKGROUND

        #Get the figure axes
        figure_axes=plt.gca()
        #Construct the flux map as an array image
        flux_map_background=figure_axes.imshow(rescaled_flux,origin='lower',aspect=None
                                               ,cmap=colormap)

        self.logger.info('Constructing the contours of the background image.')
        # CONSTRUCTION OF THE CONTOURS

        levels = np.linspace(np.max(rescaled_flux)*0.1,np.max(rescaled_flux)*0.6,5)

        contour_plot= plt.contour(rescaled_flux, levels,
                 origin='lower',
                 linewidths=1,
                 colors='w')

        # Construct labels for the contours
        warnings.filterwarnings("ignore")
        plt.clabel(contour_plot, fontsize=10)

        self.logger.info('Constructing the color bar of the background image.')


        # #  CONSTRUCTION OF THE COLOR BAR
        #Construct the colorbar and place it on the top
        divider = make_axes_locatable(figure_axes)
        cax = divider.append_axes("top", size="4%", pad=0)
        image_colorbar=plt.colorbar(flux_map_background,orientation="horizontal",cax=cax)
        image_colorbar.ax.xaxis.set_ticks_position('top')

        #Get the figure, adjust it and save it in the folder

        fig = plt.gcf()
        fig.set_size_inches(18.5, 10.5)
        plt.savefig(output_folder+'results/Polarization_contours.png', dpi=100)

        self.logger.info('Background image correctly constructed.')
        warnings.filterwarnings("always")
Esempio n. 4
0
def process_us():
    name = 'US'
    year = 2014
    n_neigh = 6

    graph = temperatures.country_network(name, 2014, n_neigh=6)
    pos = graph.vertex_properties['pos']
    station_values = graph.vertex_properties['station_values']
    weight = graph.edge_properties['weights']
    x_signal = station_values.a

    n_eigs = 500
    # n_eigs = graph.num_vertices() - 1
    alpha = -1e-3

    file_name = '{0}_{1}_k{2}'
    file_name = file_name.format(name, year, n_neigh)
    if os.path.exists(file_name + '_spec.pickle'):
        with open(file_name + '_spec.pickle', 'r') as f:
            spec_weighted = pickle.load(f)
            factory = pickle.load(f)
    else:
        factory = spec.PageRankSGFT(graph, n_eigs, alpha, weight=weight)
        spec_weighted = factory.compute(x_signal)
        with open(file_name + '_spec.pickle', 'w') as f:
            pickle.dump(spec_weighted, f)
            pickle.dump(factory, f)

    palette = sns.cubehelix_palette(256, start=2, rot=0, dark=0.15, light=1)
    cmap = colors.ListedColormap(palette)
    plt.figure()
    show_spectrogram(spec_weighted[0:30, :], cmap=cmap)
    plt.savefig(file_name + '_spec.pdf', dpi=300)

    temperatures.plot(graph, weight, pos, station_values, file_name)
    spec.show_window(factory, .5 * (graph.num_vertices() + 1), weight, pos,
                     file_name + '_window1.png')
    spec.show_window(factory, .25 * (graph.num_vertices() + 1), weight, pos,
                     file_name + '_window2.png')
    spec.show_window(factory, .75 * (graph.num_vertices() + 1), weight, pos,
                     file_name + '_window3.png')
    show_spec_in_graph(graph, 417, spec_weighted, pos, weight,
                       file_name + '_florida')
Esempio n. 5
0
def compare_spectrograms(factories, x_signal, graph, pos, weight=None,
                         file_name=None, show_ncomps=None):
    palette = sns.cubehelix_palette(256, start=2, rot=0, dark=0.15, light=1)
    cmap = colors.ListedColormap(palette)
    stem_palette = sns.color_palette('Set1', n_colors=2)

    def show_spectrogram(s):
        s /= np.atleast_2d(np.max(s, axis=0))
        s_range = np.max(s) - np.min(s)
        s = utils.smoothstep(s,
                             min_edge=np.min(s) + s_range / 3,
                             max_edge=np.max(s) - s_range / 10)
        if show_ncomps is None:
            spec.plot(s, cmap)
        else:
            spec.plot(s[:show_ncomps, :], cmap)

    def show_argmax_spectrogram_graph(s, vertex_size=20, amax_file_name=None):
        amax = np.argmax(np.abs(s), axis=0)
        n_values = np.unique(amax).size
        assignment = graph.new_vertex_property('double', vals=amax)

        if weight is None:
            edge_pen_width = 1.0
        else:
            edge_pen_width = weight

        palette = sns.color_palette('BuGn', n_colors=n_values)
        cmap = colors.ListedColormap(palette)
        gt_draw.graph_draw(graph, pos=pos, vertex_color=[0, 0, 0, 0.5],
                           vertex_fill_color=assignment, vcmap=cmap,
                           vertex_size=vertex_size, edge_color=[0, 0, 0, 0.7],
                           edge_pen_width=edge_pen_width, output=amax_file_name,
                           output_size=(1200, 1200))

    def show_argmax_spectrogram_1d(s):
        amax = np.argmax(np.abs(s), axis=0)
        _, stemlines, baseline = plt.stem(amax, markerfmt=' ')
        plt.setp(stemlines, 'color', stem_palette[1])
        plt.setp(baseline, 'color','k')
        plt.ylim((0, s.shape[0]))

    if file_name is None:
        plt.figure()
        for i in range(len(factories)):
            spectrogram = factories[i].compute(x_signal)
            plt.subplot(2, 4, i + 1)
            show_spectrogram(spectrogram)
            plt.subplot(2, 4, 5 + i)
            show_argmax_spectrogram_1d(spectrogram)
    else:
        file_name += '_{0}_{1}'
        for i in range(len(factories)):
            spectrogram = factories[i].compute(x_signal)
            plt.figure()
            show_spectrogram(spectrogram)
            plt.savefig(file_name.format(i, 'spec.pdf'), dpi=300)
            plt.figure()
            show_argmax_spectrogram_1d(spectrogram)
            plt.savefig(file_name.format(i, 'spec_amax.pdf'), dpi=300)
            amax_file_name = file_name.format(i, 'spec_amax.png')
            show_argmax_spectrogram_graph(spectrogram,
                                          amax_file_name=amax_file_name)
def plot_learning_rate(output_dir=expanduser('~/output/recommender/learning_rate')):
    with open(join(output_dir, 'results_netflix.json'), 'r') as f:
        data_netflix = json.load(f)
    with open(join(output_dir, 'results_10m.json'), 'r') as f:
        data_10m = json.load(f)
    min_time = 400
    for i, learning_rate in enumerate(sorted(data_netflix.keys(), key=lambda t : float(t))):
        this_data = data_netflix[learning_rate]
        min_time = min(this_data['time'][0], min_time)
    for i, learning_rate in enumerate(sorted(data_netflix.keys(), key=lambda t : float(t))):
        this_data = data_netflix[learning_rate]
        for j in range(len(this_data)):
            this_data['time'][j] -= this_data['time'][0] - min_time
    fig = plt.figure()
    # fig.subplots_adjust(right=0.7)
    fig.subplots_adjust(bottom=0.33)
    fig.subplots_adjust(top=0.99)
    fig.subplots_adjust(right=0.98)

    fig.set_figwidth(3.25653379549)
    fig.set_figheight(1.25)
    ax = {}
    gs = gridspec.GridSpec(1, 2)
    palette = sns.cubehelix_palette(10, start=0, rot=3, hue=1, dark=.3,
                                    light=.7,
                                    reverse=False)
    for j, data in enumerate([data_10m, data_netflix]):
        ax[j] = fig.add_subplot(gs[j])
        # palette = sns.hls_palette(10, l=.4, s=.7)
        for i, learning_rate in enumerate(sorted(data.keys(), key=lambda t : float(t))):
            if float(learning_rate) > .6:
                this_data = data[learning_rate]
                ax[j].plot(np.linspace(0., 20, len(this_data['rmse'])),
                           this_data['rmse'],
                           label='%.2f' % float(learning_rate),
                           color=palette[i],
                           zorder=int(100 * float(learning_rate)))
                ax[j].set_xscale('log')
        sns.despine(fig, ax)

        ax[j].spines['left'].set_color((.6, .6, .6))
        ax[j].spines['bottom'].set_color((.6, .6, .6))
        ax[j].xaxis.set_tick_params(color=(.6, .6, .6), which='both')
        ax[j].yaxis.set_tick_params(color=(.6, .6, .6), which='both')
        ax[j].tick_params(axis='y', labelsize=6)

    ax[0].set_ylabel('RMSE on test set')
    ax[0].set_xlabel('Epoch', ha='left', va='top')
    ax[0].xaxis.set_label_coords(-.18, -0.055)

    ax[0].set_xlim([.1, 20])
    ax[0].set_xticks([1, 10, 20])
    ax[0].set_xticklabels(['1', '10', '20'])
    ax[1].set_xlim([.1, 20])
    ax[1].set_xticks([.1, 1, 10, 20])
    ax[1].set_xticklabels(['.1', '1', '10', '20'])

    ax[0].annotate('MovieLens 10M', xy=(.95, .8), ha='right', xycoords='axes fraction')
    ax[1].annotate('Netflix', xy=(.95, .8), ha='right', xycoords='axes fraction')


    ax[0].set_ylim([0.795, 0.863])
    ax[1].set_ylim([0.93, 0.983])
    ax[0].legend(ncol=4, loc='upper left', bbox_to_anchor=(0., -.13), fontsize=6, numpoints=1, columnspacing=.3, frameon=False)
    ax[0].annotate('Learning rate $\\beta$', xy=(1.6, -.38), xycoords='axes fraction')
    ltext  = ax[0].get_legend().get_texts()
    plt.setp(ltext, fontsize=7)
    plt.savefig(expanduser('~/output/icml/learning_rate.pdf'))
T_0 = 0.00997
"""Plotting"""
# Font
rc('text', usetex=True)
matplotlib.rcParams['font.size'] = '15.0'
matplotlib.rcParams['xtick.labelsize'] = '15'
matplotlib.rcParams['ytick.labelsize'] = '15'
matplotlib.rcParams['legend.fontsize'] = '15'
matplotlib.rcParams['axes.linewidth'] = 0.6

# Colors
main_red = sns.color_palette("RdBu_r", 15)[12]
main_blue = sns.color_palette("RdBu_r", 15)[1]
soft_red = sns.color_palette("RdBu_r", 15)[10]
soft_blue = sns.color_palette("RdBu_r", 15)[4]
violet = sns.cubehelix_palette(8)[4]
green = sns.cubehelix_palette(8, start=.5, rot=-.75)[3]

fig, axes = plt.subplots(16, 7, figsize=(14., 19.5))

# Sort neurons, put neurons with max_val > 0.2 and max_val <0.3 in a separate group

smallR = []
mediumR = []
highR = []
veryhighR = []
setup = 'full_shuffling'
for neuron_index, neuron in enumerate(validNeurons):
    ANALYSIS_DIR, analysis_num_str, R_tot, T_D, T, R, R_CI_lo, R_CI_hi = plots.load_analysis_results(
        recorded_system,
        rec_length,
rows = data.shape
t = range(0, rows[0])
errs = np.zeros((rows[0], 2))
t2 = np.linspace(0, 15)
for i in xrange(0, rows[0]):
    errs[i, 0] = np.sqrt(
        np.dot(data[i, 0:3] - data[i, 3:6], data[i, 0:3] - data[i, 3:6])) * 100
    errs[i, 1] = np.sqrt(
        np.dot(data[i, 0:3] - data[i, 6:9], data[i, 0:3] - data[i, 6:9])) * 100

errs[rows[0] - 1, 0] = 14
errs[rows[0] - 1, 1] = 14
colors = sns.cubehelix_palette(8,
                               start=2.5,
                               rot=0,
                               dark=0.3,
                               light=1.1,
                               reverse=False,
                               as_cmap=True)
colors.set_under(alpha=0.0)
#print np.mean(errs[:, 0]), np.mean(errs[:, 1])

#ss.plot(series=[(t, errs[:, 1]),
#                (t, errs[:, 0])],
#                series_colors=['red', 'blue'], y_grid=True,
#series_labels=['No SLAM', 'With SLAM'], plot_xlabel="Frame", plot_ylabel="Meters", savefile="./kinematic_error.pdf", savefile_size=(4, 3));
#plt.yscale('log')
boxprops = dict(linestyle='-', linewidth=1, color=(0.3, 0.3, 0.3))
flierprops = dict(marker='.',
                  markerfacecolor='black',
                  markersize=1,
Esempio n. 9
0
def plot_benchs():
    output_dir = join(trace_dir, 'benches')

    fig = plt.figure()

    fig.subplots_adjust(right=.9)
    fig.subplots_adjust(top=.905)
    fig.subplots_adjust(bottom=.12)
    fig.subplots_adjust(left=.06)
    fig.set_figheight(fig.get_figheight() * 0.66)
    gs = gridspec.GridSpec(1, 3, width_ratios=[1, 1, 1.5])

    ylims = {
        '100k': [.90, .96],
        '1m': [.864, .915],
        '10m': [.80, .868],
        'netflix': [.93, .99]
    }
    xlims = {
        '100k': [0.0001, 10],
        '1m': [0.1, 20],
        '10m': [1, 400],
        'netflix': [30, 3000]
    }

    names = {
        'dl_partial': 'Proposed \n(partial projection)',
        'dl': 'Proposed \n(full projection)',
        'cd': 'Coordinate descent'
    }
    zorder = {'cd': 10, 'dl': 1, 'dl_partial': 5}
    for i, version in enumerate(['1m', '10m', 'netflix']):
        try:
            with open(join(output_dir, 'results_%s.json' % version), 'r') as f:
                results = json.load(f)
        except IOError:
            continue

        ax_time = fig.add_subplot(gs[0, i])
        ax_time.grid()
        sns.despine(fig, ax_time)

        ax_time.spines['left'].set_color((.6, .6, .6))
        ax_time.spines['bottom'].set_color((.6, .6, .6))
        ax_time.xaxis.set_tick_params(color=(.6, .6, .6), which='both')
        ax_time.yaxis.set_tick_params(color=(.6, .6, .6), which='both')

        for tick in ax_time.xaxis.get_major_ticks():
            tick.label.set_fontsize(7)
            tick.label.set_color('black')
        for tick in ax_time.yaxis.get_major_ticks():
            tick.label.set_fontsize(7)
            tick.label.set_color('black')

        if i == 0:
            ax_time.set_ylabel('RMSE on test set')
        if i == 2:
            ax_time.set_xlabel('CPU time')
            ax_time.xaxis.set_label_coords(1.14, -0.06)

        ax_time.grid()
        palette = sns.cubehelix_palette(3,
                                        start=0,
                                        rot=.5,
                                        hue=1,
                                        dark=.3,
                                        light=.7,
                                        reverse=False)
        color = {'dl_partial': palette[2], 'dl': palette[1], 'cd': palette[0]}
        for idx in sorted(OrderedDict(results).keys()):
            this_result = results[idx]
            ax_time.plot(this_result['timings'],
                         this_result['rmse'],
                         label=names[idx],
                         color=color[idx],
                         linewidth=2,
                         linestyle='-' if idx != 'cd' else '--',
                         zorder=zorder[idx])
        if version == 'netflix':
            ax_time.legend(loc='upper left',
                           bbox_to_anchor=(.65, 1.1),
                           numpoints=1,
                           frameon=False)
        ax_time.set_xscale('log')
        ax_time.set_ylim(ylims[version])
        ax_time.set_xlim(xlims[version])
        if version == '1m':
            ax_time.set_xticks([.1, 1, 10])
            ax_time.set_xticklabels(['0.1 s', '1 s', '10 s'])
        elif version == '10m':
            ax_time.set_xticks([1, 10, 100])
            ax_time.set_xticklabels(['1 s', '10 s', '100 s'])
        else:
            ax_time.set_xticks([100, 1000])
            ax_time.set_xticklabels(['100 s', '1000 s'])
        ax_time.annotate(
            'MovieLens %s' %
            version.upper() if version != 'netflix' else 'Netflix (140M)',
            xy=(.5 if version != 'netflix' else .4, 1),
            xycoords='axes fraction',
            ha='center',
            va='bottom')
    plt.savefig(join(trace_dir, 'bench.pdf'))
Esempio n. 10
0
def display_explained_variance_density(output_dir):
    dir_list = [join(output_dir, f) for f in os.listdir(output_dir) if
                os.path.isdir(join(output_dir, f))]

    fig = plt.figure(figsize=(fig_width * 0.73, fig_height))
    gs = gridspec.GridSpec(1, 2, width_ratios=[1, 1])
    fig.subplots_adjust(bottom=0.29)
    fig.subplots_adjust(left=0.075)
    fig.subplots_adjust(right=.92)

    results = []
    analyses = []
    ref_time = 1000000
    for dir_name in dir_list:
        try:
            analyses.append(
                json.load(open(join(dir_name, 'analysis.json'), 'r')))
            results.append(
                json.load(open(join(dir_name, 'results.json'), 'r')))
            if results[-1]['reduction'] == 12:
                timings = np.array(results[-1]['timings'])
                diff = timings[1:] - timings[:1]
                ref_time = min(ref_time, np.min(diff))
        except IOError:
            pass
    print(ref_time)
    h_reductions = []
    ax = {}
    ylim = {1e-2: [2.455e8, 2.525e8], 1e-3: [2.3e8, 2.47e8],
            1e-4: [2.16e8, 2.42e8]}
    for i, alpha in enumerate([1e-3, 1e-4]):
        ax[alpha] = fig.add_subplot(gs[:, i])
        if i == 0:
            ax[alpha].set_ylabel('Objective value on test set')
        ax[alpha].annotate('$\\lambda  = 10^{%.0f}$' % log(alpha, 10),
                           xy=(.65, .85),
                           fontsize=8,
                           xycoords='axes fraction')
        ax[alpha].set_xlim([.05, 200])
        ax[alpha].set_ylim(ylim[alpha])

        for tick in ax[alpha].xaxis.get_major_ticks():
            tick.label.set_fontsize(7)
        ax[alpha].set_xscale('log')

        ax[alpha].set_xticks([.1, 1, 10, 100])
        ax[alpha].set_xticklabels(['.1 h', '1 h', '10 h', '100 h'])

        sns.despine(fig=fig, ax=ax[alpha])

        ax[alpha].spines['left'].set_color((.6, .6, .6))
        ax[alpha].spines['bottom'].set_color((.6, .6, .6))
        ax[alpha].xaxis.set_tick_params(color=(.6, .6, .6), which='both')
        ax[alpha].yaxis.set_tick_params(color=(.6, .6, .6), which='both')
        for tick in ax[alpha].xaxis.get_major_ticks():
            tick.label.set_color('black')
        for tick in ax[alpha].yaxis.get_major_ticks():
            tick.label.set_fontsize(6)

            tick.label.set_color('black')
        t = ax[alpha].yaxis.get_offset_text()
        t.set_size(5)
    ax[1e-4].set_xlabel('CPU\ntime', ha='right')
    ax[1e-4].xaxis.set_label_coords(1.15, -0.05)

    colormap = sns.cubehelix_palette(4, start=0, rot=0., hue=1, dark=.3,
                                     light=.7,
                                     reverse=False)
    other_colormap = sns.cubehelix_palette(4, start=0, rot=.5, hue=1, dark=.3,
                                           light=.7,
                                           reverse=False)
    colormap[0] = other_colormap[0]
    colormap_dict = {reduction: color for reduction, color in
                     zip([1, 4, 8, 12],
                         colormap)}

    x_bar = []
    y_bar_objective = []
    y_bar_density = []
    hue_bar = []

    for result, analysis in zip(results, analyses):
        if result['alpha'] != 1e-2 and result['reduction'] != 2:
            print("%s %s" % (result['alpha'], result['reduction']))
            timings = (np.array(analysis['records']) + 1) / int(
                result['reduction']) * 12 * ref_time / 3600
            # timings = np.array(result['timings'])[np.array(analysis['records']) + 1] / 3600
            s, = ax[result[
                'alpha']].plot(
                timings,
                np.array(analysis['objectives']) / 4,
                color=colormap_dict[int(result['reduction'])],
                linewidth=2,
                linestyle='--' if result[
                                      'reduction'] == 1 else '-',
                zorder=result['reduction'] if result[
                                                  'reduction'] != 1 else 100)
            if result['alpha'] == 1e-3:
                h_reductions.append(
                    (s, '%.0f' % result['reduction']))

    handles, labels = list(zip(*h_reductions[::-1]))
    argsort = sorted(range(len(labels)), key=lambda t: int(labels[t]))
    handles = [handles[i] for i in argsort]
    labels = [labels[i] for i in argsort]

    offset = .3
    yoffset = -.05
    legend_vanilla = mlegend.Legend(ax[1e-3], handles[:1], ['No reduction'],
                                    loc='lower left',
                                    ncol=5,
                                    numpoints=1,
                                    handlelength=2,
                                    markerscale=1.4,
                                    bbox_to_anchor=(
                                        0.3 + offset, -.39 + yoffset),
                                    fontsize=8,
                                    frameon=False
                                    )

    legend_ratio = mlegend.Legend(ax[1e-3], handles[1:], labels[1:],
                                  loc='lower left',
                                  ncol=5,
                                  markerscale=1.4,
                                  handlelength=2,
                                  fontsize=8,
                                  bbox_to_anchor=(
                                      0.3 + offset, -.54 + yoffset),
                                  frameon=False
                                  )
    ax[1e-3].annotate('Original online algorithm',
                      xy=(0.28 + offset, -.27 + yoffset),
                      xycoords='axes fraction',
                      horizontalalignment='right', verticalalignment='bottom',
                      fontsize=8)
    ax[1e-3].annotate('Proposed reduction factor $r$',
                      xy=(0.28 + offset, -.42 + yoffset),
                      xycoords='axes fraction',
                      horizontalalignment='right', verticalalignment='bottom',
                      fontsize=8)
    ax[1e-3].add_artist(legend_ratio)
    ax[1e-3].add_artist(legend_vanilla)

    ax[1e-3].annotate('(a) Convergence speed', xy=(0.7, 1.02), ha='center',
                      fontsize=9, va='bottom', xycoords='axes fraction')

    fig.savefig(join(output_dir, 'hcp_bench.pdf'))

    for result, analysis in zip(results, analyses):
        if result['alpha'] != 1e-2 and result['reduction'] != 2:
            x_bar.append(result['alpha'])
            y_bar_objective.append(analysis['objectives'][-1])
            y_bar_density.append(analysis['densities'][-1])
            hue_bar.append(result['reduction'])
    ref_objective = {}
    for objective, alpha, reduction in zip(y_bar_objective, x_bar, hue_bar):
        if reduction == 1:
            ref_objective[alpha] = objective

    for i, (objective, alpha) in enumerate(zip(y_bar_objective, x_bar)):
        y_bar_objective[i] /= ref_objective[alpha]
        y_bar_objective[i] -= 1

    ####################### Final objective
    fig = plt.figure(figsize=(fig_width * 0.27, fig_height))
    fig.subplots_adjust(bottom=0.29)
    fig.subplots_adjust(left=0.05)
    fig.subplots_adjust(right=1.2)
    fig.subplots_adjust(top=0.85)
    gs = gridspec.GridSpec(2, 1, width_ratios=[1, 1], height_ratios=[1.2, 0.8])
    ax_bar_objective = fig.add_subplot(gs[0])
    ax_bar_objective.set_ylim(-0.007, 0.007)
    ax_bar_objective.set_yticks([-0.005, 0, 0.005])
    ax_bar_objective.set_yticklabels(['-0.5\%', '0\%', '0.5\%'])
    ax_bar_objective.tick_params(axis='y', labelsize=6)

    sns.despine(fig=fig, ax=ax_bar_objective, left=True, right=False)

    sns.barplot(x=x_bar, y=y_bar_objective, hue=hue_bar, ax=ax_bar_objective,
                order=[1e-3, 1e-4],
                palette=colormap)
    plt.setp(ax_bar_objective.patches, linewidth=0.1)
    ax_bar_objective.legend_ = None
    ax_bar_objective.get_xaxis().set_visible(False)
    ax_bar_objective.set_xlim([-.5, 1.6])
    ax_bar_objective.annotate('Final\nobjective\ndeviation\n(relative)',
                              xy=(1.28, 0.45), fontsize=7, va='center',
                              xycoords='axes fraction')
    ax_bar_objective.annotate('(Less is better)', xy=(.06, 0.1), fontsize=7,
                              va='center', xycoords='axes fraction')
    ax_bar_objective.yaxis.set_label_position('right')

    ################################## Density
    x_bar = []
    y_bar_density = []
    hue_bar = []
    for result, analysis in zip(results, analyses):
        if result['alpha'] != 1e-2 and result['reduction'] != 2:
            x_bar.append(result['alpha'])
            y_bar_density.append(analysis['densities'][-1])
            hue_bar.append(result['reduction'])

    ax_bar_density = fig.add_subplot(gs[1])
    ax_bar_density.set_yscale('log')
    ax_bar_density.set_ylim(100, 1000)
    ax_bar_density.set_yticks([100, 1000])
    ax_bar_density.set_yticklabels(['100', '1000'])
    ax_bar_density.tick_params(axis='y', labelsize=6)

    sns.barplot(x=x_bar, y=y_bar_density, hue=hue_bar, ax=ax_bar_density,
                order=[1e-3, 1e-4],
                palette=colormap)
    ax_bar_density.set_xticklabels(['$10^{-2}$', '$10^{-3}$', '$10^{-4}$'])
    sns.despine(fig=fig, ax=ax_bar_density, left=True, right=False)
    # ax_bar_density.get_xaxis().set_ticks([])
    ax_bar_density.set_xlim([-.5, 1.6])
    ax_bar_density.set_xlabel('Regularization $\\lambda$')
    ax_bar_density.annotate('$\\frac{\\ell_1}{\\ell_2}(\\mathbf D)$',
                            xy=(1.26, 0.45),
                            fontsize=7, va='center', xycoords='axes fraction')
    ax_bar_density.yaxis.set_label_position('right')

    plt.setp(ax_bar_density.patches, linewidth=0.1)
    ax_bar_density.legend_ = None

    for ax in [ax_bar_density, ax_bar_objective]:
        ax.spines['right'].set_color((.6, .6, .6))
        ax.spines['bottom'].set_color((.6, .6, .6))
        ax.xaxis.set_tick_params(color=(.6, .6, .6), which='both')
        ax.yaxis.set_tick_params(color=(.6, .6, .6), which='both')

    for tic in ax_bar_density.xaxis.get_major_ticks():
        tic.tick1On = tic.tick2On = False
    ax_bar_objective.spines['bottom'].set_position(('data', 0))
    ax_bar_objective.spines['bottom'].set_linewidth(.3)
    ax_bar_objective.annotate('(b) Decomposition quality', xy=(0.7, 1.21),
                              ha='center', va='bottom', fontsize=9,
                              xycoords='axes fraction')

    fig.savefig(expanduser(join(output_dir, 'bar_plot.pdf')))
            suppress_ticks=False)

m.drawmapboundary()

lat = Dataset(if_temp)['lat'][:]
lon = Dataset(if_temp)['lon'][:]

#rearrange matrix for plot
lon = shift_lon(lon)
arr_year = shift_lon(arr_year)

arr_year[np.isnan(arr_year)] = 150

x, y = np.meshgrid(lon, lat)

my_cmap = ListedColormap(sns.cubehelix_palette(24).as_hex()[0:24:3])
cs = m.pcolormesh(lon,
                  lat,
                  np.squeeze(arr_year),
                  cmap=my_cmap,
                  vmin=0,
                  vmax=80)  #,legend_ticks)

cbar = m.colorbar(cs,
                  location='right',
                  pad="5%",
                  ticks=[10, 20, 30, 40, 50, 60, 70])
cbar.set_label('Year', fontsize=14, rotation=270, labelpad=18)
cbar.ax.set_yticklabels([10, 20, 30, 40, 50, 60, 70], size=14)

m.readshapefile(
Esempio n. 12
0
G = nx.cubical_graph()
G = nx.relabel_nodes(G, {
    0: "O",
    1: "X",
    2: "XZ",
    3: "Z",
    4: "Y",
    5: "YZ",
    6: "XYZ",
    7: "XY"
})
pos = nx.spring_layout(G)

# Sequence of letters
sequence_of_letters = "".join(['X', 'Y', 'Z', 'Y', 'Y', 'Z'])
idx_colors = sns.cubehelix_palette(5, start=.5, rot=-.75)[::-1]
idx_weights = [3, 2, 1]

# Build plot
fig, ax = plt.subplots(figsize=(6, 4))


def update(num):
    ax.clear()
    i = num // 3
    j = num % 3 + 1
    triad = sequence_of_letters[i:i + 3]
    path = ["O"] + ["".join(sorted(set(triad[:k + 1]))) for k in range(j)]

    # Background nodes
    nx.draw_networkx_edges(G, pos=pos, ax=ax, edge_color="gray")
sgd_data = sgd_data.iloc[[idxmin]]

var_data = data.query("method != 'sgd'")
data = pd.concat([sgd_data, var_data], axis=0)
data.reset_index(inplace=True)

data.to_csv(join(analysis_dir, 'data.csv'))
dump(data, join(analysis_dir, 'data.pkl'))
# Plot
fig, ax = plt.subplots(
    1,
    1,
)
fig.subplots_adjust(left=0.15, right=0.97, top=0.97, bottom=0.2)

colormap = sns.cubehelix_palette(6, rot=0.3, light=0.85, reverse=False)
reductions = [1, 4, 6, 8, 12, 24]
ref_colormap = sns.cubehelix_palette(6,
                                     start=2,
                                     rot=0.2,
                                     light=0.7,
                                     reverse=False)
sgd_colormap = sns.cubehelix_palette(6,
                                     start=1,
                                     rot=0.2,
                                     light=0.7,
                                     reverse=False)
color_dict = {
    reduction: color
    for reduction, color in zip(reductions, colormap)
}
Esempio n. 14
0
def plot_learning_rate():
    output_dir = join(trace_dir, 'learning_rate')
    fig = plt.figure()
    fig.subplots_adjust(bottom=0.33)
    fig.subplots_adjust(top=0.99)
    fig.subplots_adjust(right=0.98)

    fig.set_figwidth(3.25653379549)
    fig.set_figheight(1.25)
    ax = {}
    gs = gridspec.GridSpec(1, 2)
    palette = sns.cubehelix_palette(10, start=0, rot=3, hue=1, dark=.3,
                                    light=.7,
                                    reverse=False)

    for j, version in enumerate(['10m', 'netflix']):
        with open(join(output_dir, 'results_%s.json' % version), 'r') as f:
            data = json.load(f)
        ax[j] = fig.add_subplot(gs[j])
        learning_rates = sorted(data, key=lambda t: float(t))
        for i, learning_rate in enumerate(learning_rates):
            this_data = data[str(learning_rate)]
            n_epochs = _get_hyperparams()['dl_partial'][version]['n_epochs']
            ax[j].plot(np.linspace(0, n_epochs, len(this_data['rmse'])),
                       this_data['rmse'],
                       label='%.2f' % float(learning_rate),
                       color=palette[i],
                       zorder=int(100 * float(learning_rate)))
            ax[j].set_xscale('log')
        sns.despine(fig, ax)

        ax[j].spines['left'].set_color((.6, .6, .6))
        ax[j].spines['bottom'].set_color((.6, .6, .6))
        ax[j].xaxis.set_tick_params(color=(.6, .6, .6), which='both')
        ax[j].yaxis.set_tick_params(color=(.6, .6, .6), which='both')
        ax[j].tick_params(axis='y', labelsize=6)

    ax[0].set_ylabel('RMSE on test set')
    ax[0].set_xlabel('Epoch', ha='left', va='top')
    ax[0].xaxis.set_label_coords(-.18, -0.055)

    ax[0].set_xlim([.1, 40])
    ax[0].set_xticks([1, 10, 40])
    ax[0].set_xticklabels(['1', '10', '40'])
    ax[1].set_xlim([.1, 25])
    ax[1].set_xticks([.1, 1, 10, 20])
    ax[1].set_xticklabels(['.1', '1', '10', '20'])

    ax[0].annotate('MovieLens 10M', xy=(.95, .9), ha='right',
                   xycoords='axes fraction', zorder=100)
    ax[1].annotate('Netflix', xy=(.95, .9), ha='right',
                   xycoords='axes fraction', zorder=100)

    ax[0].set_ylim([0.795, 0.877])
    ax[1].set_ylim([0.93, .999])
    ax[0].legend(ncol=4, loc='upper left', bbox_to_anchor=(-0.09, -.13),
                 fontsize=7, numpoints=1, columnspacing=.3, frameon=False)
    ax[0].annotate('Learning rate $\\beta$', xy=(1.6, -.38),
                   xycoords='axes fraction')
    ltext = ax[0].get_legend().get_texts()
    plt.setp(ltext, fontsize=7)

    plt.savefig(join(trace_dir, 'learning_rate.pdf'))
Esempio n. 15
0
def display_explained_variance_epoch(output_dir):
    dir_list = [join(output_dir, f) for f in os.listdir(output_dir) if
                os.path.isdir(join(output_dir, f))]

    fig = plt.figure()
    gs = gridspec.GridSpec(1, 1, width_ratios=[1])
    fig.set_figwidth(3.25653379549)
    fig.set_figheight(1.3)
    fig.subplots_adjust(bottom=0.105)
    fig.subplots_adjust(top=0.9)
    fig.subplots_adjust(left=0.12)
    fig.subplots_adjust(right=.95)

    results = []
    analyses = []
    ref_time = 1000000
    for dir_name in dir_list:
        try:
            analyses.append(
                json.load(open(join(dir_name, 'analysis.json'), 'r')))
            results.append(
                json.load(open(join(dir_name, 'results.json'), 'r')))
            if results[-1]['reduction'] == 12:
                timings = np.array(results[-1]['timings'])
                diff = timings[1:] - timings[:1]
                ref_time = min(ref_time, np.min(diff))
        except IOError:
            pass
    h_reductions = []
    ax = {}
    ylim = {1e-2: [2.475e8, 2.522e8], 1e-3: [2.3e8, 2.335e8],
            1e-4: [2.16e8, 2.24e8]}
    for i, alpha in enumerate([1e-4]):
        ax[alpha] = fig.add_subplot(gs[:, i])
        if i == 0:
            ax[alpha].set_ylabel('Objective value on test set')
        ax[alpha].set_xlim([50, 4000])

        for tick in ax[alpha].xaxis.get_major_ticks():
            tick.label.set_fontsize(7)
        ax[alpha].set_xscale('log')

        ax[alpha].set_xticks([100, 1000, 1947, 4000])
        ax[alpha].set_xticklabels(['100', '1000', 'Epoch', '4000'])


        ax[alpha].set_ylim(ylim[alpha])
        sns.despine(fig=fig, ax=ax[alpha])

        ax[alpha].spines['left'].set_color((.6, .6, .6))
        ax[alpha].spines['bottom'].set_color((.6, .6, .6))
        ax[alpha].xaxis.set_tick_params(color=(.6, .6, .6), which='both')
        ax[alpha].yaxis.set_tick_params(color=(.6, .6, .6), which='both')
        for tick in ax[alpha].xaxis.get_major_ticks():
            tick.label.set_color('black')
        for tick in ax[alpha].yaxis.get_major_ticks():
            tick.label.set_fontsize(7)

            tick.label.set_color('black')
        t = ax[alpha].yaxis.get_offset_text()
        t.set_size(6)
    ax[1e-4].set_xlabel('Records')
    ax[1e-4].xaxis.set_label_coords(-0.04, -0.047)

    colormap = sns.cubehelix_palette(4, start=0, rot=0., hue=1, dark=.3,
                                     light=.7,
                                     reverse=False)

    other_colormap = sns.cubehelix_palette(4, start=0, rot=.5, hue=1, dark=.3,
                                           light=.7,
                                           reverse=False)
    colormap[0] = other_colormap[0]
    colormap_dict = {reduction: color for reduction, color in
                     zip([1, 4, 8, 12],
                         colormap)}
    for result, analysis in zip(results, analyses):
        if result['alpha'] in [1e-4] and result['reduction'] in [1, 4, 8, 12]:

            print("%s %s" % (result['alpha'], result['reduction']))
            s, = ax[result[
                'alpha']].plot(np.array(analysis['records']),
                               np.array(analysis['objectives']) / 4,
                               color=colormap_dict[result['reduction']],
                               linewidth=1.5,
                               linestyle='--' if result[
                                                     'reduction'] == 1 else '-',
                               zorder=result['reduction'] if result[
                                                                    'reduction'] > 1 else 100)
            h_reductions.append(
                (s, result['reduction']))

    handles, labels = list(zip(*h_reductions[::-1]))
    argsort = sorted(range(len(labels)), key=lambda t: int(labels[t]))
    handles = [handles[i] for i in argsort]
    labels = [('$r=%i$' % labels[i]) for i in argsort]
    labels[0] = 'No reduction\n(original alg.)'

    ax[1e-4].annotate('$\\lambda  = 10^{%.0f}$' % log(alpha, 10),
                       xy=(0.07, 0.07),
                       ha='left',
                       va='bottom',
                       fontsize=8,
                       xycoords='axes fraction')
    legend_ratio = mlegend.Legend(ax[1e-4], handles[0:], labels[0:],
                                  loc='upper right',
                                  ncol=1,
                                  numpoints=1,
                                  handlelength=2,
                                  frameon=False,
                                  bbox_to_anchor=(1, 1.15)
                                  )
    ax[1e-4].add_artist(legend_ratio)

    fig.savefig(join(output_dir, 'hcp_epoch.pdf'))
Esempio n. 16
0
def plot_benchs():
    output_dir = join(trace_dir, 'benches')

    fig = plt.figure()

    fig.subplots_adjust(right=.9)
    fig.subplots_adjust(top=.905)
    fig.subplots_adjust(bottom=.12)
    fig.subplots_adjust(left=.06)
    fig.set_figheight(fig.get_figheight() * 0.66)
    gs = gridspec.GridSpec(1, 3, width_ratios=[1, 1, 1.5])

    ylims = {'100k': [.90, .96], '1m': [.864, .915], '10m': [.80, .868],
             'netflix': [.93, .99]}
    xlims = {'100k': [0.0001, 10], '1m': [0.1, 20], '10m': [1, 400],
             'netflix': [30, 3000]}

    names = {'dl_partial': 'Proposed \n(partial projection)',
             'dl': 'Proposed \n(full projection)',
             'cd': 'Coordinate descent'}
    zorder = {'cd': 10,
              'dl': 1,
              'dl_partial': 5}
    for i, version in enumerate(['1m', '10m', 'netflix']):
        try:
            with open(join(output_dir, 'results_%s.json' % version), 'r') as f:
                results = json.load(f)
        except IOError:
            continue

        ax_time = fig.add_subplot(gs[0, i])
        ax_time.grid()
        sns.despine(fig, ax_time)

        ax_time.spines['left'].set_color((.6, .6, .6))
        ax_time.spines['bottom'].set_color((.6, .6, .6))
        ax_time.xaxis.set_tick_params(color=(.6, .6, .6), which='both')
        ax_time.yaxis.set_tick_params(color=(.6, .6, .6), which='both')

        for tick in ax_time.xaxis.get_major_ticks():
            tick.label.set_fontsize(7)
            tick.label.set_color('black')
        for tick in ax_time.yaxis.get_major_ticks():
            tick.label.set_fontsize(7)
            tick.label.set_color('black')

        if i == 0:
            ax_time.set_ylabel('RMSE on test set')
        if i == 2:
            ax_time.set_xlabel('CPU time')
            ax_time.xaxis.set_label_coords(1.14, -0.06)

        ax_time.grid()
        palette = sns.cubehelix_palette(3, start=0, rot=.5, hue=1, dark=.3,
                                        light=.7,
                                        reverse=False)
        color = {'dl_partial': palette[2], 'dl': palette[1], 'cd': palette[0]}
        for idx in sorted(OrderedDict(results).keys()):
            this_result = results[idx]
            ax_time.plot(this_result['timings'], this_result['rmse'],
                         label=names[idx], color=color[idx],
                         linewidth=2,
                         linestyle='-' if idx != 'cd' else '--',
                         zorder=zorder[idx])
        if version == 'netflix':
            ax_time.legend(loc='upper left', bbox_to_anchor=(.65, 1.1),
                           numpoints=1,
                           frameon=False)
        ax_time.set_xscale('log')
        ax_time.set_ylim(ylims[version])
        ax_time.set_xlim(xlims[version])
        if version == '1m':
            ax_time.set_xticks([.1, 1, 10])
            ax_time.set_xticklabels(['0.1 s', '1 s', '10 s'])
        elif version == '10m':
            ax_time.set_xticks([1, 10, 100])
            ax_time.set_xticklabels(['1 s', '10 s', '100 s'])
        else:
            ax_time.set_xticks([100, 1000])
            ax_time.set_xticklabels(['100 s', '1000 s'])
        ax_time.annotate(
            'MovieLens %s' % version.upper() if version != 'netflix' else 'Netflix (140M)',
            xy=(.5 if version != 'netflix' else .4, 1),
            xycoords='axes fraction', ha='center', va='bottom')
    plt.savefig(join(trace_dir, 'bench.pdf'))
Esempio n. 17
0
def plot_learning_rate():
    output_dir = join(trace_dir, 'learning_rate')
    fig = plt.figure()
    fig.subplots_adjust(bottom=0.33)
    fig.subplots_adjust(top=0.99)
    fig.subplots_adjust(right=0.98)

    fig.set_figwidth(3.25653379549)
    fig.set_figheight(1.25)
    ax = {}
    gs = gridspec.GridSpec(1, 2)
    palette = sns.cubehelix_palette(10,
                                    start=0,
                                    rot=3,
                                    hue=1,
                                    dark=.3,
                                    light=.7,
                                    reverse=False)

    for j, version in enumerate(['10m', 'netflix']):
        with open(join(output_dir, 'results_%s.json' % version), 'r') as f:
            data = json.load(f)
        ax[j] = fig.add_subplot(gs[j])
        learning_rates = sorted(data, key=lambda t: float(t))
        for i, learning_rate in enumerate(learning_rates):
            this_data = data[str(learning_rate)]
            n_epochs = _get_hyperparams()['dl_partial'][version]['n_epochs']
            ax[j].plot(np.linspace(0, n_epochs, len(this_data['rmse'])),
                       this_data['rmse'],
                       label='%.2f' % float(learning_rate),
                       color=palette[i],
                       zorder=int(100 * float(learning_rate)))
            ax[j].set_xscale('log')
        sns.despine(fig, ax)

        ax[j].spines['left'].set_color((.6, .6, .6))
        ax[j].spines['bottom'].set_color((.6, .6, .6))
        ax[j].xaxis.set_tick_params(color=(.6, .6, .6), which='both')
        ax[j].yaxis.set_tick_params(color=(.6, .6, .6), which='both')
        ax[j].tick_params(axis='y', labelsize=6)

    ax[0].set_ylabel('RMSE on test set')
    ax[0].set_xlabel('Epoch', ha='left', va='top')
    ax[0].xaxis.set_label_coords(-.18, -0.055)

    ax[0].set_xlim([.1, 40])
    ax[0].set_xticks([1, 10, 40])
    ax[0].set_xticklabels(['1', '10', '40'])
    ax[1].set_xlim([.1, 25])
    ax[1].set_xticks([.1, 1, 10, 20])
    ax[1].set_xticklabels(['.1', '1', '10', '20'])

    ax[0].annotate('MovieLens 10M',
                   xy=(.95, .9),
                   ha='right',
                   xycoords='axes fraction',
                   zorder=100)
    ax[1].annotate('Netflix',
                   xy=(.95, .9),
                   ha='right',
                   xycoords='axes fraction',
                   zorder=100)

    ax[0].set_ylim([0.795, 0.877])
    ax[1].set_ylim([0.93, .999])
    ax[0].legend(ncol=4,
                 loc='upper left',
                 bbox_to_anchor=(-0.09, -.13),
                 fontsize=7,
                 numpoints=1,
                 columnspacing=.3,
                 frameon=False)
    ax[0].annotate('Learning rate $\\beta$',
                   xy=(1.6, -.38),
                   xycoords='axes fraction')
    ltext = ax[0].get_legend().get_texts()
    plt.setp(ltext, fontsize=7)

    plt.savefig(join(trace_dir, 'learning_rate.pdf'))