def _get_unnormalized_pressure(model_metadata_dict, example_id_strings): """Returns profiles of unnormalized pressure. E = number of examples H = number of heights :param model_metadata_dict: Dictionary read by `neural_net.read_metafile`. :param example_id_strings: length-E list of example IDs. :return: pressure_matrix_pascals: E-by-H numpy array of pressures. """ generator_option_dict = copy.deepcopy( model_metadata_dict[neural_net.TRAINING_OPTIONS_KEY]) generator_option_dict[neural_net.SCALAR_PREDICTOR_NAMES_KEY] = [ example_utils.LATITUDE_NAME, example_utils.LONGITUDE_NAME, example_utils.ZENITH_ANGLE_NAME ] generator_option_dict[neural_net.VECTOR_PREDICTOR_NAMES_KEY] = [ example_utils.PRESSURE_NAME ] generator_option_dict[neural_net.PREDICTOR_NORM_TYPE_KEY] = None predictor_matrix = neural_net.create_data_specific_examples( option_dict=generator_option_dict, net_type_string=model_metadata_dict[neural_net.NET_TYPE_KEY], example_id_strings=example_id_strings)[0] dummy_example_dict = { example_utils.SCALAR_PREDICTOR_NAMES_KEY: generator_option_dict[neural_net.SCALAR_PREDICTOR_NAMES_KEY], example_utils.VECTOR_PREDICTOR_NAMES_KEY: generator_option_dict[neural_net.VECTOR_PREDICTOR_NAMES_KEY], example_utils.HEIGHTS_KEY: generator_option_dict[neural_net.HEIGHTS_KEY] } example_dict = neural_net.predictors_numpy_to_dict( predictor_matrix=predictor_matrix, example_dict=dummy_example_dict, net_type_string=model_metadata_dict[neural_net.NET_TYPE_KEY]) return example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY][..., 0]
def test_predictors_numpy_to_dict_u_net(self): """Ensures correct output from predictors_numpy_to_dict. In this case, neural-net type is U-net. """ this_example_dict = neural_net.predictors_numpy_to_dict( predictor_matrix=U_NET_PREDICTOR_MATRIX, example_dict=EXAMPLE_DICT, net_type_string=neural_net.U_NET_TYPE_STRING) self.assertTrue( numpy.allclose( this_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY], VECTOR_PREDICTOR_MATRIX, atol=TOLERANCE)) self.assertTrue( numpy.allclose( this_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY], SCALAR_PREDICTOR_MATRIX, atol=TOLERANCE))
def prediction_function(predictor_matrix): """Prediction function itself. :param predictor_matrix: See doc for `run_forward_test_one_step`. :return: prediction_matrices: 1-D list of numpy arrays, each containing predicted values. """ net_type_string = model_metadata_dict[neural_net.NET_TYPE_KEY] if net_type_string == neural_net.DENSE_NET_TYPE_STRING: generator_option_dict = ( model_metadata_dict[neural_net.TRAINING_OPTIONS_KEY]) example_dict = { example_utils.SCALAR_PREDICTOR_NAMES_KEY: generator_option_dict[neural_net.SCALAR_PREDICTOR_NAMES_KEY], example_utils.VECTOR_PREDICTOR_NAMES_KEY: generator_option_dict[neural_net.VECTOR_PREDICTOR_NAMES_KEY], example_utils.HEIGHTS_KEY: generator_option_dict[neural_net.HEIGHTS_KEY] } new_example_dict = neural_net.predictors_numpy_to_dict( predictor_matrix=predictor_matrix, example_dict=example_dict, net_type_string=neural_net.CNN_TYPE_STRING) example_dict.update(new_example_dict) this_predictor_matrix = neural_net.predictors_dict_to_numpy( example_dict=example_dict, net_type_string=net_type_string)[0] else: this_predictor_matrix = predictor_matrix return neural_net.apply_model(model_object=model_object, predictor_matrix=this_predictor_matrix, num_examples_per_batch=1000, net_type_string=net_type_string, is_loss_constrained_mse=False, verbose=False)
def _run(model_file_name, example_file_name, num_examples, example_dir_name, example_id_file_name, layer_name, is_layer_output, neuron_indices, ideal_activation, output_file_name): """Makes saliency map for each example, according to one model. This is effectively the main method. :param model_file_name: See documentation at top of file. :param example_file_name: Same. :param num_examples: Same. :param example_dir_name: Same. :param example_id_file_name: Same. :param layer_name: Same. :param is_layer_output: Same. :param neuron_indices: Same. :param ideal_activation: Same. :param output_file_name: Same. """ print('Reading model from: "{0:s}"...'.format(model_file_name)) model_object = neural_net.read_model(model_file_name) metafile_name = neural_net.find_metafile( model_dir_name=os.path.split(model_file_name)[0], raise_error_if_missing=True ) print('Reading metadata from: "{0:s}"...'.format(metafile_name)) metadata_dict = neural_net.read_metafile(metafile_name) predictor_matrix, _, example_id_strings = ( misc_utils.get_examples_for_inference( model_metadata_dict=metadata_dict, example_file_name=example_file_name, num_examples=num_examples, example_dir_name=example_dir_name, example_id_file_name=example_id_file_name ) ) print(SEPARATOR_STRING) generator_option_dict = metadata_dict[neural_net.TRAINING_OPTIONS_KEY] if is_layer_output: dummy_example_dict = { example_utils.SCALAR_TARGET_NAMES_KEY: generator_option_dict[neural_net.SCALAR_TARGET_NAMES_KEY], example_utils.VECTOR_TARGET_NAMES_KEY: generator_option_dict[neural_net.VECTOR_TARGET_NAMES_KEY], example_utils.HEIGHTS_KEY: generator_option_dict[neural_net.HEIGHTS_KEY] } target_field_name, target_height_m_agl = ( neural_net.neuron_indices_to_target_var( neuron_indices=neuron_indices, example_dict=copy.deepcopy(dummy_example_dict), net_type_string=metadata_dict[neural_net.NET_TYPE_KEY] ) ) else: target_field_name = None target_height_m_agl = None print('Target field and height = {0:s}, {1:s}'.format( str(target_field_name), str(target_height_m_agl) )) print('Computing saliency for neuron {0:s} in layer "{1:s}"...'.format( str(neuron_indices), layer_name )) saliency_matrix = saliency.get_saliency_one_neuron( model_object=model_object, predictor_matrix=predictor_matrix, layer_name=layer_name, neuron_indices=neuron_indices, ideal_activation=ideal_activation ) net_type_string = metadata_dict[neural_net.NET_TYPE_KEY] if net_type_string == neural_net.DENSE_NET_TYPE_STRING: dummy_example_dict = { example_utils.SCALAR_PREDICTOR_NAMES_KEY: generator_option_dict[neural_net.SCALAR_PREDICTOR_NAMES_KEY], example_utils.VECTOR_PREDICTOR_NAMES_KEY: generator_option_dict[neural_net.VECTOR_PREDICTOR_NAMES_KEY], example_utils.HEIGHTS_KEY: generator_option_dict[neural_net.HEIGHTS_KEY] } dummy_example_dict = neural_net.predictors_numpy_to_dict( predictor_matrix=saliency_matrix, example_dict=dummy_example_dict, net_type_string=metadata_dict[neural_net.NET_TYPE_KEY] ) scalar_saliency_matrix = ( dummy_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY] ) vector_saliency_matrix = ( dummy_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY] ) else: num_scalar_predictors = len( generator_option_dict[neural_net.SCALAR_PREDICTOR_NAMES_KEY] ) scalar_saliency_matrix = saliency_matrix[..., -num_scalar_predictors:] vector_saliency_matrix = saliency_matrix[..., :-num_scalar_predictors] print('Writing saliency maps to: "{0:s}"...'.format(output_file_name)) saliency.write_file( netcdf_file_name=output_file_name, scalar_saliency_matrix=scalar_saliency_matrix, vector_saliency_matrix=vector_saliency_matrix, example_id_strings=example_id_strings, model_file_name=model_file_name, layer_name=layer_name, neuron_indices=neuron_indices, ideal_activation=ideal_activation, target_field_name=target_field_name, target_height_m_agl=target_height_m_agl )
def _run(model_file_name, example_file_name, num_examples, example_dir_name, example_id_file_name, ideal_activation, scalar_output_layer_name, vector_output_layer_name, output_file_name): """Makes saliency map for each example and target, according to one model. This is effectively the main method. :param model_file_name: See documentation at top of file. :param example_file_name: Same. :param num_examples: Same. :param example_dir_name: Same. :param example_id_file_name: Same. :param ideal_activation: Same. :param scalar_output_layer_name: Same. :param vector_output_layer_name: Same. :param output_file_name: Same. """ print('Reading model from: "{0:s}"...'.format(model_file_name)) model_object = neural_net.read_model(model_file_name) metafile_name = neural_net.find_metafile( model_dir_name=os.path.split(model_file_name)[0], raise_error_if_missing=True ) print('Reading metadata from: "{0:s}"...'.format(metafile_name)) metadata_dict = neural_net.read_metafile(metafile_name) predictor_matrix, _, example_id_strings = ( misc_utils.get_examples_for_inference( model_metadata_dict=metadata_dict, example_file_name=example_file_name, num_examples=num_examples, example_dir_name=example_dir_name, example_id_file_name=example_id_file_name ) ) print(SEPARATOR_STRING) net_type_string = metadata_dict[neural_net.NET_TYPE_KEY] generator_option_dict = metadata_dict[neural_net.TRAINING_OPTIONS_KEY] scalar_predictor_names = ( generator_option_dict[neural_net.SCALAR_PREDICTOR_NAMES_KEY] ) vector_predictor_names = ( generator_option_dict[neural_net.VECTOR_PREDICTOR_NAMES_KEY] ) scalar_target_names = ( generator_option_dict[neural_net.SCALAR_TARGET_NAMES_KEY] ) vector_target_names = ( generator_option_dict[neural_net.VECTOR_TARGET_NAMES_KEY] ) heights_m_agl = generator_option_dict[neural_net.HEIGHTS_KEY] num_scalar_predictors = len(scalar_predictor_names) num_vector_predictors = len(vector_predictor_names) num_scalar_targets = len(scalar_target_names) num_vector_targets = len(vector_target_names) num_heights = len(heights_m_agl) num_examples = len(example_id_strings) if net_type_string == neural_net.DENSE_NET_TYPE_STRING: saliency_matrix_scalar_p_scalar_t = numpy.full( (num_examples, num_scalar_predictors, num_scalar_targets), numpy.nan ) else: saliency_matrix_scalar_p_scalar_t = numpy.full( (num_examples, num_heights, num_scalar_predictors, num_scalar_targets), numpy.nan ) saliency_matrix_vector_p_scalar_t = numpy.full( (num_examples, num_heights, num_vector_predictors, num_scalar_targets), numpy.nan ) if net_type_string == neural_net.DENSE_NET_TYPE_STRING: saliency_matrix_scalar_p_vector_t = numpy.full( (num_examples, num_scalar_predictors, num_heights, num_vector_targets), numpy.nan ) else: saliency_matrix_scalar_p_vector_t = numpy.full( (num_examples, num_heights, num_scalar_predictors, num_heights, num_vector_targets), numpy.nan ) saliency_matrix_vector_p_vector_t = numpy.full( (num_examples, num_heights, num_vector_predictors, num_heights, num_vector_targets), numpy.nan ) dummy_example_dict = { example_utils.SCALAR_PREDICTOR_NAMES_KEY: scalar_predictor_names, example_utils.VECTOR_PREDICTOR_NAMES_KEY: vector_predictor_names, example_utils.SCALAR_TARGET_NAMES_KEY: scalar_target_names, example_utils.VECTOR_TARGET_NAMES_KEY: vector_target_names, example_utils.HEIGHTS_KEY: heights_m_agl } for k in range(num_scalar_targets): these_neuron_indices = neural_net.target_var_to_neuron_indices( example_dict=copy.deepcopy(dummy_example_dict), net_type_string=net_type_string, target_name=scalar_target_names[k] ) print('Computing saliency for "{0:s}"...'.format( scalar_target_names[k] )) this_saliency_matrix = saliency.get_saliency_one_neuron( model_object=model_object, predictor_matrix=predictor_matrix, layer_name=scalar_output_layer_name, neuron_indices=these_neuron_indices, ideal_activation=ideal_activation ) if net_type_string == neural_net.DENSE_NET_TYPE_STRING: new_example_dict = neural_net.predictors_numpy_to_dict( predictor_matrix=this_saliency_matrix, example_dict=copy.deepcopy(dummy_example_dict), net_type_string=net_type_string ) saliency_matrix_scalar_p_scalar_t[..., k] = ( new_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY] ) saliency_matrix_vector_p_scalar_t[..., k] = ( new_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY] ) else: saliency_matrix_scalar_p_scalar_t[..., k] = ( this_saliency_matrix[..., -num_scalar_predictors:] ) saliency_matrix_vector_p_scalar_t[..., k] = ( this_saliency_matrix[..., :-num_scalar_predictors] ) print(SEPARATOR_STRING) for k in range(num_vector_targets): for j in range(num_heights): these_neuron_indices = neural_net.target_var_to_neuron_indices( example_dict=copy.deepcopy(dummy_example_dict), net_type_string=net_type_string, target_name=vector_target_names[k], height_m_agl=heights_m_agl[j] ) print('Computing saliency for "{0:s}" at {1:d} m AGL...'.format( vector_target_names[k], int(numpy.round(heights_m_agl[j])) )) this_saliency_matrix = saliency.get_saliency_one_neuron( model_object=model_object, predictor_matrix=predictor_matrix, layer_name=vector_output_layer_name, neuron_indices=these_neuron_indices, ideal_activation=ideal_activation ) if net_type_string == neural_net.DENSE_NET_TYPE_STRING: new_example_dict = neural_net.predictors_numpy_to_dict( predictor_matrix=this_saliency_matrix, example_dict=copy.deepcopy(dummy_example_dict), net_type_string=net_type_string ) saliency_matrix_scalar_p_vector_t[..., j, k] = ( new_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY] ) saliency_matrix_vector_p_vector_t[..., j, k] = ( new_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY] ) else: saliency_matrix_scalar_p_vector_t[..., j, k] = ( this_saliency_matrix[..., -num_scalar_predictors:] ) saliency_matrix_vector_p_vector_t[..., j, k] = ( this_saliency_matrix[..., :-num_scalar_predictors] ) print(SEPARATOR_STRING) print('Writing saliency maps to: "{0:s}"...'.format(output_file_name)) saliency.write_all_targets_file( netcdf_file_name=output_file_name, saliency_matrix_scalar_p_scalar_t=saliency_matrix_scalar_p_scalar_t, saliency_matrix_vector_p_scalar_t=saliency_matrix_vector_p_scalar_t, saliency_matrix_scalar_p_vector_t=saliency_matrix_scalar_p_vector_t, saliency_matrix_vector_p_vector_t=saliency_matrix_vector_p_vector_t, example_id_strings=example_id_strings, model_file_name=model_file_name, ideal_activation=ideal_activation )
def _run(model_file_name, example_file_name, num_examples, example_dir_name, example_id_file_name, layer_name, neuron_indices, ideal_activation, num_iterations, learning_rate, l2_weight, output_file_name): """Runs backwards optimization. This is effectively the main method. :param model_file_name: See documentation at top of file. :param example_file_name: Same. :param num_examples: Same. :param example_dir_name: Same. :param example_id_file_name: Same. :param layer_name: Same. :param neuron_indices: Same. :param ideal_activation: Same. :param num_iterations: Same. :param learning_rate: Same. :param l2_weight: Same. :param output_file_name: Same. """ print('Reading model from: "{0:s}"...'.format(model_file_name)) model_object = neural_net.read_model(model_file_name) metafile_name = neural_net.find_metafile( model_dir_name=os.path.split(model_file_name)[0], raise_error_if_missing=True ) print('Reading metadata from: "{0:s}"...'.format(metafile_name)) metadata_dict = neural_net.read_metafile(metafile_name) predictor_matrix, _, example_id_strings = ( misc_utils.get_examples_for_inference( model_metadata_dict=metadata_dict, example_file_name=example_file_name, num_examples=num_examples, example_dir_name=example_dir_name, example_id_file_name=example_id_file_name ) ) print(SEPARATOR_STRING) generator_option_dict = metadata_dict[neural_net.TRAINING_OPTIONS_KEY] normalization_file_name = ( generator_option_dict[neural_net.NORMALIZATION_FILE_KEY] ) print(( 'Reading training examples (for normalization) from: "{0:s}"...' ).format( normalization_file_name )) training_example_dict = example_io.read_file(normalization_file_name) training_example_dict = example_utils.subset_by_height( example_dict=training_example_dict, heights_m_agl=generator_option_dict[neural_net.HEIGHTS_KEY] ) num_examples = len(example_id_strings) bwo_dict = None for i in range(num_examples): this_bwo_dict = bwo.optimize_input_for_neuron( model_object=model_object, init_function_or_matrix=predictor_matrix[i, ...], layer_name=layer_name, neuron_indices=neuron_indices, ideal_activation=ideal_activation, num_iterations=num_iterations, learning_rate=learning_rate, l2_weight=l2_weight ) if i == num_examples - 1: print(SEPARATOR_STRING) else: print(MINOR_SEPARATOR_STRING) if bwo_dict is None: these_dim = numpy.array( (num_examples,) + this_bwo_dict[bwo.INITIAL_PREDICTORS_KEY].shape[1:], dtype=int ) bwo_dict = { bwo.INITIAL_PREDICTORS_KEY: numpy.full(these_dim, numpy.nan), bwo.FINAL_PREDICTORS_KEY: numpy.full(these_dim, numpy.nan), bwo.INITIAL_ACTIVATIONS_KEY: numpy.full(num_examples, numpy.nan), bwo.FINAL_ACTIVATIONS_KEY: numpy.full(num_examples, numpy.nan) } bwo_dict[bwo.INITIAL_PREDICTORS_KEY][i, ...] = ( this_bwo_dict[bwo.INITIAL_PREDICTORS_KEY][0, ...] ) bwo_dict[bwo.FINAL_PREDICTORS_KEY][i, ...] = ( this_bwo_dict[bwo.FINAL_PREDICTORS_KEY][0, ...] ) bwo_dict[bwo.INITIAL_ACTIVATIONS_KEY][i] = ( this_bwo_dict[bwo.INITIAL_ACTIVATION_KEY] ) bwo_dict[bwo.FINAL_ACTIVATIONS_KEY][i] = ( this_bwo_dict[bwo.FINAL_ACTIVATION_KEY] ) if example_file_name == '': example_file_name = example_io.find_many_files( directory_name=example_dir_name, first_time_unix_sec=0, last_time_unix_sec=int(1e12), raise_error_if_any_missing=False, raise_error_if_all_missing=True )[0] first_example_dict = example_io.read_file(example_file_name) first_example_dict = example_utils.subset_by_height( example_dict=first_example_dict, heights_m_agl=generator_option_dict[neural_net.HEIGHTS_KEY] ) net_type_string = metadata_dict[neural_net.NET_TYPE_KEY] init_example_dict = copy.deepcopy(first_example_dict) this_example_dict = neural_net.predictors_numpy_to_dict( predictor_matrix=bwo_dict[bwo.INITIAL_PREDICTORS_KEY], example_dict=init_example_dict, net_type_string=net_type_string ) init_example_dict.update(this_example_dict) if generator_option_dict[neural_net.PREDICTOR_NORM_TYPE_KEY] is not None: init_example_dict = normalization.denormalize_data( new_example_dict=init_example_dict, training_example_dict=training_example_dict, normalization_type_string= generator_option_dict[neural_net.PREDICTOR_NORM_TYPE_KEY], min_normalized_value= generator_option_dict[neural_net.PREDICTOR_MIN_NORM_VALUE_KEY], max_normalized_value= generator_option_dict[neural_net.PREDICTOR_MAX_NORM_VALUE_KEY], separate_heights=True, apply_to_predictors=True, apply_to_vector_targets=False, apply_to_scalar_targets=False ) init_scalar_predictor_matrix = ( init_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY] ) init_vector_predictor_matrix = ( init_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY] ) final_example_dict = copy.deepcopy(first_example_dict) this_example_dict = neural_net.predictors_numpy_to_dict( predictor_matrix=bwo_dict[bwo.FINAL_PREDICTORS_KEY], example_dict=final_example_dict, net_type_string=net_type_string ) final_example_dict.update(this_example_dict) if generator_option_dict[neural_net.PREDICTOR_NORM_TYPE_KEY] is not None: final_example_dict = normalization.denormalize_data( new_example_dict=final_example_dict, training_example_dict=training_example_dict, normalization_type_string= generator_option_dict[neural_net.PREDICTOR_NORM_TYPE_KEY], min_normalized_value= generator_option_dict[neural_net.PREDICTOR_MIN_NORM_VALUE_KEY], max_normalized_value= generator_option_dict[neural_net.PREDICTOR_MAX_NORM_VALUE_KEY], separate_heights=True, apply_to_predictors=True, apply_to_vector_targets=False, apply_to_scalar_targets=False ) final_scalar_predictor_matrix = ( final_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY] ) final_vector_predictor_matrix = ( final_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY] ) print('Writing results to file: "{0:s}"...'.format(output_file_name)) bwo.write_file( netcdf_file_name=output_file_name, init_scalar_predictor_matrix=init_scalar_predictor_matrix, final_scalar_predictor_matrix=final_scalar_predictor_matrix, init_vector_predictor_matrix=init_vector_predictor_matrix, final_vector_predictor_matrix=final_vector_predictor_matrix, initial_activations=bwo_dict[bwo.INITIAL_ACTIVATIONS_KEY], final_activations=bwo_dict[bwo.FINAL_ACTIVATIONS_KEY], example_id_strings=example_id_strings, model_file_name=model_file_name, layer_name=layer_name, neuron_indices=neuron_indices, ideal_activation=ideal_activation, num_iterations=num_iterations, learning_rate=learning_rate, l2_weight=l2_weight )
def run_backwards_test(predictor_matrix, target_matrices, model_object, model_metadata_dict, cost_function, shuffle_profiles_together=True, num_bootstrap_reps=DEFAULT_NUM_BOOTSTRAP_REPS): """Runs backwards version of permutation test (both single- and multi-pass). B = number of bootstrap replicates N = number of predictors (either channels or height/channel pairs) available to permute :param predictor_matrix: See doc for `run_forward_test`. :param target_matrices: Same. :param model_object: Same. :param model_metadata_dict: Same. :param cost_function: Same. :param shuffle_profiles_together: Same. :param num_bootstrap_reps: Same. :return: result_dict: Dictionary with the following keys. result_dict['orig_cost_estimates']: length-B numpy array with estimates of original cost (before *de*permutation). result_dict['best_predictor_names']: length-N list with best predictor at each step. result_dict['best_heights_m_agl']: length-N numpy array of corresponding heights (metres above ground level). This may be None. result_dict['best_cost_matrix']: N-by-B numpy array of costs after *de*permutation at each step. result_dict['step1_predictor_names']: length-N list with predictors in order that they were *de*permuted in step 1. result_dict['step1_heights_m_agl']: length-N numpy array of corresponding heights (metres above ground level). This may be None. result_dict['step1_cost_matrix']: N-by-B numpy array of costs after *de*permutation in step 1. result_dict['is_backwards_test']: Boolean flag (always True for this method). """ error_checking.assert_is_boolean(shuffle_profiles_together) error_checking.assert_is_integer(num_bootstrap_reps) num_bootstrap_reps = numpy.maximum(num_bootstrap_reps, 1) generator_option_dict = model_metadata_dict[ neural_net.TRAINING_OPTIONS_KEY] example_dict = { example_utils.SCALAR_PREDICTOR_NAMES_KEY: generator_option_dict[neural_net.SCALAR_PREDICTOR_NAMES_KEY], example_utils.VECTOR_PREDICTOR_NAMES_KEY: generator_option_dict[neural_net.VECTOR_PREDICTOR_NAMES_KEY], example_utils.HEIGHTS_KEY: generator_option_dict[neural_net.HEIGHTS_KEY] } new_example_dict = neural_net.predictors_numpy_to_dict( predictor_matrix=predictor_matrix, example_dict=example_dict, net_type_string=model_metadata_dict[neural_net.NET_TYPE_KEY]) example_dict.update(new_example_dict) predictor_matrix, predictor_name_matrix, height_matrix_m_agl = ( neural_net.predictors_dict_to_numpy( example_dict=example_dict, net_type_string=neural_net.CNN_TYPE_STRING)) scalar_channel_flags = numpy.isnan(height_matrix_m_agl[0, :]) prediction_function = _make_prediction_function( model_object=model_object, model_metadata_dict=model_metadata_dict) # Permute all predictors. num_channels = predictor_matrix.shape[-1] if shuffle_profiles_together: num_heights = 1 else: num_heights = predictor_matrix.shape[-2] clean_predictor_matrix = predictor_matrix + 0. for j in range(num_heights): for k in range(num_channels): predictor_matrix = _permute_values( predictor_matrix=predictor_matrix, channel_index=k, height_index=None if shuffle_profiles_together else j)[0] # Find original cost (before *de*permutation). print('Finding original cost (before *de*permutation)...') orig_cost_estimates = _bootstrap_cost( target_matrices=target_matrices, prediction_matrices=prediction_function(predictor_matrix), cost_function=cost_function, num_replicates=num_bootstrap_reps) # Do dirty work. permuted_flag_matrix = numpy.full(predictor_matrix.shape[1:], True, dtype=bool) best_predictor_names = [] best_cost_matrix = numpy.full((0, num_bootstrap_reps), numpy.nan) if shuffle_profiles_together: best_heights_m_agl = None else: best_heights_m_agl = [] step1_predictor_names = None step1_heights_m_agl = None step1_cost_matrix = None step_num = 0 while True: print(MINOR_SEPARATOR_STRING) step_num += 1 this_result_dict = run_backwards_test_one_step( predictor_matrix=predictor_matrix, clean_predictor_matrix=clean_predictor_matrix, target_matrices=target_matrices, prediction_function=prediction_function, cost_function=cost_function, permuted_flag_matrix=permuted_flag_matrix, scalar_channel_flags=scalar_channel_flags, shuffle_profiles_together=shuffle_profiles_together, num_bootstrap_reps=num_bootstrap_reps) if this_result_dict is None: break predictor_matrix = this_result_dict[PREDICTORS_KEY] permuted_flag_matrix = this_result_dict[PERMUTED_FLAGS_KEY] these_predictor_names, these_heights_m_agl = ( _predictor_indices_to_metadata( all_predictor_name_matrix=predictor_name_matrix, all_height_matrix_m_agl=height_matrix_m_agl, one_step_result_dict=this_result_dict)) this_best_index = numpy.argmin( numpy.mean(this_result_dict[DEPERMUTED_COSTS_KEY], axis=1)) best_predictor_names.append(these_predictor_names[this_best_index]) best_cost_matrix = numpy.concatenate( (best_cost_matrix, this_result_dict[DEPERMUTED_COSTS_KEY][[this_best_index], :]), axis=0) log_string = 'Best predictor at {0:d}th step = {1:s}'.format( step_num, best_predictor_names[-1]) if not shuffle_profiles_together: best_heights_m_agl.append(these_heights_m_agl[this_best_index]) log_string += ' at {0:s} m AGL'.format(best_heights_m_agl[-1]) log_string += ' ... cost = {0:f}'.format( numpy.mean(best_cost_matrix[-1, :])) print(log_string) if step_num != 1: continue step1_predictor_names = copy.deepcopy(these_predictor_names) step1_cost_matrix = this_result_dict[DEPERMUTED_COSTS_KEY] + 0. if not shuffle_profiles_together: step1_heights_m_agl = these_heights_m_agl + 0 return { ORIGINAL_COST_KEY: orig_cost_estimates, BEST_PREDICTORS_KEY: best_predictor_names, BEST_HEIGHTS_KEY: best_heights_m_agl, BEST_COSTS_KEY: best_cost_matrix, STEP1_PREDICTORS_KEY: step1_predictor_names, STEP1_HEIGHTS_KEY: step1_heights_m_agl, STEP1_COSTS_KEY: step1_cost_matrix, BACKWARDS_FLAG_KEY: True }