Esempio n. 1
0
def save(model_name='dop_po4', time_step=1, parameter_sets=range(9999), data_kind='WOA', eval_f=True, eval_df=True, as_jobs=False, node_kind='clexpress'):
    from simulation.model.constants import DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME, DATABASE_TIME_STEP_DIRNAME, DATABASE_PARAMETERS_SET_DIRNAME, DATABASE_PARAMETERS_FILENAME, DATABASE_SPINUP_DIRNAME, DATABASE_DERIVATIVE_DIRNAME
    
    ## get time step dir
    model_dirname = DATABASE_MODEL_DIRNAME.format(model_name)
    model_dir = os.path.join(DATABASE_OUTPUT_DIR, model_dirname)
    time_step_dirname = DATABASE_TIME_STEP_DIRNAME.format(time_step)
    time_step_dir = os.path.join(model_dir, time_step_dirname)
    
    ## create model
    model = simulation.model.eval.Model()

    ## save for all parameter sets
    for parameter_set_number in parameter_sets:

        ## get parameter
        cost_function_family = None

        parameter_set_dirname = DATABASE_PARAMETERS_SET_DIRNAME.format(parameter_set_number)
        parameter_set_dir = os.path.join(time_step_dir, parameter_set_dirname)
        parameters_file = os.path.join(parameter_set_dir, DATABASE_PARAMETERS_FILENAME)

        ## create cost functions
        if os.path.exists(parameters_file):
            spinup_dir = os.path.join(parameter_set_dir, DATABASE_SPINUP_DIRNAME)
            last_run_dir = model.last_run_dir(spinup_dir)

            if last_run_dir is not None:
                cf_kargs = {'data_kind': data_kind, 'model_options': {'spinup_options': {'years':1,}, 'time_step': time_step, 'total_concentration_factor_included_in_parameters': True}, 'job_setup':{'name': 'SCF_' + data_kind}}
                cost_function_family = simulation.optimization.cost_function.Family(**cf_kargs)
                
        ## eval cf family
        if cost_function_family is not None:
            p = np.loadtxt(parameters_file)
            try:
                ## eval cf by itself
                if not as_jobs:
                    ## eval f
                    if eval_f:
                        cost_function_family.f(p)
                        cost_function_family.f_normalized(p)

                    ## eval df
                    if eval_df:
                        derivative_dir = os.path.join(parameter_set_dir, DATABASE_DERIVATIVE_DIRNAME)
                        if os.path.exists(derivative_dir):
                            cost_function_family.df(p)
                    
                ## eval cf as job
                else:
                    for cf in cost_function_family.family:
                        if (eval_f and not cf.f_available(p)) or (eval_df and not cf.df_available(p)):
                            from util.constants import TMP_DIR
                            output_dir = tempfile.TemporaryDirectory(dir=TMP_DIR, prefix='save_value_cost_function_tmp_').name
                            cf_kargs = cf.kargs
                            cf_kargs['job_setup'] = {'name': '{}:{}'.format(cf, parameter_set_number)}
                            nodes_setup = util.batch.universal.system.NodeSetup(memory=50, node_kind=node_kind, nodes=1, cpus=1, total_cpus_max=1, walltime=1)
                            with simulation.optimization.job.CostFunctionJob(output_dir, p, cf.kind, eval_f=eval_f, eval_df=eval_df, nodes_setup=nodes_setup, **cf_kargs) as cf_job:
                                cf_job.start()
                            time.sleep(10)

            except ValueError as e:
                logger.exception(e)
Esempio n. 2
0
def model_diff(parameter_set_nr, data_kind='WOA', path='/tmp', normalize_with_deviation=False, y_max=(None, None)):
    from simulation.model.constants import DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME, DATABASE_TIME_STEP_DIRNAME, DATABASE_PARAMETERS_SET_DIRNAME, DATABASE_PARAMETERS_FILENAME
    from simulation.model.constants import (METOS_X_DIM as X_DIM, METOS_Y_DIM as Y_DIM, METOS_Z_LEFT as Z_VALUES_LEFT)

    logger.debug('Plotting model output for parameter set {}'.format(parameter_set_nr))

    ## load parameters
    parameter_set_dirname = DATABASE_PARAMETERS_SET_DIRNAME.format(parameter_set_nr)
    p_file = os.path.join(DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME.format('dop_po4'), DATABASE_TIME_STEP_DIRNAME.format(1), parameter_set_dirname, DATABASE_PARAMETERS_FILENAME)
    p = np.loadtxt(p_file)

    ## init data base
    data_base = simulation.util.data_base.init_data_base(data_kind)
    if not normalize_with_deviation:
        file = os.path.join(path, 'model_diff_-_{}_-_' + parameter_set_dirname + '_-_{}.png')
    else:
        file = os.path.join(path, 'model_diff_normalized_with_deviation_-_{}_-_' + parameter_set_dirname + '_-_{}.png')

    ## print for WOA
    if data_kind.upper() == 'WOA':
        diff_boxes = np.abs(data_base.diff_boxes(p, normalize_with_deviation=normalize_with_deviation))
    ## print for WOD
    elif data_kind.upper() == 'WOD':
        diff = np.abs(data_base.diff(p, normalize_with_deviation=normalize_with_deviation))
        diff_boxes = data_base.convert_to_boxes(diff, no_data_value=np.inf)
    else:
        raise ValueError('Data_kind {} unknown. Must be "WOA" or "WOD".'.format(data_kind))


    def plot_tracer_diff(diff, file, y_max=None):
        util.plot.data(np.abs(diff), file, land_value=np.nan, no_data_value=np.inf, v_min=0, v_max=y_max)

    plot_tracer_diff(diff_boxes[0], file.format(data_kind, 'dop'), y_max=y_max[0])
    plot_tracer_diff(diff_boxes[1], file.format(data_kind, 'po4'), y_max=y_max[1])
Esempio n. 3
0
def check_db_entry_integrity(model_name='dop_po4', time_step=1, parameter_set_dirs_to_check=None, check_for_same_parameters=True):
    from simulation.model.constants import DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME, DATABASE_TIME_STEP_DIRNAME, DATABASE_SPINUP_DIRNAME, DATABASE_DERIVATIVE_DIRNAME, JOB_OPTIONS_FILENAME, DATABASE_PARAMETERS_FILENAME
    from simulation.util.constants import CACHE_DIRNAME, WOD_F_FILENAME, WOD_DF_FILENAME

    wod_m = simulation.util.data_base.WOD().m

    model_dirname = DATABASE_MODEL_DIRNAME.format(model_name)
    model_dir = os.path.join(DATABASE_OUTPUT_DIR, model_dirname)
    time_step_dirname = DATABASE_TIME_STEP_DIRNAME.format(time_step)
    time_step_dir = os.path.join(model_dir, time_step_dirname)
    df_step_sizes = [10**(-6), 10**(-7)]

    check_all_parameter_sets = parameter_set_dirs_to_check is None or (len(parameter_set_dirs_to_check) == 1 and parameter_set_dirs_to_check[0] is None)
    check_for_same_parameters = check_for_same_parameters and (check_all_parameter_sets or len(parameter_set_dirs_to_check) > 1)
    if check_all_parameter_sets or check_for_same_parameters:
        parameter_set_dirs_all = util.io.fs.get_dirs(time_step_dir)
    if check_all_parameter_sets:
        parameter_set_dirs_to_check = parameter_set_dirs_all

    for parameter_set_dir in parameter_set_dirs_to_check:
    
        print('Checking integrity of parameter set {}.'.format(parameter_set_dir))
        
        ## check spinup dir
        spinup_dir = os.path.join(parameter_set_dir, DATABASE_SPINUP_DIRNAME)
        check_db_entry_integrity_spinup(spinup_dir, True)
        
        ## check derivative dir
        for df_step_size in df_step_sizes:
            derivative_dir = os.path.join(parameter_set_dir, DATABASE_DERIVATIVE_DIRNAME.format(df_step_size))
            partial_derivative_dirs = util.io.fs.get_dirs(derivative_dir)
            for partial_derivative_dir in partial_derivative_dirs:
                check_db_entry_integrity_spinup(partial_derivative_dir, False)

        ## check for parameters
        p = np.loadtxt(os.path.join(parameter_set_dir, DATABASE_PARAMETERS_FILENAME))
        if not np.all(np.isfinite(p)):
            print('Parameters {} in set {} are not finite!'.format(p, parameter_set_dir))

        ## check for same parameters
        if check_for_same_parameters:
            for parameter_set_dir_i in parameter_set_dirs_all:
                if parameter_set_dir_i != parameter_set_dir:
                    p_i = np.loadtxt(os.path.join(parameter_set_dir_i, DATABASE_PARAMETERS_FILENAME))
                    # if np.allclose(p, p_i):
                    if np.all(p == p_i):
                        print('Parameter set {} and {} have same parameters!'.format(parameter_set_dir, parameter_set_dir_i))

        ## check WOD output
        f_wod_file = os.path.join(parameter_set_dir, CACHE_DIRNAME, WOD_F_FILENAME)
        try:
            f_wod = np.load(f_wod_file)
        except FileNotFoundError:
            f_wod = None
        if f_wod is not None:
            if f_wod.ndim != 1 or len(f_wod) != wod_m:
                print('Wod f file {} has wrong shape {}!'.format(f_wod_file, f_wod.shape))

        df_wod_file = os.path.join(parameter_set_dir, CACHE_DIRNAME, WOD_DF_FILENAME)
        try:
            df_wod = np.load(df_wod_file)
        except FileNotFoundError:
            df_wod = None
        if df_wod is not None:
            if df_wod.ndim != 2 or len(df_wod) != wod_m or df_wod.shape[1] != len(p):
                print('Wod df file {} has wrong shape {}!'.format(df_wod_file, df_wod.shape))

        ## check value cache
        value_cache_option_files = util.io.fs.filter_with_filename_pattern(parameter_set_dir, '*options.npy', exclude_dirs=True, use_absolute_filenames=True, recursive=True)
        for value_cache_option_file in value_cache_option_files:
            value_cache_option = np.load(value_cache_option_file)
            if not value_cache_option.ndim == 1:
                print('Value cache option {} has ndim {}!'.format(value_cache_option_file, value_cache_option.ndim))
            if not len(value_cache_option) in [3, 6]:
                print('Value cache option {} has len {}!'.format(value_cache_option_file, len(value_cache_option)))
        
        ## check file permissions
        def check_file(file):
            permissions = os.stat(file)[stat.ST_MODE]
            if not (permissions & stat.S_IRUSR and permissions & stat.S_IRGRP):
                print('File {} is not readable!'.format(file))
        def check_dir(file):
            permissions = os.stat(file)[stat.ST_MODE]
            if not (permissions & stat.S_IRUSR and permissions & stat.S_IXUSR and permissions & stat.S_IRGRP and permissions & stat.S_IXGRP):
                print('Dir {} is not readable!'.format(file))
        
        util.io.fs.walk_all_in_dir(parameter_set_dir, check_file, check_dir, exclude_dir=False, topdown=True)
Esempio n. 4
0
def model_confidence(parameter_set_nr, kind='WOA_WLS', path='/tmp', v_max=[None, None], time_dim_confidence=12, time_dim_df=12, average_in_time=False):
    from simulation.model.constants import DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME, DATABASE_TIME_STEP_DIRNAME, DATABASE_PARAMETERS_SET_DIRNAME
    from simulation.accuracy.constants import CACHE_DIRNAME, MODEL_CONFIDENCE_FILENAME

    logger.debug('Plotting model confidence for parameter set {}'.format(parameter_set_nr))

    ## load value
    parameter_set_dirname = DATABASE_PARAMETERS_SET_DIRNAME.format(parameter_set_nr)
    f_file = os.path.join(DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME.format('dop_po4'), DATABASE_TIME_STEP_DIRNAME.format(1), parameter_set_dirname, CACHE_DIRNAME, kind, MODEL_CONFIDENCE_FILENAME.format(time_dim_confidence=time_dim_confidence, time_dim_df=time_dim_df))
    f = np.load(f_file)
    if average_in_time:
        f = f.mean(axis=1)
    assert len(f) == 2
    
    ## set v_max
    for i in range(len(f)):
        if v_max[i] is None:
            v_max[i] = np.nanmax(f[i])
            rounding_exponent = np.sign(np.log10(v_max[i])) * np.ceil(np.abs(np.log10(v_max[i])))
            v_max[i] = np.floor(v_max[i] * 10**(-rounding_exponent)) * 10**rounding_exponent
    
    ## plot
    file = os.path.join(path, 'model_confidence_-_' + parameter_set_dirname + '_-_' + get_label(kind) + '_-_time_dim_df_{}'.format(time_dim_df) + '_-_{tracer}.png')
    tracers = ('dop', 'po4')
    for i in range(len(tracers)):
        util.plot.data(f[i], file.format(tracer=tracers[i]), land_value=np.nan, no_data_value=None, v_min=0, v_max=v_max[i], contours=True, colorbar=False)
Esempio n. 5
0
def average_model_confidence_increase(parameter_set_nr, kind='WOA_WLS', path='/tmp', time_dim_df=12):
    from simulation.model.constants import DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME, DATABASE_TIME_STEP_DIRNAME, DATABASE_PARAMETERS_SET_DIRNAME
    from simulation.accuracy.constants import CACHE_DIRNAME, AVERAGE_MODEL_CONFIDENCE_INCREASE_FILENAME

    logger.debug('Plotting average model confidence increase for parameter set {}'.format(parameter_set_nr))

    parameter_set_dirname = DATABASE_PARAMETERS_SET_DIRNAME.format(parameter_set_nr)

    f_file = os.path.join(DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME.format('dop_po4'), DATABASE_TIME_STEP_DIRNAME.format(1), parameter_set_dirname, CACHE_DIRNAME, kind, AVERAGE_MODEL_CONFIDENCE_INCREASE_FILENAME.format(time_dim_df))
    f = np.load(f_file)

    v_min = np.nanmin(f, axis=tuple(np.arange(f.ndim-1)+1))
    v_max = np.nanmax(f, axis=tuple(np.arange(f.ndim-1)+1))
    significant_digits=3
    for i in range(len(v_min)):
        round_factor = 10 ** (np.ceil(-np.log10(v_min[i])) + significant_digits)
        v_min[i] = np.floor(v_min[i] * round_factor) / round_factor
    for i in range(len(v_max)):
        round_factor = 10 ** (np.ceil(-np.log10(v_min[i])) + significant_digits)
        v_max[i] = np.ceil(v_max[i] * round_factor) / round_factor

    file = os.path.join(path, 'average_model_confidence_increase_-_' + parameter_set_dirname + '_-_time_dim_df_{}'.format(time_dim_df) + '_-_{}_-_{}.png')
    util.plot.data(f[0], file.format(kind, 'dop'), land_value=np.nan, no_data_value=None, v_min=v_min[0], v_max=v_max[0])
    util.plot.data(f[1], file.format(kind, 'po4'), land_value=np.nan, no_data_value=None, v_min=v_min[1], v_max=v_max[1])
Esempio n. 6
0
def model_output(parameter_set_nr, kind='BOXES', path='/tmp', y_max=(None, None), average_in_time=False):
    from simulation.model.constants import DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME, DATABASE_TIME_STEP_DIRNAME, DATABASE_PARAMETERS_SET_DIRNAME, DATABASE_PARAMETERS_FILENAME
    from simulation.util.constants import CACHE_DIRNAME, BOXES_F_FILENAME, WOD_F_FILENAME

    logger.debug('Plotting model output for parameter set {}'.format(parameter_set_nr))

    ## load parameters
    parameter_set_dirname = DATABASE_PARAMETERS_SET_DIRNAME.format(parameter_set_nr)
    p_file = os.path.join(DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME.format('dop_po4'), DATABASE_TIME_STEP_DIRNAME.format(1), parameter_set_dirname, DATABASE_PARAMETERS_FILENAME)
    p = np.loadtxt(p_file)

    ## init data base
    if kind.upper() == 'BOXES':
        data_base = simulation.util.data_base.init_data_base('WOA')
        f = data_base.f_boxes(p)
        f[f < 0] = 0
        if average_in_time:
            f = f.mean(axis=1)
    else:
        data_base = simulation.util.data_base.init_data_base('WOD')
        f = data_base.f(p)
        f = data_base.convert_to_boxes(f, no_data_value=np.inf)

    file = os.path.join(path, 'model_output_-_' + kind + '_-_' + parameter_set_dirname + '_-_{tracer}.png')
    tracers = ('dop', 'po4')
    for i in range(len(tracers)):
        util.plot.data(f[i], file.format(tracer=tracers[i]), land_value=np.nan, no_data_value=np.inf, v_min=0, v_max=y_max[i], contours=True, colorbar=False)
Esempio n. 7
0
def relative_parameter_confidence(parameter_set_nr, kind='WOA_WLS', path='/tmp'):
    from simulation.model.constants import DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME, DATABASE_TIME_STEP_DIRNAME, DATABASE_PARAMETERS_SET_DIRNAME, DATABASE_PARAMETERS_FILENAME
    from simulation.accuracy.constants import CACHE_DIRNAME, PARAMETER_CONFIDENCE_FILENAME

    logger.debug('Plotting parameter confidence for parameter set {}'.format(parameter_set_nr))

    ## load value
    parameter_dirname = DATABASE_PARAMETERS_SET_DIRNAME.format(parameter_set_nr)
    parameter_dir = os.path.join(DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME.format('dop_po4'), DATABASE_TIME_STEP_DIRNAME.format(1), parameter_dirname)
    parameter_value_file = os.path.join(parameter_dir, DATABASE_PARAMETERS_FILENAME)
    parameter_value = np.loadtxt(parameter_value_file)
    parameter_confidence_file = os.path.join(parameter_dir, CACHE_DIRNAME, kind, PARAMETER_CONFIDENCE_FILENAME)
    parameter_confidence = np.load(parameter_confidence_file)
    
    relative_parameter_confidence_percent = 100 * parameter_confidence / parameter_value
    relative_parameter_confidence_percent_interval = [relative_parameter_confidence_percent, - relative_parameter_confidence_percent]
    
    ## plot
    file = os.path.join(path, 'relative_parameter_confidence_-_' + parameter_dirname + '_-_' + get_label(kind) + '.png')
    util.plot.intervals(relative_parameter_confidence_percent_interval, file, use_percent_ticks=True)
Esempio n. 8
0
def model_diff(parameter_set_nr, data_kind='WOA', path='/tmp', normalize_with_deviation=False, y_max=(None, None)):
    from simulation.model.constants import DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME, DATABASE_TIME_STEP_DIRNAME, DATABASE_PARAMETERS_DIRNAME, DATABASE_PARAMETERS_FILENAME
    from simulation.model.constants import (METOS_X_DIM as X_DIM, METOS_Y_DIM as Y_DIM, METOS_Z_LEFT as Z_VALUES_LEFT)

    logger.debug('Plotting model output for parameter set {}'.format(parameter_set_nr))

    ## load parameters
    parameter_set_dirname = DATABASE_PARAMETERS_DIRNAME.format(parameter_set_nr)
    p_file = os.path.join(DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME.format('dop_po4'), DATABASE_TIME_STEP_DIRNAME.format(1), parameter_set_dirname, DATABASE_PARAMETERS_FILENAME)
    p = np.loadtxt(p_file)

    ## init data base
    data_base = simulation.util.data_base.init_data_base(data_kind)
    if not normalize_with_deviation:
        file = os.path.join(path, 'model_diff_-_{}_-_' + parameter_set_dirname + '_-_{}.png')
    else:
        file = os.path.join(path, 'model_diff_normalized_with_deviation_-_{}_-_' + parameter_set_dirname + '_-_{}.png')

    ## print for WOA
    if data_kind.upper() == 'WOA':
        diff_boxes = np.abs(data_base.diff_boxes(p, normalize_with_deviation=normalize_with_deviation))
    ## print for WOD
    elif data_kind.upper() == 'WOD':
        diff = np.abs(data_base.diff(p, normalize_with_deviation=normalize_with_deviation))
        diff_boxes = data_base.convert_to_boxes(diff, no_data_value=np.inf)
    else:
        raise ValueError('Data_kind {} unknown. Must be "WOA" or "WOD".'.format(data_kind))


    def plot_tracer_diff(diff, file, y_max=None):
        util.plot.data(np.abs(diff), file, land_value=np.nan, no_data_value=np.inf, v_min=0, v_max=y_max)

    plot_tracer_diff(diff_boxes[0], file.format(data_kind, 'dop'), y_max=y_max[0])
    plot_tracer_diff(diff_boxes[1], file.format(data_kind, 'po4'), y_max=y_max[1])
Esempio n. 9
0
def average_model_confidence_increase(parameter_set_nr, kind='WOA_WLS', path='/tmp', time_dim_df=12):
    from simulation.model.constants import DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME, DATABASE_TIME_STEP_DIRNAME, DATABASE_PARAMETERS_DIRNAME
    from simulation.accuracy.constants import CACHE_DIRNAME, AVERAGE_MODEL_CONFIDENCE_INCREASE_FILENAME

    logger.debug('Plotting average model confidence increase for parameter set {}'.format(parameter_set_nr))

    parameter_set_dirname = DATABASE_PARAMETERS_DIRNAME.format(parameter_set_nr)

    f_file = os.path.join(DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME.format('dop_po4'), DATABASE_TIME_STEP_DIRNAME.format(1), parameter_set_dirname, CACHE_DIRNAME, kind, AVERAGE_MODEL_CONFIDENCE_INCREASE_FILENAME.format(time_dim_df))
    f = np.load(f_file)

    v_min = np.nanmin(f, axis=tuple(np.arange(f.ndim-1)+1))
    v_max = np.nanmax(f, axis=tuple(np.arange(f.ndim-1)+1))
    significant_digits=3
    for i in range(len(v_min)):
        round_factor = 10 ** (np.ceil(-np.log10(v_min[i])) + significant_digits)
        v_min[i] = np.floor(v_min[i] * round_factor) / round_factor
    for i in range(len(v_max)):
        round_factor = 10 ** (np.ceil(-np.log10(v_min[i])) + significant_digits)
        v_max[i] = np.ceil(v_max[i] * round_factor) / round_factor

    file = os.path.join(path, 'average_model_confidence_increase_-_' + parameter_set_dirname + '_-_time_dim_df_{}'.format(time_dim_df) + '_-_{}_-_{}.png')
    util.plot.data(f[0], file.format(kind, 'dop'), land_value=np.nan, no_data_value=None, v_min=v_min[0], v_max=v_max[0])
    util.plot.data(f[1], file.format(kind, 'po4'), land_value=np.nan, no_data_value=None, v_min=v_min[1], v_max=v_max[1])
Esempio n. 10
0
def model_confidence(parameter_set_nr, kind='WOA_WLS', path='/tmp', v_max=[None, None], time_dim_confidence=12, time_dim_df=12, average_in_time=False):
    from simulation.model.constants import DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME, DATABASE_TIME_STEP_DIRNAME, DATABASE_PARAMETERS_DIRNAME
    from simulation.accuracy.constants import CACHE_DIRNAME, MODEL_CONFIDENCE_FILENAME

    logger.debug('Plotting model confidence for parameter set {}'.format(parameter_set_nr))

    ## load value
    parameter_set_dirname = DATABASE_PARAMETERS_DIRNAME.format(parameter_set_nr)
    f_file = os.path.join(DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME.format('dop_po4'), DATABASE_TIME_STEP_DIRNAME.format(1), parameter_set_dirname, CACHE_DIRNAME, kind, MODEL_CONFIDENCE_FILENAME.format(time_dim_confidence=time_dim_confidence, time_dim_df=time_dim_df))
    f = np.load(f_file)
    if average_in_time:
        f = f.mean(axis=1)
    assert len(f) == 2
    
    ## set v_max
    for i in range(len(f)):
        if v_max[i] is None:
            v_max[i] = np.nanmax(f[i])
            rounding_exponent = np.sign(np.log10(v_max[i])) * np.ceil(np.abs(np.log10(v_max[i])))
            v_max[i] = np.floor(v_max[i] * 10**(-rounding_exponent)) * 10**rounding_exponent
    
    ## plot
    file = os.path.join(path, 'model_confidence_-_' + parameter_set_dirname + '_-_' + get_label(kind) + '_-_time_dim_df_{}'.format(time_dim_df) + '_-_{tracer}.png')
    tracers = ('dop', 'po4')
    for i in range(len(tracers)):
        util.plot.data(f[i], file.format(tracer=tracers[i]), land_value=np.nan, no_data_value=None, v_min=0, v_max=v_max[i], contours=True, colorbar=False)
Esempio n. 11
0
def relative_parameter_confidence(parameter_set_nr, kind='WOA_WLS', path='/tmp'):
    from simulation.model.constants import DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME, DATABASE_TIME_STEP_DIRNAME, DATABASE_PARAMETERS_DIRNAME, DATABASE_PARAMETERS_FILENAME
    from simulation.accuracy.constants import CACHE_DIRNAME, PARAMETER_CONFIDENCE_FILENAME

    logger.debug('Plotting parameter confidence for parameter set {}'.format(parameter_set_nr))

    ## load value
    parameter_dirname = DATABASE_PARAMETERS_DIRNAME.format(parameter_set_nr)
    parameter_dir = os.path.join(DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME.format('dop_po4'), DATABASE_TIME_STEP_DIRNAME.format(1), parameter_dirname)
    parameter_value_file = os.path.join(parameter_dir, DATABASE_PARAMETERS_FILENAME)
    parameter_value = np.loadtxt(parameter_value_file)
    parameter_confidence_file = os.path.join(parameter_dir, CACHE_DIRNAME, kind, PARAMETER_CONFIDENCE_FILENAME)
    parameter_confidence = np.load(parameter_confidence_file)
    
    relative_parameter_confidence_percent = 100 * parameter_confidence / parameter_value
    relative_parameter_confidence_percent_interval = [relative_parameter_confidence_percent, - relative_parameter_confidence_percent]
    
    ## plot
    file = os.path.join(path, 'relative_parameter_confidence_-_' + parameter_dirname + '_-_' + get_label(kind) + '.png')
    util.plot.intervals(relative_parameter_confidence_percent_interval, file, use_percent_ticks=True)
Esempio n. 12
0
def model_output(parameter_set_nr, kind='BOXES', path='/tmp', y_max=(None, None), average_in_time=False):
    from simulation.model.constants import DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME, DATABASE_TIME_STEP_DIRNAME, DATABASE_PARAMETERS_DIRNAME, DATABASE_PARAMETERS_FILENAME
    from simulation.util.constants import CACHE_DIRNAME, BOXES_F_FILENAME, WOD_F_FILENAME

    logger.debug('Plotting model output for parameter set {}'.format(parameter_set_nr))

    ## load parameters
    parameter_set_dirname = DATABASE_PARAMETERS_DIRNAME.format(parameter_set_nr)
    p_file = os.path.join(DATABASE_OUTPUT_DIR, DATABASE_MODEL_DIRNAME.format('dop_po4'), DATABASE_TIME_STEP_DIRNAME.format(1), parameter_set_dirname, DATABASE_PARAMETERS_FILENAME)
    p = np.loadtxt(p_file)

    ## init data base
    if kind.upper() == 'BOXES':
        data_base = simulation.util.data_base.init_data_base('WOA')
        f = data_base.f_boxes(p)
        f[f < 0] = 0
        if average_in_time:
            f = f.mean(axis=1)
    else:
        data_base = simulation.util.data_base.init_data_base('WOD')
        f = data_base.f(p)
        f = data_base.convert_to_boxes(f, no_data_value=np.inf)

    file = os.path.join(path, 'model_output_-_' + kind + '_-_' + parameter_set_dirname + '_-_{tracer}.png')
    tracers = ('dop', 'po4')
    for i in range(len(tracers)):
        util.plot.data(f[i], file.format(tracer=tracers[i]), land_value=np.nan, no_data_value=np.inf, v_min=0, v_max=y_max[i], contours=True, colorbar=False)