def plot_convergence(title, init_global, random_generator, samples=128):
    resolutions = [64, 128, 256, 512]

    variables = ["rho", "ux", "uy", "uz", "p"]

    errors = collections.defaultdict(lambda: np.zeros(len(resolutions)))

    for n, resolution in enumerate(resolutions):
        print()
        print(f"resolution: {resolution}")
        data_local = collections.defaultdict(lambda: np.zeros(
            (resolution, resolution, resolution)))
        data_coarse = collections.defaultdict(lambda: np.zeros(
            (resolution // 2, resolution // 2, resolution // 2)))

        for sample in range(samples):
            sys.stdout.write(f'Sample: {sample}\r')
            sys.stdout.flush()
            random_generator()
            init_global(data_local['rho'], data_local['ux'], data_local['uz'],
                        data_local['uz'], data_local['p'], resolution,
                        resolution, resolution, 0, 0, 0, 1, 1, 1)

            init_global(data_coarse['rho'], data_coarse['ux'],
                        data_coarse['uz'], data_coarse['uz'], data_coarse['p'],
                        resolution // 2, resolution // 2, resolution // 2, 0,
                        0, 0, 1, 1, 1)

            for variable in data_local.keys():
                upscaled_coarse = np.repeat(
                    np.repeat(np.repeat(data_coarse[variable], 2, 0), 2, 1), 2,
                    2)
                errors[variable][n] += np.sum(
                    abs(data_local[variable] -
                        upscaled_coarse)) / resolution**3

        errors[variable][n] /= samples

    for variable in variables:
        plt.loglog(resolutions, errors[variable], '-o', basex=2, basey=2)
        plt.xticks(resolutions, [f'${r}^3$' for r in resolutions])

        plt.xlabel('Resolution ($N^3$)')
        plt.ylabel('Error ($\\mathbb{E}(||u^N-u^{N/2}||)$)')
        plt.title(f'{title}\nVariable: {variable}, samples: {samples}')
        plot_info.savePlot(f'initial_data_convergence_{title}_{variable}')
def plot_convergence_single_sample(basename,
                                   title,
                                   variable,
                                   starting_resolution,
                                   zoom=True,
                                   compute_rate=False):
    resolution = starting_resolution

    resolutions = []
    errors = []

    while resolution_exists(basename, resolution) and resolution_exists(
            basename, 2 * resolution):
        print(resolution)
        error = 0.0
        for plane in range(2 * resolution):
            data_fine = load_plane(basename.format(resolution=2 * resolution),
                                   plane, variable)
            data_coarse = np.repeat(
                np.repeat(
                    load_plane(basename.format(resolution=resolution),
                               plane // 2, variable), 2, 0), 2, 1)

            error += np.sum(abs(data_coarse - data_fine))
        error /= resolution**3

        errors.append(error)
        resolutions.append(resolution)

        resolution *= 2

    resolutions = 2 * np.array(resolutions)

    min_error = np.min(errors)
    max_error = np.max(errors)
    if zoom:
        plt.ylim([
            2**np.floor(np.log2(min_error) - 1),
            2**np.ceil(np.log2(max_error) + 1)
        ])
    p = plt.loglog(resolutions, errors, '-o', basex=2, basey=2)
    if compute_rate:
        poly = np.polyfit(np.log(resolutions), np.log(errors), 1)
        plt.loglog(resolutions,
                   np.exp(poly[1]) * resolutions**poly[0],
                   '--',
                   color=p[0].get_color(),
                   label=f'$\\mathcal{{O}}(N^{{{poly[0]:.2f}}})$',
                   basex=2,
                   basey=2)
        plt.legend()
    plt.xlabel('Resolution ($N^3$)')
    plt.ylabel(
        f'Error ($\\lVert {latex_variables[variable]}^{{N}}-{latex_variables[variable]}^{{N/2}}\\rVert_{{L^1(D)}}$)'
    )
    plt.xticks(resolutions, [f"${r}^3$" for r in resolutions])
    plt.title(f"Convergence of single sample,\n"
              f"{title}\n"
              f"Variable: ${latex_variables[variable]}$")

    plot_info.saveData(f'single_sample_convergence_{title}_{variable}_errors',
                       errors)
    plot_info.saveData(
        f'single_sample_convergence_{title}_{variable}_resolutions',
        resolutions)

    plot_info.savePlot(f'single_sample_convergence_{title}_{variable}')
示例#3
0
                          f"\n"
                          f"mean(dnno)={np.mean(min_value_per_iteration_competitor, 1)}\n"
                          f" var(dnno)={np.mean(min_value_per_iteration_competitor, 1)}\n")
    
                    plt.xlabel("Iteration $k$")
                    if source_name == "objective":
                        plt.ylabel("$\\mathbb{E}( J(x_k^*))$")
                    else:
                        plt.ylabel(f"$\\mathrm{{{source_name}}}(x_k^*)$")
                        
                    plt.legend()
                    plt.title("type: {}, script: {}, generator: {}, batch_size_factor: {},\nstarting_size: {}".format(
                        source_name, python_script, generator, batch_size_factor, starting_size))
                    plot_info.savePlot("{script}_{source_name}_{generator}_{batch_size}_{starting_size}".format(
                        script=python_script.replace(".py", ""),
                        source_name = source_name,
                        batch_size=iterations[1],
                        starting_size=starting_size,
                        generator=generator))
                    plt.close('all')


                    ## min/max
                    plt.fill_between(iteration_range, np.min(source[0], 1), np.max(source[0], 1),
                                     alpha = 0.3, color='C0')

                    plt.plot(iteration_range, np.mean(source[0], 1), '-o',
                            label='ISMO')

                    plt.fill_between(iteration_range, np.min(source[1], 1), np.max(source[1], 1),
                                     alpha=0.3, color='C1')
示例#4
0
            ax.set_title("Fixing $z={}$".format(j / M), fontsize=20)

            ax = axes[j, 1]
            d = f.variables[sample_key][:, (j * N) // M, :]

            im = ax.pcolormesh(x, y, d)
            try:
                ax.set_xlabel('$x$')
                ax.set_ylabel('$z$')
            except:
                pass
            fig.colorbar(im, ax=ax)
            ax.set_title("Fixing $y={}$".format(j / M), fontsize=20)

            ax = axes[j, 2]
            d = f.variables[sample_key][(j * N) // M, :, :]

            img = ax.pcolormesh(x, y, d)
            try:
                ax.set_xlabel('$y$')
                ax.set_ylabel('$z$')
            except:
                pass
            fig.colorbar(im, ax=ax)
            ax.set_title("Fixing $x={}$".format(j / M), fontsize=20)
        parameters_as_str = "_".join(
            [f"{key}_{parameters[key]}" for key in parameters.keys()])

        plot_info.savePlot(os.path.splitext(os.path.basename(inname))[0] + "_" + \
                           sample_key + "_resolution_" + str(N) + parameters_as_str)
                             fmt='*',
                             uplims=True,
                             lolims=True)

                print("#" * 80)
                print(
                    f"starting_size = {starting_size}, batch_size = {batch_size}"
                )
                print(
                    f"mean(ismo)={np.mean(min_value_per_iteration, 1)}\n"
                    f" var(ismo)={np.mean(min_value_per_iteration, 1)}\n"
                    f"\n"
                    f"mean(dnno)={np.mean(min_value_per_iteration_competitor, 1)}\n"
                    f" var(dnno)={np.mean(min_value_per_iteration_competitor, 1)}\n"
                )

                plt.xlabel("Iteration $k$")
                plt.ylabel("$\\mathbb{E}( J(x_k^*))$")
                plt.legend()
                plt.title(
                    "script: {}, generator: {}, batch_size: {},\nstarting_size: {}"
                    .format(python_script, generator, batch_size,
                            starting_size))
                plot_info.savePlot(
                    "{script}_objective_{generator}_{batch_size}_{starting_size}"
                    .format(script=python_script.replace(".py", ""),
                            batch_size=batch_size,
                            starting_size=starting_size,
                            generator=generator))
                plt.close('all')
示例#6
0
def plot_convergence(basename, statistic_name, title, conserved_variables = conserved_variables_default,
                     resolutions=[64, 128,256,512],
                     reference=True):
    
    all_data = load_all_data(basename, resolutions, conserved_variables,
                             statistic_name, max(resolutions))
    
    min_values, max_values = get_min_max_values(all_data)
    
    if reference:
        # We do reference convergence
        reference_resolution = max(resolutions)
        reference_solution = all_data[reference_resolution]
    
    errors = []
    
    for resolution in resolutions[:-1]:
        timepoint = get_time(basename.format(resolution=resolution))
        
        
        data = all_data[resolution]
        
        if not reference:
            reference_resolution = resolution * 2
            reference_solution = all_data[reference_resolution]
        
        # Upscale
        while data.shape[0] < reference_resolution:
            data = np.repeat(np.repeat(np.repeat(data, 2, 0), 2, 1), 2, 2)
        
        # compute error in L^1
        error = np.sum(abs(data - reference_solution))/reference_resolution**3
        
        errors.append(error)
        
    if reference:
        convergence_type = 'reference'
    else:
        convergence_type = 'cauchy'
        
    plot_info.saveData(f'convergence_{convergence_type}_{statistic_name}_{title}_{timepoint}_errors', errors)
    plot_info.saveData(f'convergence_{convergence_type}_{statistic_name}_{title}_{timepoint}_resolutions', resolutions)
    
    plt.loglog(resolutions[:-1], errors, '-o')
    poly = np.polyfit(np.log(resolutions[:-1]), np.log(errors), 1)
    
    plt.loglog(resolutions[:-1], np.exp(poly[1])*resolutions[:-1]**poly[0],
               '--', label=f'$\\mathcal{{O}}(N^{{{poly[0]:.1f}}})$', basex=2,
               basey=2)
    
    plt.xlabel("Resolution ($N\\times N$)")
    if reference:
        plt.ylabel(f"Error ($\\|{stats_latex(statistic_name, 'N')}-{stats_latex(statistic_name, str(reference_resolution))}\\|_{{L^1(D)}}$)")
    else:
        plt.ylabel(f"Error ($\\|{stats_latex(statistic_name, 'N')}-{stats_latex(statistic_name, '2N')}\\|_{{L^1(D)}}$)")
    
    plt.xticks(resolutions[:-1], [f'${N}\\times {N}$' for N in resolutions[:-1]])
    plt.title(f'Convergence of {statistic_name.replace("_", " ")}\n{title.replace("_"," ")}\n$T={timepoint}$ {convergence_type} convergence')
    
    
    # Scale to nearest power of two to make the y axis not zoom in too much
    min_error = np.min(errors)
    max_error = np.max(errors)
    
    min_power_of_two = 2**(np.floor(np.log2(min_error)))
    max_power_of_two = 2**(np.ceil(np.log2(max_error)))
    
    if min_power_of_two == max_power_of_two:
        max_power_of_two *= 2
    
    plt.ylim([min_power_of_two, max_power_of_two])
    
    plt.legend()
    plot_info.savePlot(f'convergence_{convergence_type}_{statistic_name}_{title}_{timepoint}')
    plt.close('all')
示例#7
0
def plot_variance_decay(title, resolutions, sample_computer, norm_ord, name):
    variances, variances_details = compute_variance_decay(resolutions, 
                                                                    sample_computer,
                                                                    norm_ord)
    
    
    speedups = [1]
    
    for n in range(1, len(resolutions)):
        local_resolutions = resolutions[:n+1]
        
        # Notice the max, Monte Carlo is always a form of MLMC, so we 
        # have a minimum speedup of one!
        speedup = max(1, compute_speedup(local_resolutions, 
                                  variances[:n+1],
                                  variances_details[:n]))
        
        speedups.append(speedup)
        
        
    fig, ax1 = plt.subplots()
    ax1.loglog(resolutions, variances, '-o', 
               label=f'$\\|\\mathrm{{Var}}({name.format(N="N")})\\|_{{L^{{{norm_ord}}}}}$')
    
    
    ax1.loglog(resolutions[1:], variances_details, '-*', 
               label=f'$\\|\\mathrm{{Var}}({name.format(N="N")}-{name.format(N="N/2")})\\|_{{L^{{{norm_ord}}}}}$',
               basex=2, basey=2)
    
    ax1.legend()
    
    ax1.set_xlabel("Resolution ($N$)")
    
    ax1.set_ylabel("Variance")
    
    plt.xticks(resolutions, [f'${r}$' for r in resolutions])
    
    #plt.title(f'Structure function variance decay\n{title}\nVariable: {variable}')
    
    plot_info.savePlot(f'variance_decay_{name}_{norm_ord}_{title}')
    
    plot_info.saveData(f'variance_details_{name}_{norm_ord}_{title}', variances_details)

    plot_info.saveData(f'variance_{name}_{norm_ord}_{title}', variances)
    
    plot_info.saveData(f'variance_decay_resolutions_{name}_{norm_ord}_{title}', resolutions)
    
    ax2 = ax1.twinx()

    
    
    ax2.loglog(resolutions, speedups, '--x', label='MLMC Speedup')

    
    ax2.legend(loc=1)

    ax2.set_xscale("log", basex=2)
    ax2.set_yscale("log", basex=2)
    ax2.set_xticks(resolutions, [f'${r}$' for r in resolutions])
    ax2.set_ylabel("Potential MLMC speedup")
            
    ylims = ax2.get_ylim()
    
    ax2.set_ylim([min(ylims[0], 0.5), max(ylims[1], 4.4)])
    plt.xticks(resolutions, [f'${r}$' for r in resolutions])
    
    plot_info.savePlot(f'variance_decay_with_speedup_{name}_{norm_ord}_{title}')
    
    plot_info.saveData(f'variance_decay_speedups_{name}_{norm_ord}_{title}', speedups)
     
def plot_convergence(basename, title, variable, starting_resolution, stat, zoom, compute_rate, reference_solution=False):

    if variable != 'all':
        variables = [variable]
    else:
        variables = ['rho', 'mx', 'my', 'mz', 'E']

    resolution = starting_resolution

    resolutions = []
    errors = []
    if reference_solution:
        max_resolution = starting_resolution
        while resolution_exists(basename, max_resolution, stat):
            resolutions.append(max_resolution)
            max_resolution = 2*max_resolution
        print(f'max_resolution = {resolutions[-1]}')
        for resolution in resolutions[:-1]:
            print(resolution)
            error = 0.0
            for plane in range(resolutions[-1]):
                for variable_local in variables:
                    data_fine = load_plane(basename.format(resolution=resolutions[-1], stat=stat), plane, variable_local)
                
                    factor_plane = resolutions[-1]//resolution
                    data_coarse = load_plane(basename.format(resolution=resolution, stat=stat), plane//factor_plane, variable_local)
                
                    while data_coarse.shape[0] < data_fine.shape[0]:
                        data_coarse = np.repeat(np.repeat(data_coarse, 2, 0), 2, 1)

                    error += np.sum(abs(data_coarse-data_fine))
            error /= (resolutions[-1])**3

            errors.append(error)
        resolutions = np.array(resolutions)[:-1]
    else:
        while resolution_exists(basename, resolution,stat) and resolution_exists(basename, 2*resolution,stat):
            print(resolution)
            error = 0.0
            for plane in range(2*resolution):
                for variable_local in variables:
                    data_fine = load_plane(basename.format(resolution=2*resolution, stat=stat), plane, variable_local)
                    data_coarse = np.repeat(np.repeat(load_plane(basename.format(resolution=resolution, stat=stat), plane//2, variable_local), 2, 0), 2, 1)

                    error += np.sum(abs(data_coarse-data_fine))
            error /= (2*resolution)**3

            errors.append(error)
            resolutions.append(resolution)

            resolution *= 2

    
        resolutions = 2*np.array(resolutions)

    min_error = np.min(errors)
    max_error = np.max(errors)
    if zoom:
        plt.ylim([2**np.floor(np.log2(min_error)-1), 2**np.ceil(np.log2(max_error)+1)])
    p = plt.loglog(resolutions, errors, '-o', basex=2, basey=2)
    if compute_rate:
        poly = np.polyfit(np.log(resolutions), np.log(errors), 1)
        plt.loglog(resolutions, np.exp(poly[1])*resolutions**poly[0], '--',
                   color=p[0].get_color(),
                   label=f'$\\mathcal{{O}}(N^{{{poly[0]:.2f}}})$',
                   basex=2,
                   basey=2)
        plt.legend()

    plt.xlabel('Resolution ($N^3$)')
    if reference_solution:
        plt.ylabel(f'Error ($\\lVert {latex_stat[stat]}({latex_variables[variable]}^{{N}})-{latex_stat[stat]}({latex_variables[variable]}^{{{2*resolutions[-1]}}})\\rVert_{{L^1(D)}}$)')
    else:
        plt.ylabel(f'Error ($\\lVert {latex_stat[stat]}({latex_variables[variable]}^{{N}})-{latex_stat[stat]}({latex_variables[variable]}^{{N/2}})\\rVert_{{L^1(D)}}$)')
    plt.xticks(resolutions, [f"${r}^3$" for r in resolutions])
    plt.title(f"Convergence of {stat},\n"
              f"{title}\n"
              f"Variable: ${latex_variables[variable]}$")
    if reference_solution:
        plot_info.saveData(f'{stat}_convergence_reference_{title}_{variable}_errors', errors)
        plot_info.saveData(f'{stat}_convergence_reference_{title}_{variable}_resolutions', resolutions)

        plot_info.savePlot(f'{stat}_convergence_reference_{title}_{variable}')
    else:
        plot_info.saveData(f'{stat}_convergence_{title}_{variable}_errors', errors)
        plot_info.saveData(f'{stat}_convergence_{title}_{variable}_resolutions', resolutions)

        plot_info.savePlot(f'{stat}_convergence_{title}_{variable}')
示例#9
0
def plot_convergence(basename_a, name_a, basename_b, name_b, title, variable,
                     starting_resolution, stat, zoom, compute_rate):

    resolution = starting_resolution

    resolutions = []
    errors = []

    while resolution_exists(basename_a, resolution,
                            stat) and resolution_exists(
                                basename_b, resolution, stat):
        print(resolution)
        error = 0.0
        for plane in range(resolution):
            data_a = load_plane(
                basename_a.format(resolution=resolution, stat=stat), plane,
                variable)
            data_b = load_plane(
                basename_b.format(resolution=resolution, stat=stat), plane,
                variable)

            data_a_relative = np.max(abs(data_a))
            error += np.sum(abs(data_a - data_b) / abs(data_a_relative))
        error /= resolution**3

        errors.append(error)
        resolutions.append(resolution)

        resolution *= 2

    resolutions = np.array(resolutions)

    min_error = np.min(errors)
    max_error = np.max(errors)
    if zoom:
        plt.ylim([
            2**np.floor(np.log2(min_error) - 1),
            2**np.ceil(np.log2(max_error) + 1)
        ])
    p = plt.loglog(resolutions, errors, '-o', basex=2, basey=2)
    if compute_rate:
        poly = np.polyfit(np.log(resolutions), np.log(errors), 1)
        plt.loglog(resolutions,
                   np.exp(poly[1]) * resolutions**poly[0],
                   '--',
                   color=p[0].get_color(),
                   label=f'$\\mathcal{{O}}(N^{{{poly[0]:.2f}}})$',
                   basex=2,
                   basey=2)
        plt.legend()

    plt.xlabel('Resolution ($N^3$)')
    if "_" not in latex_variables[variable]:
        plt.ylabel(
            f'Error ($\\lVert \\left({latex_stat[stat]}({latex_variables[variable]}^{{N}}_{{\\mathrm{{{name_a}}}}})-{latex_stat[stat]}({latex_variables[variable]}^{{N}}_{{\\mathrm{{{name_b}}}}})\\right)/(\\max({latex_stat[stat]}({latex_variables[variable]}^{{N}}_{{\\mathrm{{{name_a}}}}}))\\rVert_{{L^1(D)}}$)'
        )
    else:
        latex_variable_with_subscript_a = f"{latex_variables[variable][:-2]}_{{{latex_variables[variable][-1]}, \\mathrm{{{name_a}}}}}"
        latex_variable_with_subscript_b = f"{latex_variables[variable][:-2]}_{{{latex_variables[variable][-1]}, \\mathrm{{{name_b}}}}}"
        plt.ylabel(
            f'Error ($\\lVert \\left({latex_stat[stat]}({latex_variable_with_subscript_a}^{{N}})-{latex_stat[stat]}({latex_variable_with_subscript_b}^{{N}})\\right)/(\\max({latex_stat[stat]}({latex_variable_with_subscript_a}^{{N}}))\\rVert_{{L^1(D)}}$)'
        )
    plt.xticks(resolutions, [f"${r}^3$" for r in resolutions])
    plt.title(f"Convergence of {stat},\n"
              f"Comparing {name_a} and {name_b}\n"
              f"{title}\n"
              f"Variable: ${latex_variables[variable]}$")

    plot_info.saveData(
        f'{stat}_convergence_comparison_{name_a}_{name_b}_{title}_{variable}_errors',
        errors)
    plot_info.saveData(
        f'{stat}_convergence_comparison_{name_a}_{name_b}_{title}_{variable}_resolutions',
        resolutions)

    plot_info.savePlot(
        f'{stat}_convergence_comparison_{name_a}_{name_b}_{title}_{variable}')
示例#10
0
    work_per_iteration = np.cumsum(sample_per_iteration)

    plt.errorbar(work_per_iteration,
                 np.mean(data_ismo, axis=1),
                 yerr=np.std(data_ismo, axis=1),
                 label='ISMO',
                 fmt='o',
                 uplims=True,
                 lolims=True)

    plt.errorbar(work_per_iteration,
                 np.mean(data_ismo_renew, axis=1),
                 yerr=np.std(data_ismo_renew, axis=1),
                 label='ISMO reusing samples',
                 fmt='o',
                 uplims=True,
                 lolims=True)

    plt.title(
        f'Comparison ISMO with reusing samples ({args.generator})\nwith {args.compare_start_size} starting samples\nand {args.compare_batch_size} batch size.'
    )
    plt.xscale('log', basex=2)
    plt.yscale('log', basey=2)
    plt.xlabel("Number of evaluations of the simulator")
    plt.ylabel("Minimum value")
    plot_info.plot_info.legendLeft()
    plot_info.savePlot(
        f"compare_with_ismo_reuse_mean_std_batch_size_{args.generator}_{args.compare_batch_size}_{args.compare_start_size}"
    )
    plt.close('all')
示例#11
0
                                    ):sum(iterations[:iteration + 1]), 0],
                                     bins=15,
                                     range=(min_objective_value,
                                            max_objective_value))
                            plt.xlabel(f"{source_name} value")
                            plt.ylabel("Number of samples")
                            plt.title(
                                "iteration: {}, type: {}, script: {}, generator: {}, batch_size_factor: {},\nstarting_size: {}"
                                .format(iteration, source_name, python_script,
                                        generator, batch_size_factor,
                                        starting_size))
                            plot_info.savePlot(
                                "evolution_hist_{script}_{source_name}_{generator}_{batch_size}_{starting_size}_{iteration}"
                                .format(script=python_script.replace(
                                    ".py", ""),
                                        source_name=source_name,
                                        batch_size=iterations[1],
                                        starting_size=starting_size,
                                        generator=generator,
                                        iteration=iteration))
                            plt.close('all')
                    except:
                        print("Skipping")

                for iteration in range(len(iterations)):
                    mean_value = np.mean(min_value_per_iteration[iteration, :])
                    end_index = sum(iterations[:iteration + 1])
                    index_closest_to_mean_value = np.unravel_index(
                        abs(mean_value -
                            value_per_iteration[:end_index, :]).argmin(),
                        value_per_iteration.shape)
            f.write(str(runtime))

    for version, runtime in zip(versions, runtimes):
        print("{}: {}".format(version, runtime))

    indices = np.arange(0, len(versions))

    plt.bar(indices, runtimes)
    plt.gca().set_xticks(indices)
    plt.gca().set_xticklabels(versions)
    plt.xlabel("Tensorflow version")
    plt.ylabel("Total runtime (s)")
    plt.title(
        "Runtimes for different tensorflow version\nEach runtime includes 5 retrainings with an (approx) 12*10 network,\nand 5 retrainings with a 6x4 network\nand done for three functionals (Q1, Q2 and Q3)\nKeras version: {}"
        .format(default_keras_version))
    savePlot('runtimes_tensorflow')
    plt.close()

    keras_versions = get_keras_versions()
    default_tensorflow_version = get_tensorflow_versions()[0]
    runtimes_keras = []
    for keras_version in keras_versions:
        build_docker_with_keras_and_tensorflow_version(
            tensorflow_version=default_tensorflow_version,
            keras_version=keras_version)
        start = time.time()
        run_with_keras_and_tensorflow_version(
            tensorflow_version=default_tensorflow_version,
            keras_version=keras_version)
        end = time.time()