def test_negative_freq_error(self): """Test _apply_knutson_criterion with infeasible input.""" criterion = [{'basin': 'SP', 'category': [0, 1], 'year': 2100, 'change': 0.5, 'variable': 'frequency'} ] tc = TropCyclone() tc.frequency = np.ones(2) tc.basin = ['SP', 'SP'] tc.category = np.array([0, 1]) with self.assertRaises(ValueError): tc._apply_knutson_criterion(criterion, 3)
def test_two_criterion_track(self): """Test _apply_criterion function with two criteria""" criterion = [ {'basin': 'NA', 'category': [1, 2, 3, 4, 5], 'year': 2100, 'change': 1.045, 'variable': 'intensity'}, {'basin': 'WP', 'category': [1, 2, 3, 4, 5], 'year': 2100, 'change': 1.025, 'variable': 'intensity'}, {'basin': 'WP', 'category': [1, 2, 3, 4, 5], 'year': 2100, 'change': 1.025, 'variable': 'frequency'}, {'basin': 'NA', 'category': [0, 1, 2, 3, 4, 5], 'year': 2100, 'change': 0.7, 'variable': 'frequency'}, {'basin': 'NA', 'category': [1, 2, 3, 4, 5], 'year': 2100, 'change': 1, 'variable': 'frequency'}, {'basin': 'NA', 'category': [3, 4, 5], 'year': 2100, 'change': 1, 'variable': 'frequency'}, {'basin': 'NA', 'category': [4, 5], 'year': 2100, 'change': 2, 'variable': 'frequency'} ] scale = 0.75 tc = TropCyclone() tc.intensity = np.zeros((4, 10)) tc.intensity[0, :] = np.arange(10) tc.intensity[1, 5] = 10 tc.intensity[2, :] = np.arange(10, 20) tc.intensity[3, 3] = 3 tc.intensity = sparse.csr_matrix(tc.intensity) tc.frequency = np.ones(4) * 0.5 tc.basin = ['NA'] * 4 tc.basin[3] = 'WP' tc.category = np.array([2, 0, 4, 1]) tc.event_id = np.arange(4) tc_cc = tc._apply_knutson_criterion(criterion, scale) self.assertTrue(np.allclose(tc.intensity[1, :].toarray(), tc_cc.intensity[1, :].toarray())) self.assertFalse( np.allclose(tc.intensity[3, :].toarray(), tc_cc.intensity[3, :].toarray())) self.assertFalse( np.allclose(tc.intensity[0, :].toarray(), tc_cc.intensity[0, :].toarray())) self.assertFalse( np.allclose(tc.intensity[2, :].toarray(), tc_cc.intensity[2, :].toarray())) self.assertTrue( np.allclose(tc.intensity[0, :].toarray() * 1.03375, tc_cc.intensity[0, :].toarray())) self.assertTrue( np.allclose(tc.intensity[2, :].toarray() * 1.03375, tc_cc.intensity[2, :].toarray())) self.assertTrue( np.allclose(tc.intensity[3, :].toarray() * 1.01875, tc_cc.intensity[3, :].toarray())) res_frequency = np.ones(4) * 0.5 res_frequency[1] = 0.5 * (1 + (0.7 - 1) * scale) res_frequency[2] = 0.5 * (1 + (2 - 1) * scale) res_frequency[3] = 0.5 * (1 + (1.025 - 1) * scale) self.assertTrue(np.allclose(tc_cc.frequency, res_frequency))
def test_apply_criterion_track(self): """Test _apply_criterion function.""" criterion = [{ 'basin': 'NA', 'category': [1, 2, 3, 4, 5], 'year': 2100, 'change': 1.045, 'variable': 'intensity' }] scale = 0.75 # artificially increase the size of the hazard by repeating (tiling) the data: ntiles = 8 tc = TropCyclone() tc.intensity = np.zeros((4, 10)) tc.intensity[0, :] = np.arange(10) tc.intensity[1, 5] = 10 tc.intensity[2, :] = np.arange(10, 20) tc.intensity[3, 3] = 3 tc.intensity = np.tile(tc.intensity, (ntiles, 1)) tc.intensity = sparse.csr_matrix(tc.intensity) tc.basin = ['NA'] * 4 tc.basin[3] = 'WP' tc.basin = ntiles * tc.basin tc.category = np.array(ntiles * [2, 0, 4, 1]) tc.event_id = np.arange(tc.intensity.shape[0]) tc_cc = tc._apply_knutson_criterion(criterion, scale) for i_tile in range(ntiles): offset = i_tile * 4 # no factor applied because of category 0 np.testing.assert_array_equal( tc.intensity[offset + 1, :].toarray(), tc_cc.intensity[offset + 1, :].toarray()) # no factor applied because of basin "WP" np.testing.assert_array_equal( tc.intensity[offset + 3, :].toarray(), tc_cc.intensity[offset + 3, :].toarray()) # factor is applied to the remaining events np.testing.assert_array_almost_equal( tc.intensity[offset + 0, :].toarray() * 1.03375, tc_cc.intensity[offset + 0, :].toarray()) np.testing.assert_array_almost_equal( tc.intensity[offset + 2, :].toarray() * 1.03375, tc_cc.intensity[offset + 2, :].toarray())