def test_IForm3d(self): # TODO what does this test do """ 3-dimensional IFORM contour. """ # Define dependency tuple. dep1 = (None, None, None) dep2 = (0, None, 0) dep3 = (0, None, 0) # Define parameters. shape = ConstantParam(1.471) loc = ConstantParam(0.8888) scale = ConstantParam(2.776) par1 = (shape, loc, scale) mu = FunctionParam('power3', 0.1000, 1.489, 0.1901) sigma = FunctionParam('exp3', 0.0400, 0.1748, -0.2243) #del shape, loc, scale # Create distributions. dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(mu=mu, sigma=sigma) dist3 = LognormalDistribution(mu=mu, sigma=sigma) distributions = [dist1, dist2, dist3] dependencies = [dep1, dep2, dep3] mul_dist = MultivariateDistribution(distributions, dependencies) test_contour_IForm = IFormContour(mul_dist, 50, 3, 400)
def test_IForm3d(self): # TODO what does this test do """ Creating Contour example """ #define dependency tuple dep1 = (None, None, None) dep2 = (0, None, 0) dep3 = (0, None, 0) #define parameters shape = ConstantParam(1.471) loc = ConstantParam(0.8888) scale = ConstantParam(2.776) par1 = (shape, loc, scale) mu = FunctionParam(0.1000, 1.489, 0.1901, "f1") sigma = FunctionParam(0.0400, 0.1748, -0.2243, "f2") #del shape, loc, scale #create distributions dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(mu=mu, sigma=sigma) dist3 = LognormalDistribution(mu=mu, sigma=sigma) distributions = [dist1, dist2, dist3] dependencies = [dep1, dep2, dep3] mul_dist = MultivariateDistribution(distributions, dependencies) test_contour_IForm = IFormContour(mul_dist, 50, 3, 400)
def test_IForm2d_WL(self): """ 2-d IFORM contour. The used probabilistic model is described in Vanem and Bitner-Gregersen (2012), DOI: 10.1016/j.apor.2012.05.006 """ # Define dependency tuple dep1 = (None, None, None) dep2 = (0, None, 0) # Define parameters shape = ConstantParam(1.471) loc = ConstantParam(0.8888) scale = ConstantParam(2.776) par1 = (shape, loc, scale) mu = FunctionParam(0.1000, 1.489, 0.1901, "power3") sigma = FunctionParam(0.0400, 0.1748, -0.2243, "exp3") # Create distributions dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(mu=mu, sigma=sigma) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) test_contour_IForm = IFormContour(mul_dist, 50, 3, 50) calculated_coordinates = pd.DataFrame({ 'x': test_contour_IForm.coordinates[0][0], 'y': test_contour_IForm.coordinates[0][1] }) #calculated_coordinates.to_csv('save_this_file.csv', sep=',', header=['x', 'y'], index=False) true_coordinates = pd.read_csv(testfiles_path + "/IForm2dWL_coordinates.csv") for o, p in [(o, p) for o in true_coordinates.index for p in true_coordinates.columns]: self.assertAlmostEqual(calculated_coordinates.loc[o, p], true_coordinates.loc[o, p], places=8)
def test_IForm2d_WN(self): """ 2-d IFORM contour. """ # Define dependency tuple. dep1 = (None, None, None) dep2 = (None, 0, 0) # Define parameters. shape = ConstantParam(1.471) loc = ConstantParam(0.8888) scale = ConstantParam(2.776) par1 = (shape, loc, scale) shape = None loc = FunctionParam(7, 1.489, 0.1901, "power3") scale = FunctionParam(1.5, 0.1748, -0.2243, "exp3") par2 = (shape, loc, scale) # Create distributions. dist1 = WeibullDistribution(*par1) dist2 = NormalDistribution(*par2) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) test_contour_IForm = IFormContour(mul_dist, 50, 3, 50) calculated_coordinates = pd.DataFrame({ 'x': test_contour_IForm.coordinates[0][0], 'y': test_contour_IForm.coordinates[0][1] }) true_coordinates = pd.read_csv(testfiles_path + "/IForm2dWN_coordinates.csv") for r, s in [(r, s) for r in true_coordinates.index for s in true_coordinates.columns]: self.assertAlmostEqual(calculated_coordinates.loc[r, s], true_coordinates.loc[r, s], places=8)
def test_IForm2d_WN(self): """ Creating Contour example """ #define dependency tuple dep1 = (None, None, None) dep2 = (None, 0, 0) #define parameters shape = ConstantParam(1.471) loc = ConstantParam(0.8888) scale = ConstantParam(2.776) par1 = (shape, loc, scale) shape = None loc = FunctionParam(7, 1.489, 0.1901, "f1") scale = FunctionParam(1.5, 0.1748, -0.2243, "f2") par2 = (shape, loc, scale) #del shape, loc, scale #create distributions dist1 = WeibullDistribution(*par1) dist2 = NormalDistribution(*par2) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) test_contour_IForm = IFormContour(mul_dist, 50, 3, 400) finaldt5 = pd.DataFrame({ 'x': test_contour_IForm.coordinates[0][0], 'y': test_contour_IForm.coordinates[0][1] }) result5 = pd.read_csv(testfiles_path + "/IForm2dWN_coordinates.csv") for r, s in [(r, s) for r in result5.index for s in result5.columns]: self.assertAlmostEqual(result5.loc[r, s], finaldt5.loc[r, s], places=8)
def test_IForm2d_WL(self): """ Creating Contour example """ #define dependency tuple dep1 = (None, None, None) dep2 = (0, None, 0) #define parameters shape = ConstantParam(1.471) loc = ConstantParam(0.8888) scale = ConstantParam(2.776) par1 = (shape, loc, scale) mu = FunctionParam(0.1000, 1.489, 0.1901, "f1") sigma = FunctionParam(0.0400, 0.1748, -0.2243, "f2") #create distributions dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(mu=mu, sigma=sigma) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) test_contour_IForm = IFormContour(mul_dist, 50, 3, 400) finaldt4 = pd.DataFrame({ 'x': test_contour_IForm.coordinates[0][0], 'y': test_contour_IForm.coordinates[0][1] }) result4 = pd.read_csv(testfiles_path + "/IForm2dWL_coordinates.csv") for o, p in [(o, p) for o in result4.index for p in result4.columns]: self.assertAlmostEqual(result4.loc[o, p], finaldt4.loc[o, p], places=8)
def test_plot_contour_without_sample(self): """ Plots a contour in the most basic way. """ # Define dependency tuple. dep1 = (None, None, None) dep2 = (0, None, 0) # Define parameters. shape = ConstantParam(1.471) loc = ConstantParam(0.8888) scale = ConstantParam(2.776) par1 = (shape, loc, scale) mu = FunctionParam('power3', 0.1000, 1.489, 0.1901) sigma = FunctionParam('exp3', 0.0400, 0.1748, -0.2243) # Create distributions. dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(mu=mu, sigma=sigma) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) test_contour_IForm = IFormContour(mul_dist, 50, 3, 50) contour_hs = test_contour_IForm.coordinates[0][0] contour_tz = test_contour_IForm.coordinates[0][1] fig = plt.figure() ax = fig.add_subplot(111) plot_contour(contour_hs, contour_tz, ax) #plt.show() x_plot, y_plot = ax.lines[0].get_xydata().T self.assertAlmostEqual(y_plot[0], contour_tz[0], delta=0.001)
def iform(probabilistic_model: ProbabilisticModel, return_period, state_duration, n_points): """ Interface to viroconcom to compute an IFORM contour. Parameters ---------- probabilistic_model : ProbabilisticModel, The probabilistic model, i.e. the joint distribution function, which should be evaluated. return_period : float, The return period of the contour in years. state_duration : float, The sea state's or more general the environmental state's duration in hours. n_points : int, Number of points along the contour that should be calculated. Returns ------- contour_coordinates : list of list of numpy.ndarray, Contains the coordinates of points on the contour. The outer list contains can hold multiple contour paths if the distribution is multimodal. The inner list contains multiple numpy arrays of the same length, one per dimension. The values of the arrays are the coordinates in the corresponding dimension. """ mul_dist = setup_mul_dist(probabilistic_model) contour = IFormContour(mul_var_distribution=mul_dist, return_period=return_period, state_duration=state_duration, n_points=n_points, timeout=MAX_COMPUTING_TIME) contour_coordinates = contour.coordinates return contour_coordinates
loc = ConstantParam(0.889) scale = ConstantParam(2.776) dist0 = WeibullDistribution(shape, loc, scale) dep0 = (None, None, None) # All three parameters are independent. my_sigma = FunctionParam('exp3', 0.040, 0.175, -0.224) my_mu = FunctionParam('power3', 0.100, 1.489, 0.190) dist1 = LognormalDistribution(sigma=my_sigma, mu=my_mu) dep1 = (0, None, 0) # Parameter one and three depend on dist0. distributions = [dist0, dist1] dependencies = [dep0, dep1] mul_dist = MultivariateDistribution(distributions, dependencies) # Compute an IFORM, an ISORM and a highest density contour. return_period = 50 # In years sea_state_duration = 6 # In hours iform_contour = IFormContour(mul_dist, return_period, sea_state_duration, 100) isorm_contour = ISormContour(mul_dist, return_period, sea_state_duration, 100) limits = [(0, 20), (0, 20)] # Limits of the computational domain deltas = [0.005, 0.005] # Dimensions of the grid cells hdens_contour = HighestDensityContour( mul_dist, return_period, sea_state_duration, limits, deltas) # Plot the three contours. plt.scatter(hdens_contour.coordinates[0][0], hdens_contour.coordinates[0][1], label='highest density contour') plt.scatter(iform_contour.coordinates[0][0], iform_contour.coordinates[0][1], label='IFORM contour') plt.scatter(isorm_contour.coordinates[0][0], isorm_contour.coordinates[0][1], label='ISORM contour') plt.xlabel('significant wave height [m]') plt.ylabel('zero-upcrossing period [s]')
sts.lognorm.pdf(np.linspace(0, 20, 100), s=shape, scale=scale)) ax_4.set_xlabel('spectral peak period [s]') plt.show() # Plot the fit of the dependency function of scale. fig = plt.figure() x_1 = np.linspace(0, 12, 100) plt.plot(param_grid, my_fit.multiple_fit_inspection_data[1].scale_value, 'x', label='discrete scale values') plt.plot(x_1, my_fit.mul_var_dist.distributions[1].scale(x_1), label='fitted dependency function') plt.xlabel('significant wave height [m]') plt.ylabel('scale parameter (Tp-distribution) [-]') plt.legend() plt.show() # Compute a contour based on the fit and plot it together with the sample. iform_contour = IFormContour(my_fit.mul_var_dist, 25, 3, 100) plt.scatter(sample_1, sample_2, label='sample') plt.plot(iform_contour.coordinates[0][0], iform_contour.coordinates[0][1], '-k', label='IFORM contour') plt.xlabel('significant wave height [m]') plt.ylabel('spectral peak period [s]') plt.legend() plt.show()
fig = plt.figure(figsize=(10, 5), dpi=150) plot_marginal_fit(sample_hs, dist0, fig=fig, label='Significant wave height (m)') fig.suptitle('Dataset ' + DATASET_CHAR) fig = plt.figure(figsize=(6, 5), dpi=150) plot_dependence_functions(fit=fit, fig=fig, unconditonal_variable_label=label_hs) fig.suptitle('Dataset ' + DATASET_CHAR) # Compute IFORM-contours with return periods of 1 and 20 years. return_period_1 = 1 iform_contour_1 = IFormContour(fit.mul_var_dist, return_period_1, 1, 100) return_period_20 = 20 iform_contour_20 = IFormContour(fit.mul_var_dist, return_period_20, 1, 100) # Save the contours as csv files in the required format. folder_name = 'contour_coordinates/' file_name_1 = determine_file_name_e1('John', 'Doe', DATASET_CHAR, return_period_1) write_contour(iform_contour_1.coordinates[0][0], iform_contour_1.coordinates[0][1], folder_name + file_name_1, label_x=label_hs, label_y=label_tz) file_name_20 = determine_file_name_e1('John', 'Doe', DATASET_CHAR, return_period_20) write_contour(iform_contour_20.coordinates[0][0],
dep0 = (None, None, None) # All three parameters are independent. # Define a Lognormal distribution representing spectral peak period. my_sigma = FunctionParam(0.05, 0.2, -0.2, "exp3") my_mu = FunctionParam(0.1, 1.5, 0.2, "power3") dist1 = LognormalDistribution(sigma=my_sigma, mu=my_mu) dep1 = (0, None, 0) # Parameter one and three depend on dist0. # Create a multivariate distribution by bundling the two distributions. distributions = [dist0, dist1] dependencies = [dep0, dep1] mul_dist = MultivariateDistribution(distributions, dependencies) # Compute an IFORM contour with a return period of 25 years, a sea state # duration of 3 hours and 100 points along the contour. iform_contour = IFormContour(mul_dist, 25, 3, 100) # Compute a highest density contour with the same settings (25 years return # period, 3 hour sea state duration). limits = [(0, 20), (0, 20)] # The limits of the computational domain. deltas = [0.4, 0.4] # The dimensions of the grid cells. hdens_contour = HighestDensityContour(mul_dist, 25, 3, limits, deltas) # Plot the two contours. plt.scatter(hdens_contour.coordinates[0][0], hdens_contour.coordinates[0][1], label="highest density contour") plt.scatter(iform_contour.coordinates[0][0], iform_contour.coordinates[0][1], label="IFORM contour") plt.xlabel('significant wave height [m]') plt.ylabel('spectral peak period [s]') plt.legend()
def test_plot_contour_and_sample(self): """ Plots a contour together with the dataset that has been used to fit a distribution for the contour. """ sample_hs, sample_tz, label_hs, label_tz = read_ecbenchmark_dataset() # Define the structure of the probabilistic model that will be fitted to the # dataset. dist_description_hs = { 'name': 'Weibull_Exp', 'dependency': (None, None, None, None), 'width_of_intervals': 0.5 } dist_description_tz = { 'name': 'Lognormal_SigmaMu', 'dependency': (0, None, 0), # Shape, Location, Scale 'functions': ('asymdecrease3', None, 'lnsquare2'), # Shape, Location, Scale 'min_datapoints_for_fit': 50 } # Fit the model to the data. fit = Fit((sample_hs, sample_tz), (dist_description_hs, dist_description_tz)) contour = IFormContour(fit.mul_var_dist, 20, 1, 50) contour_hs_20 = contour.coordinates[0][0] contour_tz_20 = contour.coordinates[0][1] # Find datapoints that exceed the 20-yr contour. hs_outside, tz_outside, hs_inside, tz_inside = \ points_outside(contour_hs_20, contour_tz_20, np.asarray(sample_hs), np.asarray(sample_tz)) # Compute the median tz conditonal on hs. hs = np.linspace(0, 14, 100) d1 = fit.mul_var_dist.distributions[1] c1 = d1.scale.a c2 = d1.scale.b tz = c1 + c2 * np.sqrt(np.divide(hs, 9.81)) fig = plt.figure(figsize=(5, 5), dpi=150) ax = fig.add_subplot(111) # Plot the 20-year contour and the sample. plotted_sample = SamplePlotData(x=np.asarray(sample_tz), y=np.asarray(sample_hs), ax=ax, x_inside=tz_inside, y_inside=hs_inside, x_outside=tz_outside, y_outside=hs_outside, return_period=20) plot_contour(x=contour_tz_20, y=contour_hs_20, ax=ax, contour_label='20-yr IFORM contour', x_label=label_tz, y_label=label_hs, line_style='b-', plotted_sample=plotted_sample, x_lim=(0, 19), upper_ylim=15, median_x=tz, median_y=hs, median_label='median of $T_z | H_s$') plot_wave_breaking_limit(ax)
mul_dist = MultivariateDistribution(distributions, dependencies) ref_f = mul_dist.pdf(x.T) ref_f_expweib = mul_dist.distributions[0].pdf(x[:, 0]) ref_ln = mul_dist.distributions[1] ref_f_ln = [] for given in my_given: y = np.stack([np.full_like(x[:, 1], given), x[:, 1]]) ref_f_ln.append(ref_ln.pdf(x[:, 1], y, (0, None, 0))) ref_f_ln = np.stack(ref_f_ln, axis=1) ref_iform = IFormContour(mul_dist, return_period, state_duration) ref_coordinates = np.stack(ref_iform.coordinates, axis=1) # %% # np.savez_compressed("reference_data_IFORM.npz", ref_coordinates=ref_coordinates) # %% plt.close("all") plt.plot(my_f, label="my_f") plt.plot(ref_f, label="ref_f") plt.legend() plt.figure()
label='$v$ (m s$^{-1}$)', dataset_char='D') plot_dependence_functions(fit=fit, fig=fig_fit, ax1=ax2, ax2=ax3, unconditonal_variable_label=v_label) fig_fit.subplots_adjust(wspace=0.25, bottom=0.15) figure_folder = 'gfx/' figure_fname = figure_folder + 'FitVHs' #fig_fit.savefig(figure_fname + '.pdf', bbox_inches='tight') fig_fit.savefig(figure_fname, dpi=dpi_for_printing_figures, bbox_inches='tight') IFORMC = IFormContour(joint_dist, return_period=50, state_duration=1) limits = [(0, 45), (0, 25)] deltas = [0.05, 0.05] HDC2D = HighestDensityContour(joint_dist, return_period=50, state_duration=1, limits=limits, deltas=deltas) print('Done with calcluating the 2D HDC') v_step = 0.1 h_step = 0.1 vgrid, hgrid = np.mgrid[0:45:v_step, 0:22:h_step] f = np.empty_like(vgrid) for i in range(vgrid.shape[0]):