def test_FunctionParam_power3(self): """ tests if function power3 calculates the correct value """ test_func = FunctionParam('power3', 0, 1, 0) self.assertEqual(test_func._value(0), 1)
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 _setup(self, dep1=(None, None, None), dep2=(0, None, 0), par1=(ConstantParam(1.471), ConstantParam(0.8888), ConstantParam(2.776)), par2=(FunctionParam('exp3', 0.0400, 0.1748, -0.2243), None, FunctionParam('power3', 0.1, 1.489, 0.1901)) ): """ Creating contour. """ # Define dependency tuple. self.dep1 = dep1 self.dep2 = dep2 # Define parameters. self.par1 = par1 self.par2 = par2 # Create distributions. dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(sigma=par2[0], mu=par2[2]) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) # Calculate contour dsc = DirectSamplingContour(mul_dist, 10000000, 25, 6, 6) test_contour_dsc = dsc.direct_sampling_contour() return test_contour_dsc
def test_FunctionParam_f2(self): """ tests if function f1 calculates the correct value """ test_func = FunctionParam(1, 1, 0, 'f2') self.assertEqual(test_func._value(0), 2)
def test_multivariate_draw_sample(self): """ Create an example MultivariateDistribution (Vanem2012 model). """ # 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) shape = FunctionParam('exp3', 0.0400, 0.1748, -0.2243) loc = None scale = FunctionParam('power3', 0.1, 1.489, 0.1901) par2 = (shape, loc, scale) del shape, loc, scale # Create distributions. dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(*par2) distributions = [dist1, dist2] dependencies = [dep1, dep2] ref_points = 99119 mul_var_dist = MultivariateDistribution(distributions, dependencies) my_points = mul_var_dist.draw_sample(ref_points) my_points0 = my_points[0].size my_points1 = my_points[1].size assert ref_points == my_points0 assert ref_points == my_points1
def test_FunctionParam_exp3(self): """ tests if function exp3 calculates the correct value. """ test_func = FunctionParam('exp3', 1, 1, 0) self.assertEqual(test_func._value(0), 2)
def test_FunctionParam_asymdecrease3(self): """ Tests if function asymdecrease3 calculates the correct value. """ test_func = FunctionParam('asymdecrease3', 1, 4, 3) self.assertEqual(test_func._value(0), 5) self.assertEqual(test_func.func_name, 'asymdecrease3') self.assertEqual(str(test_func)[0:1], '1')
def test_FunctionParam_powerdecrease3(self): """ Tests if function powerdecrease3 calculates the correct value. """ test_func = FunctionParam('powerdecrease3', 1, 2, 2) self.assertEqual(test_func._value(0), 1.25) self.assertEqual(test_func.func_name, 'powerdecrease3') self.assertEqual(str(test_func)[0:1], '1')
def test_FunctionParam_lnsquare2(self): """ Tests if function lnsquare2 calculates the correct value. """ test_func = FunctionParam('lnsquare2', 1, 1, None) self.assertEqual(test_func._value(0), 0) self.assertEqual(test_func.func_name, 'lnsquare2') self.assertEqual(str(test_func)[0:2], 'ln')
def test_FunctionParam_logistics4(self): """ Tests if function logistics4 calculates the correct value. """ test_func = FunctionParam('logistics4', 1, 2, 3, 4) self.assertAlmostEqual(test_func._value(0), 1, delta=0.001) self.assertAlmostEqual(test_func._value(10), 3, delta=0.001) self.assertEqual(test_func.func_name, 'logistics4') self.assertEqual(str(test_func)[0:1], '1')
def test_HDC3d_WLN(self): dep1 = (None, None, None) dep2 = (0, None, 0) dep3 = (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(4, 10, 0.02, "f1") scale = FunctionParam(0.1, 0.02, -0.1, "f2") par2 = (shape, loc, scale) mu = FunctionParam(0.1, 1.5, 0.2, "f1") sigma = FunctionParam(0.1, 0.2, -0.2, "f2") #create distributions dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(mu=mu, sigma=sigma) dist3 = NormalDistribution(*par2) distributions = [dist1, dist2, dist3] dependencies = [dep1, dep2, dep3] mul_dist = MultivariateDistribution(distributions, dependencies) del mu, sigma #del dist1, dist2, par1, par2, dep1, dep2, dependencies, distributions #calc contour n_years = 50 limits = [(0, 20), (0, 20), (0, 20)] deltas = [0.5, 0.5, 0.05] test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas) finaldt3 = pd.DataFrame({ 'x': test_contour_HDC.coordinates[0][0], 'y': test_contour_HDC.coordinates[0][1], 'z': test_contour_HDC.coordinates[0][2] }) matlab3 = pd.read_csv(testfiles_path + "/hdc3d_wln.csv", names=['x', 'y', 'z']) result3 = pd.read_csv(testfiles_path + "/HDC3dWLN_coordinates.csv") for m, n in [(m, n) for m in result3.index for n in result3.columns]: self.assertAlmostEqual(result3.loc[m, n], finaldt3.loc[m, n], places=8)
def test_HDC3d_WLL(self): """ Creating Contour example for 3-d HDC with Weibull, Lognormal and Lognormal distribution """ 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) #del dist1, dist2, par1, par2, dep1, dep2, dependencies, distributions #calc contour n_years = 50 limits = [(0, 20), (0, 18), (0, 18)] deltas = [1, 1, 1] test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas) finaldt = pd.DataFrame({ 'x': test_contour_HDC.coordinates[0][0], 'y': test_contour_HDC.coordinates[0][1], 'z': test_contour_HDC.coordinates[0][2] }) result = pd.read_csv(testfiles_path + "/HDC3dWLL_coordinates.csv") for i, j in [(i, j) for i in result.index for j in result.columns]: self.assertAlmostEqual(result.loc[i, j], finaldt.loc[i, j], places=8)
def test_draw_sample_distribution(self): """ Create an example MultivariateDistribution (Vanem2012 model). """ # 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) shape = FunctionParam('exp3', 0.0400, 0.1748, -0.2243) loc = None scale = FunctionParam('power3', 0.1, 1.489, 0.1901) par2 = (shape, loc, scale) del shape, loc, scale # Create distributions. dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(*par2) distributions = [dist1, dist2] dependencies = [dep1, dep2] points = 1000000 mul_var_dist = MultivariateDistribution(distributions, dependencies) my_points = mul_var_dist.draw_sample(points) #Fit the sample # Describe the distribution that should be fitted to the sample. dist_description_0 = { 'name': 'Weibull', 'dependency': (None, None, None), 'width_of_intervals': 2 } dist_description_1 = { 'name': 'Lognormal', 'dependency': (0, None, 0), 'functions': ('exp3', None, 'power3') } my_fit = Fit([my_points[0], my_points[1]], [dist_description_0, dist_description_1]) print(my_fit.mul_var_dist.distributions[0].shape(0)) print(mul_var_dist.distributions[0].shape(0)) assert np.round(my_fit.mul_var_dist.distributions[0].shape(0), 2) == np.round(mul_var_dist.distributions[0].shape(0), 2)
def test_HDC2d_WL(self): """ 2-d HDC with Weibull and Lognormal distribution. 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') #del shape, loc, scale #create distributions dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(mu=mu, sigma=sigma) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) #del dist1, dist2, par1, par2, dep1, dep2, dependencies, distributions #calc contour n_years = 50 limits = [(0, 20), (0, 18)] deltas = [0.1, 0.1] test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas) finaldt0 = pd.DataFrame({ 'x': test_contour_HDC.coordinates[0][0], 'y': test_contour_HDC.coordinates[0][1] }) result0 = pd.read_csv(testfiles_path + "/HDC2dWL_coordinates.csv") for g, h in [(g, h) for g in result0.index for h in result0.columns]: self.assertAlmostEqual(result0.loc[g, h], finaldt0.loc[g, h], places=8)
def test_HDC2d_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(4, 10, 0.02, "f1") scale = FunctionParam(0.1, 0.02, -0.1, "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) #del dist1, dist2, par1, par2, dep1, dep2, dependencies, distributions #calc contour n_years = 50 limits = [(0, 20), (0, 20)] deltas = [0.05, 0.01] test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas) finaldt2 = pd.DataFrame({ 'x': test_contour_HDC.coordinates[0][0], 'y': test_contour_HDC.coordinates[0][1] }) result2 = pd.read_csv(testfiles_path + "/HDC2dWN_coordinates.csv") for k, l in [(k, l) for k in result2.index for l in result2.columns]: self.assertAlmostEqual(result2.loc[k, l], finaldt2.loc[k, l], places=8)
def test_omae2020_wind_wave_contour(self): """ Contour similar to the wind-wave contour in 'Global hierararchical models for wind and wave contours', dataset D. First variable = wind speed, second variable = significant wave height. """ # Define dependency tuple. dep1 = (None, None, None, None) # shape, location, scale, shape2 dep2 = (0, None, 0, None) # shape, location, scale, shape2 # Define parameters. v_shape = ConstantParam(2.42) v_loc = None v_scale = ConstantParam(10) v_shape2 = ConstantParam(0.761) par1 = (v_shape, v_loc, v_scale, v_shape2) hs_shape = FunctionParam('logistics4', 0.582, 1.90, 0.248, 8.49) hs_loc = None hs_scale = FunctionParam('alpha3', 0.394, 0.0178, 1.88, C1=0.582, C2=1.90, C3=0.248, C4=8.49) hs_shape2 = ConstantParam(5) par2 = (hs_shape, hs_loc, hs_scale, hs_shape2) # Create distributions. dist1 = ExponentiatedWeibullDistribution(*par1) dist2 = ExponentiatedWeibullDistribution(*par2) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) # Calculate the contour. n_years = 50 limits = [(0, 40), (0, 20)] deltas = [0.1, 0.1] test_contour_HDC = HighestDensityContour(mul_dist, n_years, 1, limits, deltas) # Compare the computed contours to the contours published in # 'Global hierarchical models for wind and wave contours', Figure 8. max_v = max(test_contour_HDC.coordinates[0][0]) self.assertAlmostEqual(max_v, 29.5, delta=0.5) # Should be about 29.5 max_hs = max(test_contour_HDC.coordinates[0][1]) self.assertAlmostEqual(max_hs, 14.5, delta=0.5) # Should be about 15
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_FunctionParam_unknown(self): """ tests if the right exception appears when trying to create a non existent function """ with self.assertRaises(ValueError): FunctionParam('linear', 2.5, 1.0, 0.5)
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 setup_mul_dist(probabilistic_model: ProbabilisticModel): """ Generates a MultiVariateDistribution from a ProbabilisticModel. MultiVariateDistribution objects are used to perform the statistical computations in the viroconcom package. ProbabilisticModel objects are used in the viroconweb package to be saved in the data base. Parameters ---------- probabilistic_model : ProbabilisticModel, The probabilistic model, which should be converted. Returns ------- mutivar_distribution : MultivariateDistribution, The object, which can be used in the viroconcom package. """ distributions_model = DistributionModel.objects.filter( probabilistic_model=probabilistic_model) distributions = [] dependencies = [] for dist in distributions_model: dependency = [] parameters = [] parameters_model = ParameterModel.objects.filter(distribution=dist) for param in parameters_model: dependency.append(adjust(param.dependency)) if adjust(param.function) is not None: parameters.append( FunctionParam(float(param.x0), float(param.x1), float(param.x2), param.function)) else: parameters.append(ConstantParam(float(param.x0))) dependencies.append(dependency) if dist.distribution == 'Normal': distributions.append(NormalDistribution(*parameters)) elif dist.distribution == 'Weibull': distributions.append(WeibullDistribution(*parameters)) elif dist.distribution == 'Lognormal_SigmaMu': distributions.append( LognormalDistribution(sigma=parameters[0], mu=parameters[2])) elif dist.distribution == 'KernelDensity': distributions.append(KernelDensityDistribution(*parameters)) else: raise KeyError('{} is not a matching distribution'.format( dist.distribution)) mutivar_distribution = MultivariateDistribution(distributions, dependencies) return mutivar_distribution
def test_FunctionParam_alpha3(self): """ Tests if function alpha3 calculates the correct value. """ # Use the function presented in 'Global hierachical models ...' for dataset D. test_func = FunctionParam('alpha3', 0.394, 0.0178, 1.88, C1=0.582, C2=1.90, C3=0.248, C4=8.49) self.assertAlmostEqual(test_func._value(0), 0.2, delta=0.2) self.assertAlmostEqual(test_func._value(10), 1, delta=0.3) self.assertAlmostEqual(test_func._value(20), 4, delta=0.5) self.assertEqual(test_func.func_name, 'alpha3') self.assertEqual(str(test_func)[0:1], '(')
def test_HDC4d_WLLL(self): """ Creating Contour example for 4-d HDC with Weibull, Lognormal, Lognormal and Lognormal distribution """ #define dependency tuple dep1 = (None, None, None) dep2 = (0, None, 0) dep3 = (0, None, 0) dep4 = (0, None, 0) #define parameters shape = ConstantParam(2.776) loc = ConstantParam(1.471) scale = ConstantParam(0.8888) 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) dist3 = LognormalDistribution(mu=mu, sigma=sigma) dist4 = LognormalDistribution(mu=mu, sigma=sigma) distributions = [dist1, dist2, dist3, dist4] dependencies = [dep1, dep2, dep3, dep4] mul_dist = MultivariateDistribution(distributions, dependencies) #del dist1, dist2, par1, par2, dep1, dep2, dependencies, distributions #calc contour n_years = 50 limits = [(0, 20), (0, 18), (0, 18), (0, 18)] deltas = [1, 1, 1, 1] test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas)
def _setup(self, limits=[(0, 20), (0, 20)], deltas=[0.05, 0.05], n_years = 25, dep1=(None, None, None), dep2=(0, None, 0), par1=(ConstantParam(1.471), ConstantParam(0.8888), ConstantParam(2.776)), par2=(FunctionParam('exp3', 0.0400, 0.1748, -0.2243), None, FunctionParam('power3', 0.1, 1.489, 0.1901)) ): """ Creating a contour (same as in DOI: 10.1016/j.coastaleng.2017.03.002). """ self.limits = limits self.deltas = deltas self.n_years = n_years # Define dependency tuple. self.dep1 = dep1 self.dep2 = dep2 # Define parameters. self.par1 = par1 self.par2 = par2 # Create distributions. dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(*par2) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) # Compute contour. test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas) return test_contour_HDC
def test_HDC4d_WLLL(self): """ Contour example for a 4-dimensinal HDC with Weibull, Lognormal, Lognormal and Lognormal distribution. """ # Define dependency tuple. dep1 = (None, None, None) dep2 = (0, None, 0) dep3 = (0, None, 0) dep4 = (0, None, 0) # Define parameters. shape = ConstantParam(2.776) loc = ConstantParam(1.471) scale = ConstantParam(0.8888) 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) dist3 = LognormalDistribution(mu=mu, sigma=sigma) dist4 = LognormalDistribution(mu=mu, sigma=sigma) distributions = [dist1, dist2, dist3, dist4] dependencies = [dep1, dep2, dep3, dep4] mul_dist = MultivariateDistribution(distributions, dependencies) # Compute contour. n_years = 50 limits = [(0, 20), (0, 18), (0, 18), (0, 18)] deltas = [1, 1, 1, 1] test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas)
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 _setup(self, limits=[(0, 20), (0, 20)], deltas=[0.05, 0.05], n_years=25, dep1=(None, None, None), dep2=(0, None, 0), par1=(ConstantParam(1.471), ConstantParam(0.8888), ConstantParam(2.776)), par2=(FunctionParam(0.0400, 0.1748, -0.2243, "f2"), None, FunctionParam(0.1, 1.489, 0.1901, "f1"))): """ Creating Contour example """ self.limits = limits self.deltas = deltas self.n_years = n_years #define dependency tuple self.dep1 = dep1 self.dep2 = dep2 #define parameters self.par1 = par1 self.par2 = par2 #create distributions dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(*par2) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) #calc contour test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas) return test_contour_HDC
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_Wrapper(self): """ tests if a wrapper object gives the same result as the class Wrapper() calculates """ test_func = FunctionParam('power3', 0.5, 1.0, 0.0, wrapper=np.exp) test_func2 = FunctionParam('power3', 0.5, 1.0, 0.0, wrapper=Wrapper(np.exp)) self.assertEqual(test_func._value(9), test_func2._value(9))