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])
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)
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)
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)
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])
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)