<commit_before>"""Plots examples (storm objects) with surrounding context.

Specifically, for each storm object, this script plots surrounding radar data
and tornado reports for near-past and near-future times.
"""

import copy
import os.path
import argparse
from itertools import chain
import numpy
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as pyplot
from gewittergefahr.gg_io import myrorss_and_mrms_io
from gewittergefahr.gg_io import storm_tracking_io as tracking_io
from gewittergefahr.gg_utils import radar_utils
from gewittergefahr.gg_utils import linkage
from gewittergefahr.gg_utils import time_conversion
from gewittergefahr.gg_utils import target_val_utils
from gewittergefahr.gg_utils import echo_top_tracking
from gewittergefahr.gg_utils import temporal_tracking
from gewittergefahr.gg_utils import radar_sparse_to_full as radar_s2f
from gewittergefahr.gg_utils import storm_tracking_utils as tracking_utils
from gewittergefahr.gg_utils import file_system_utils
from gewittergefahr.deep_learning import cnn
from gewittergefahr.deep_learning import model_activation
from gewittergefahr.deep_learning import model_interpretation
from gewittergefahr.deep_learning import training_validation_io as trainval_io
from gewittergefahr.plotting import plotting_utils
from gewittergefahr.plotting import radar_plotting
from gewittergefahr.plotting import storm_plotting
from gewittergefahr.plotting import imagemagick_utils

# TODO(thunderhoser): Deal with both tornado occurrence and genesis as target
# variables.

TIME_INTERVAL_SECONDS = 300
TIME_FORMAT = '%Y-%m-%d-%H%M%S'
SEPARATOR_STRING = '\n\n' + '*' * 50 + '\n\n'

TORNADO_MARKER_TYPE = '^'
TORNADO_MARKER_SIZE = 16
TORNADO_MARKER_EDGE_WIDTH = 1
TORNADO_MARKER_COLOUR = numpy.full(3, 0.)

TITLE_FONT_SIZE = 16
FONT_SIZE = 16

NUM_PARALLELS = 8
NUM_MERIDIANS = 6
BORDER_COLOUR = numpy.full(3, 0.)
FIGURE_RESOLUTION_DPI = 300

ACTIVATION_FILE_ARG_NAME = 'input_activation_file_name'
TORNADO_DIR_ARG_NAME = 'input_tornado_dir_name'
TRACKING_DIR_ARG_NAME = 'input_tracking_dir_name'
MYRORSS_DIR_ARG_NAME = 'input_myrorss_dir_name'
RADAR_FIELD_ARG_NAME = 'radar_field_name'
RADAR_HEIGHT_ARG_NAME = 'radar_height_m_asl'
LATITUDE_BUFFER_ARG_NAME = 'latitude_buffer_deg'
LONGITUDE_BUFFER_ARG_NAME = 'longitude_buffer_deg'
TIME_BUFFER_ARG_NAME = 'time_buffer_seconds'
OUTPUT_DIR_ARG_NAME = 'output_dir_name'

ACTIVATION_FILE_HELP_STRING = (
    'Path to activation file, containing model predictions for one or more '
    'examples (storm objects).  Will be read by `model_activation.read_file`.')

TORNADO_DIR_HELP_STRING = (
    'Name of directory with tornado reports.  Files therein will be found by '
    '`tornado_io.find_processed_file` and read by '
    '`tornado_io.read_processed_file`.')

TRACKING_DIR_HELP_STRING = (
    'Name of top-level directory with storm-tracking files.  Files therein will'
    ' be found by `storm_tracking_io.find_file` and read by '
    '`storm_tracking_io.read_file`.')

MYRORSS_DIR_HELP_STRING = (
    'Name of top-level directory with MYRORSS-formatted radar data.  Files '
    'therein will be found by `myrorss_and_mrms_io.find_raw_file` and read by '
    '`myrorss_and_mrms_io.read_data_from_sparse_grid_file`.')

RADAR_FIELD_HELP_STRING = (
    'Will plot this radar field around storm object.  Must be accepted by '
    '`radar_utils.check_field_name`.')

RADAR_HEIGHT_HELP_STRING = (
    'Height of radar field (required only if field is "{0:s}").'
).format(radar_utils.REFL_NAME)

LATITUDE_BUFFER_HELP_STRING = (
    'Latitude buffer (deg N).  Will plot this much latitude around the edge of '
    'the storm object at each time step.')

LONGITUDE_BUFFER_HELP_STRING = (
    'Longitude buffer (deg E).  Will plot this much longitude around the edge '
    'of the storm object at each time step.')

TIME_BUFFER_HELP_STRING = (
    'Time buffer.  Will plot radar fields for this many seconds before and '
    'after storm object.')

OUTPUT_DIR_HELP_STRING = (
    'Name of output directory.  Figures will be saved here.')

DEFAULT_TORNADO_DIR_NAME = (
    '/condo/swatwork/ralager/tornado_observations/processed')
DEFAULT_TRACKING_DIR_NAME = (
    '/condo/swatcommon/common/gridrad_final/myrorss_format/new_tracks/'
    'reanalyzed')
DEFAULT_MYRORSS_DIR_NAME = (
    '/condo/swatcommon/common/gridrad_final/myrorss_format')

INPUT_ARG_PARSER = argparse.ArgumentParser()
INPUT_ARG_PARSER.add_argument(
    '--' + ACTIVATION_FILE_ARG_NAME, type=str, required=True,
    help=ACTIVATION_FILE_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + TORNADO_DIR_ARG_NAME, type=str, required=False,
    default=DEFAULT_TORNADO_DIR_NAME, help=TORNADO_DIR_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + TRACKING_DIR_ARG_NAME, type=str, required=False,
    default=DEFAULT_TRACKING_DIR_NAME, help=TRACKING_DIR_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + MYRORSS_DIR_ARG_NAME, type=str, required=False,
    default=DEFAULT_MYRORSS_DIR_NAME, help=MYRORSS_DIR_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + RADAR_FIELD_ARG_NAME, type=str, required=False,
    default=radar_utils.ECHO_TOP_40DBZ_NAME, help=RADAR_FIELD_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + RADAR_HEIGHT_ARG_NAME, type=str, required=False,
    default=-1, help=RADAR_HEIGHT_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + LATITUDE_BUFFER_ARG_NAME, type=float, required=False,
    default=0.5, help=LATITUDE_BUFFER_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + LONGITUDE_BUFFER_ARG_NAME, type=float, required=False,
    default=0.5, help=LONGITUDE_BUFFER_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + TIME_BUFFER_ARG_NAME, type=int, required=False,
    default=1800, help=TIME_BUFFER_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + OUTPUT_DIR_ARG_NAME, type=str, required=True,
    help=OUTPUT_DIR_HELP_STRING)


def _get_plotting_limits(storm_object_table, latitude_buffer_deg,
                         longitude_buffer_deg):
    """Returns plotting limits (lat-long box).

    :param storm_object_table: See doc for `_plot_one_example_one_time`.
    :param latitude_buffer_deg: See documentation at top of file.
    :param longitude_buffer_deg: Same.
    :return: latitude_limits_deg: length-2 numpy array with [min, max]
        latitudes in deg N.
    :return: longitude_limits_deg: length-2 numpy array with [min, max]
        longitudes in deg E.
    """

    vertex_latitudes_deg_2d_list = [
        p.exterior.xy[1] for p in
        storm_object_table[tracking_utils.LATLNG_POLYGON_COLUMN].values
    ]

    vertex_longitudes_deg_2d_list = [
        p.exterior.xy[0] for p in
        storm_object_table[tracking_utils.LATLNG_POLYGON_COLUMN].values
    ]

    print(vertex_latitudes_deg_2d_list)
    print(type(vertex_latitudes_deg_2d_list))

    vertex_latitudes_deg = numpy.array(chain(vertex_latitudes_deg_2d_list))
    vertex_longitudes_deg = numpy.array(chain(vertex_longitudes_deg_2d_list))

    min_plot_latitude_deg = (
        numpy.min(vertex_latitudes_deg) - latitude_buffer_deg
    )
    max_plot_latitude_deg = (
        numpy.max(vertex_latitudes_deg) + latitude_buffer_deg
    )
    min_plot_longitude_deg = (
        numpy.min(vertex_longitudes_deg) - longitude_buffer_deg
    )
    max_plot_longitude_deg = (
        numpy.max(vertex_longitudes_deg) + longitude_buffer_deg
    )

    return (
        numpy.array([min_plot_latitude_deg, max_plot_latitude_deg]),
        numpy.array([min_plot_longitude_deg, max_plot_longitude_deg])
    )
<commit_msg>Added debugging print statement to plot_examples_with_context.py.<commit_after>"""Plots examples (storm objects) with surrounding context.

Specifically, for each storm object, this script plots surrounding radar data
and tornado reports for near-past and near-future times.
"""

import copy
import os.path
import argparse
from itertools import chain
import numpy
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as pyplot
from gewittergefahr.gg_io import myrorss_and_mrms_io
from gewittergefahr.gg_io import storm_tracking_io as tracking_io
from gewittergefahr.gg_utils import radar_utils
from gewittergefahr.gg_utils import linkage
from gewittergefahr.gg_utils import time_conversion
from gewittergefahr.gg_utils import target_val_utils
from gewittergefahr.gg_utils import echo_top_tracking
from gewittergefahr.gg_utils import temporal_tracking
from gewittergefahr.gg_utils import radar_sparse_to_full as radar_s2f
from gewittergefahr.gg_utils import storm_tracking_utils as tracking_utils
from gewittergefahr.gg_utils import file_system_utils
from gewittergefahr.deep_learning import cnn
from gewittergefahr.deep_learning import model_activation
from gewittergefahr.deep_learning import model_interpretation
from gewittergefahr.deep_learning import training_validation_io as trainval_io
from gewittergefahr.plotting import plotting_utils
from gewittergefahr.plotting import radar_plotting
from gewittergefahr.plotting import storm_plotting
from gewittergefahr.plotting import imagemagick_utils

# TODO(thunderhoser): Deal with both tornado occurrence and genesis as target
# variables.

TIME_INTERVAL_SECONDS = 300
TIME_FORMAT = '%Y-%m-%d-%H%M%S'
SEPARATOR_STRING = '\n\n' + '*' * 50 + '\n\n'

TORNADO_MARKER_TYPE = '^'
TORNADO_MARKER_SIZE = 16
TORNADO_MARKER_EDGE_WIDTH = 1
TORNADO_MARKER_COLOUR = numpy.full(3, 0.)

TITLE_FONT_SIZE = 16
FONT_SIZE = 16

NUM_PARALLELS = 8
NUM_MERIDIANS = 6
BORDER_COLOUR = numpy.full(3, 0.)
FIGURE_RESOLUTION_DPI = 300

ACTIVATION_FILE_ARG_NAME = 'input_activation_file_name'
TORNADO_DIR_ARG_NAME = 'input_tornado_dir_name'
TRACKING_DIR_ARG_NAME = 'input_tracking_dir_name'
MYRORSS_DIR_ARG_NAME = 'input_myrorss_dir_name'
RADAR_FIELD_ARG_NAME = 'radar_field_name'
RADAR_HEIGHT_ARG_NAME = 'radar_height_m_asl'
LATITUDE_BUFFER_ARG_NAME = 'latitude_buffer_deg'
LONGITUDE_BUFFER_ARG_NAME = 'longitude_buffer_deg'
TIME_BUFFER_ARG_NAME = 'time_buffer_seconds'
OUTPUT_DIR_ARG_NAME = 'output_dir_name'

ACTIVATION_FILE_HELP_STRING = (
    'Path to activation file, containing model predictions for one or more '
    'examples (storm objects).  Will be read by `model_activation.read_file`.')

TORNADO_DIR_HELP_STRING = (
    'Name of directory with tornado reports.  Files therein will be found by '
    '`tornado_io.find_processed_file` and read by '
    '`tornado_io.read_processed_file`.')

TRACKING_DIR_HELP_STRING = (
    'Name of top-level directory with storm-tracking files.  Files therein will'
    ' be found by `storm_tracking_io.find_file` and read by '
    '`storm_tracking_io.read_file`.')

MYRORSS_DIR_HELP_STRING = (
    'Name of top-level directory with MYRORSS-formatted radar data.  Files '
    'therein will be found by `myrorss_and_mrms_io.find_raw_file` and read by '
    '`myrorss_and_mrms_io.read_data_from_sparse_grid_file`.')

RADAR_FIELD_HELP_STRING = (
    'Will plot this radar field around storm object.  Must be accepted by '
    '`radar_utils.check_field_name`.')

RADAR_HEIGHT_HELP_STRING = (
    'Height of radar field (required only if field is "{0:s}").'
).format(radar_utils.REFL_NAME)

LATITUDE_BUFFER_HELP_STRING = (
    'Latitude buffer (deg N).  Will plot this much latitude around the edge of '
    'the storm object at each time step.')

LONGITUDE_BUFFER_HELP_STRING = (
    'Longitude buffer (deg E).  Will plot this much longitude around the edge '
    'of the storm object at each time step.')

TIME_BUFFER_HELP_STRING = (
    'Time buffer.  Will plot radar fields for this many seconds before and '
    'after storm object.')

OUTPUT_DIR_HELP_STRING = (
    'Name of output directory.  Figures will be saved here.')

DEFAULT_TORNADO_DIR_NAME = (
    '/condo/swatwork/ralager/tornado_observations/processed')
DEFAULT_TRACKING_DIR_NAME = (
    '/condo/swatcommon/common/gridrad_final/myrorss_format/new_tracks/'
    'reanalyzed')
DEFAULT_MYRORSS_DIR_NAME = (
    '/condo/swatcommon/common/gridrad_final/myrorss_format')

INPUT_ARG_PARSER = argparse.ArgumentParser()
INPUT_ARG_PARSER.add_argument(
    '--' + ACTIVATION_FILE_ARG_NAME, type=str, required=True,
    help=ACTIVATION_FILE_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + TORNADO_DIR_ARG_NAME, type=str, required=False,
    default=DEFAULT_TORNADO_DIR_NAME, help=TORNADO_DIR_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + TRACKING_DIR_ARG_NAME, type=str, required=False,
    default=DEFAULT_TRACKING_DIR_NAME, help=TRACKING_DIR_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + MYRORSS_DIR_ARG_NAME, type=str, required=False,
    default=DEFAULT_MYRORSS_DIR_NAME, help=MYRORSS_DIR_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + RADAR_FIELD_ARG_NAME, type=str, required=False,
    default=radar_utils.ECHO_TOP_40DBZ_NAME, help=RADAR_FIELD_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + RADAR_HEIGHT_ARG_NAME, type=str, required=False,
    default=-1, help=RADAR_HEIGHT_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + LATITUDE_BUFFER_ARG_NAME, type=float, required=False,
    default=0.5, help=LATITUDE_BUFFER_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + LONGITUDE_BUFFER_ARG_NAME, type=float, required=False,
    default=0.5, help=LONGITUDE_BUFFER_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + TIME_BUFFER_ARG_NAME, type=int, required=False,
    default=1800, help=TIME_BUFFER_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + OUTPUT_DIR_ARG_NAME, type=str, required=True,
    help=OUTPUT_DIR_HELP_STRING)


def _get_plotting_limits(storm_object_table, latitude_buffer_deg,
                         longitude_buffer_deg):
    """Returns plotting limits (lat-long box).

    :param storm_object_table: See doc for `_plot_one_example_one_time`.
    :param latitude_buffer_deg: See documentation at top of file.
    :param longitude_buffer_deg: Same.
    :return: latitude_limits_deg: length-2 numpy array with [min, max]
        latitudes in deg N.
    :return: longitude_limits_deg: length-2 numpy array with [min, max]
        longitudes in deg E.
    """

    vertex_latitudes_deg_2d_list = [
        p.exterior.xy[1] for p in
        storm_object_table[tracking_utils.LATLNG_POLYGON_COLUMN].values
    ]

    vertex_longitudes_deg_2d_list = [
        p.exterior.xy[0] for p in
        storm_object_table[tracking_utils.LATLNG_POLYGON_COLUMN].values
    ]

    print(vertex_latitudes_deg_2d_list)
    print(type(vertex_latitudes_deg_2d_list))

    vertex_latitudes_deg = numpy.array(chain(vertex_latitudes_deg_2d_list))
    vertex_longitudes_deg = numpy.array(chain(vertex_longitudes_deg_2d_list))

    print(vertex_latitudes_deg)
    print(type(vertex_latitudes_deg))

    min_plot_latitude_deg = (
        numpy.min(vertex_latitudes_deg) - latitude_buffer_deg
    )
    max_plot_latitude_deg = (
        numpy.max(vertex_latitudes_deg) + latitude_buffer_deg
    )
    min_plot_longitude_deg = (
        numpy.min(vertex_longitudes_deg) - longitude_buffer_deg
    )
    max_plot_longitude_deg = (
        numpy.max(vertex_longitudes_deg) + longitude_buffer_deg
    )

    return (
        numpy.array([min_plot_latitude_deg, max_plot_latitude_deg]),
        numpy.array([min_plot_longitude_deg, max_plot_longitude_deg])
    )

{'fraction_non_alphanumeric': 0.07824540604623592, 'fraction_numerical': 0.005795956003424883, 'mean_word_length': 5.144880615135572, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1785679', 'n_tokens_mistral': 5573, 'n_tokens_neox': 5387, 'n_words': 1031}