Example #1
0
    def test_plot_confidence_interval(self):
        """
        Plots a contour's confidence interval.
        """
        dataset_d_v, dataset_d_hs, label_v, label_hs = \
            read_ecbenchmark_dataset('datasets/1year_dataset_D.txt')

        # Read the contours that have beem computed previously from csv files.
        folder_name = 'contour-coordinates/'
        file_name_median = 'doe_john_years_25_median.txt'
        file_name_bottom = 'doe_john_years_25_bottom.txt'
        file_name_upper = 'doe_john_years_25_upper.txt'
        (contour_v_median,
         contour_hs_median) = read_contour(folder_name + file_name_median)
        (contour_v_bottom,
         contour_hs_bottom) = read_contour(folder_name + file_name_bottom)
        (contour_v_upper,
         contour_hs_upper) = read_contour(folder_name + file_name_upper)

        # Plot the sample, the median contour and the confidence interval.
        fig = plt.figure(figsize=(5, 5), dpi=150)
        ax = fig.add_subplot(111)
        plotted_sample = SamplePlotData(x=np.asarray(dataset_d_v),
                                        y=np.asarray(dataset_d_hs),
                                        ax=ax,
                                        label='dataset D')
        contour_labels = [
            '50th percentile contour', '2.5th percentile contour',
            '97.5th percentile contour'
        ]
        plot_confidence_interval(x_median=contour_v_median,
                                 y_median=contour_hs_median,
                                 x_bottom=contour_v_bottom,
                                 y_bottom=contour_hs_bottom,
                                 x_upper=contour_v_upper,
                                 y_upper=contour_hs_upper,
                                 ax=ax,
                                 x_label=label_v,
                                 y_label=label_hs,
                                 contour_labels=contour_labels,
                                 plotted_sample=plotted_sample)
Example #2
0
 def test_read_write_contour(self):
     """
     Read a contour, then writes this contour to a new file.
     """
     folder_name = 'contour-coordinates/'
     file_name_median = 'doe_john_years_25_median.txt'
     (contour_v_median,
      contour_hs_median) = read_contour(folder_name + file_name_median)
     new_file_path = folder_name + 'test_contour.txt'
     write_contour(contour_v_median, contour_hs_median, new_file_path,
                   'Wind speed (m/s)', 'Significant wave height (m)')
     os.remove(new_file_path)
Example #3
0
        contours_hs = []
        contours_tz = []
        max_hs_on_contour = np.empty(n_contours_to_analyze)
        for i in range(n_contours_to_analyze):
            contribution_nr = i + 1
            if 11 >= contribution_nr >= 9:
                contribution_nr = 9
            elif contribution_nr > 11:
                # Because contribution 9 holds 3 sets of contours.
                contribution_nr = contribution_nr - 2
            folder_name = 'results/exercise-1/contribution-' + str(
                contribution_nr)
            file_name = folder_name + '/' + lastname_firstname[i] + '_dataset_' + \
                        dataset_char + '_' + str(return_period) + '.txt'
            (hs, tz) = read_contour(file_name)
            if i in (7, 8, 9, 10):
                (tz, hs) = read_contour(file_name)
            contours_hs.append(hs)
            contours_tz.append(tz)
            max_hs_on_contour[i] = max(hs[~np.isnan(tz)])

        # Plot the data and the contour.
        ax1.scatter(tz_p, hs_p, c='black', alpha=0.5, zorder=-2)
        ax1.scatter(tz_r,
                    hs_r,
                    marker='v',
                    facecolor='None',
                    edgecolor='black',
                    alpha=0.5,
                    zorder=-2)
contours = []
contours_v = []
contours_hs = []
max_hs_on_contours = np.empty(n_contours_to_analyze)
for i in range(n_contours_to_analyze):
    contribution_nr = i + 1
    if 11 >= contribution_nr >= 9:
        contribution_nr = 9
    elif contribution_nr > 11:
        # Because contribution 9 holds 3 sets of contours.
        contribution_nr = contribution_nr - 2
    folder_name = 'results/exercise-1/contribution-' + str(contribution_nr)
    file_name = folder_name + '/' + lastname_firstname[i] + '_dataset_' + \
                dataset_char + '_' + str(return_period) + '.txt'
    if contribution_nr in (1, 2, 3, 5, 6, 8, 10):
        (hs, v) = read_contour(file_name)
    else:
        (v, hs) = read_contour(file_name)
    contour = Object()
    contour.c = (np.append(v, v[0]), np.append(hs, hs[0]))
    contours.append(contour)
    contours_v.append(v)
    contours_hs.append(hs)
    max_hs_on_contours[i] = max(hs[~np.isnan(hs)])

# Compute the min, max and median contour.
# First, define the origin and angles based on normalization.
v0 = np.mean(v_p)
hs0 = np.mean(hs_p)
angle_step_for_ci = 2
theta_stars = np.arange(0, 360, angle_step_for_ci) / 180 * np.pi
]

slengths = [1, 5, 25]
ylims = [18, 18, 18]

fig, axs = plt.subplots(4, 3, sharex=True, sharey=True, figsize=(8.5, 11))

for idx, (axi, name, style, leg_str,
          num) in enumerate(zip(axs, names, styles, leg_strs, nums)):
    for (ax, slength, ylim) in zip(axi, slengths, ylims):
        folder_name = 'results/exercise-2/contribution-' + str(num)
        temp = folder_name + '/' + name + '_years_' + str(slength)
        file_name_median = temp + '_median.txt'
        file_name_lower = temp + '_bottom.txt'
        file_name_upper = temp + '_upper.txt'
        v_median, hs_median = read_contour(file_name_median)
        v_lower, hs_lower = read_contour(file_name_lower)
        v_upper, hs_upper = read_contour(file_name_upper)

        sample_plot_data = SamplePlotData(x=np.asarray(sample_v),
                                          y=np.asarray(sample_hs),
                                          ax=ax,
                                          label='Dataset D (provided)')

        plot_confidence_interval(x_median=v_median,
                                 y_median=hs_median,
                                 x_bottom=v_lower,
                                 y_bottom=hs_lower,
                                 x_upper=v_upper,
                                 y_upper=hs_upper,
                                 ax=ax,