def test_similarity_dist(self): subject = from_file('floodestimation/tests/data/37017.CD3') donor = from_file('floodestimation/tests/data/37020.CD3') analysis = GrowthCurveAnalysis(subject) result = analysis._similarity_distance(subject, donor) expected = 0.1159 # Science Report SC050050, table 6.6, row 2 self.assertAlmostEqual(result, expected, places=4)
def test_add_catchment_twice(self): catchment = loaders.from_file('floodestimation/tests/data/17002.CD3') loaders.to_db(catchment, self.session) duplicate_catchment = loaders.from_file('floodestimation/tests/data/17002.CD3') loaders.to_db(duplicate_catchment, self.session) self.assertRaises(IntegrityError, self.session.flush) self.session.rollback()
def test_add_catchment_twice(self): catchment = loaders.from_file('floodestimation/tests/data/17002.CD3') loaders.to_db(catchment, self.session) duplicate_catchment = loaders.from_file( 'floodestimation/tests/data/17002.CD3') loaders.to_db(duplicate_catchment, self.session) self.assertRaises(IntegrityError, self.session.flush) self.session.rollback()
def test_update_catchment(self): catchment = loaders.from_file('floodestimation/tests/data/17002.CD3') # Make a change catchment.location = "Dundee" loaders.to_db(catchment, self.session) # Reload catchment again duplicate_catchment = loaders.from_file('floodestimation/tests/data/17002.CD3') loaders.to_db(duplicate_catchment, self.session, method='update') self.session.flush() # Should have original data self.assertEqual(self.session.query(Catchment).get(17002).location, "Leven") self.session.rollback()
def test_catchment_by_number(self): expected = loaders.from_file('floodestimation/tests/data/17002.CD3') self.db_session.add(expected) result = CatchmentCollections( self.db_session).catchment_by_number(17002) self.assertIs(expected, result)
def test_37017(self): subject = from_file('floodestimation/tests/data/37017.CD3') analysis = GrowthCurveAnalysis(subject) self.assertEqual(len(subject.amax_records), 34) var, skew = analysis._var_and_skew(subject) self.assertAlmostEqual(var, 0.2232, places=4) self.assertAlmostEqual(skew, -0.0908, places=4)
def _load_data(self): self.results['report_date'] = date.today() self.results['version'] = __version__ self.catchment = loaders.from_file(self.catchment_file) self.results['catchment'] = self.catchment self.db_session = db.Session() if self.db_session.query(entities.Catchment).count() == 0: self.msg_queue.put(Progress("Downloading and storing NRFA data.", 10)) loaders.nrfa_to_db(self.db_session, autocommit=True, incl_pot=False) if self.config.getboolean('application', 'check_nrfa_updates', fallback=True): if fehdata.update_available(): self.msg_queue.put(Progress("Downloading and storing NRFA data update.", 10)) db.empty_db_tables() loaders.nrfa_to_db(self.db_session, autocommit=True, incl_pot=False) # Add subject catchment to db if gauged if self.catchment.record_length > 0: loaders.to_db(self.catchment, self.db_session, method='update', autocommit=True) self.msg_queue.put(Progress("Loading additional data.", 20)) loaders.userdata_to_db(self.db_session, autocommit=True) self.gauged_catchments = CatchmentCollections(self.db_session, load_data='manual') self.results['nrfa'] = fehdata.nrfa_metadata()
def test_l_skew_weight(self): subject = from_file('floodestimation/tests/data/37017.CD3') analysis = GrowthCurveAnalysis(subject) donor = copy(subject) donor.similarity_dist = 0.2010 result = analysis._l_skew_weight(donor) expected = 47.34 # Science Report SC050050, table 6.6, row 4 (note that donor has same record length as subject) self.assertAlmostEqual(result, expected, places=1)
def test_update_catchment(self): catchment = loaders.from_file('floodestimation/tests/data/17002.CD3') # Make a change catchment.location = "Dundee" loaders.to_db(catchment, self.session) # Reload catchment again duplicate_catchment = loaders.from_file( 'floodestimation/tests/data/17002.CD3') loaders.to_db(duplicate_catchment, self.session, method='update') self.session.flush() # Should have original data self.assertEqual( self.session.query(Catchment).get(17002).location, "Leven") self.session.rollback()
def test_nearest_catchments(self): subject_catchment = loaders.from_file( 'floodestimation/tests/data/17002.CD3') catchments = CatchmentCollections( self.db_session).nearest_qmed_catchments(subject_catchment) result = [catchment.id for catchment in catchments] expected = [17001, 10001, 10002] self.assertEqual(expected, result)
def test_most_similar_catchments(self): subject_catchment = loaders.from_file('floodestimation/tests/data/17002.CD3') # Dummy similarity distance function function = lambda c1, c2: abs(c2.descriptors.altbar - c1.descriptors.altbar) catchments = CatchmentCollections(self.db_session).most_similar_catchments(subject_catchment, function) result = [c.id for c in catchments] expected = [10001, 10002] self.assertEqual(expected, result)
def test_most_similar_catchments_excl_rejected_amax(self): subject_catchment = loaders.from_file('floodestimation/tests/data/17002.CD3') # Dummy similarity distance function function = lambda c1, c2: abs(c2.descriptors.bfihost - c1.descriptors.bfihost) catchments = CatchmentCollections(self.db_session).most_similar_catchments(subject_catchment, function, records_limit=36) result = [c.id for c in catchments] expected = [10002, 10001] self.assertEqual(expected, result)
def test_l_cv_and_skew(self): gauged_catchments = CatchmentCollections(self.db_session) catchment = from_file('floodestimation/tests/data/37017.CD3') analysis = GrowthCurveAnalysis(catchment, gauged_catchments) var, skew = analysis._var_and_skew(catchment) self.assertAlmostEqual(var, 0.2232, places=4) self.assertAlmostEqual(skew, -0.0908, places=4)
def test_l_cv_and_skew_one_donor_urban(self): catchment = from_file('floodestimation/tests/data/37017.CD3') analysis = GrowthCurveAnalysis(catchment, year=2000) analysis.donor_catchments = [catchment] var, skew = analysis._var_and_skew(analysis.donor_catchments) self.assertAlmostEqual(var, 0.21987, places=4) self.assertAlmostEqual(skew, -0.08746, places=4)
def test_incl_subject_catchment(self): # Subject catchment not in db subject_catchment = loaders.from_file('floodestimation/tests/data/37017.CD3') # Dummy similarity distance function function = lambda c1, c2: abs(c2.descriptors.altbar - c1.descriptors.altbar) catchments = CatchmentCollections(self.db_session).most_similar_catchments(subject_catchment, function, include_subject_catchment='force') result = [c.id for c in catchments] expected = [37017, 10002, 10001] self.assertEqual(expected, result) self.assertEqual(catchments[0].similarity_dist, 0)
def test_incl_subject_catchment_updated(self): # Subject catchment in db subject_catchment = loaders.from_file('floodestimation/tests/data/201002.CD3') subject_catchment.location = "Updated location name" # Dummy similarity distance function function = lambda c1, c2: abs(c2.descriptors.altbar - c1.descriptors.altbar) catchments = CatchmentCollections(self.db_session).most_similar_catchments(subject_catchment, function, include_subject_catchment='force') result = [c.id for c in catchments] expected = [201002, 10001, 10002] self.assertEqual(expected, result) self.assertEqual(catchments[0].location, "Updated location name")
def test_invalid_subj_catchment_option(self): subject_catchment = loaders.from_file( 'floodestimation/tests/data/17002.CD3') # Dummy similarity distance function function = lambda c1, c2: abs(c2.descriptors.altbar - c1.descriptors. altbar) self.assertRaises(ValueError, CatchmentCollections( self.db_session).most_similar_catchments, subject_catchment, function, include_subject_catchment='invalid')
def test_dist_params(self): catchment = from_file('floodestimation/tests/data/37017.CD3') analysis = GrowthCurveAnalysis(catchment) growth_curve = analysis.growth_curve(method='single_site') params = growth_curve.params self.assertAlmostEqual(params['loc'], 1) self.assertAlmostEqual(params['scale'], 0.2202, places=4) self.assertAlmostEqual(params['k'], 0.0908, places=4) # Kurtosis calculated from L-skew using eqns on # http://researcher.watson.ibm.com/researcher/view_group.php?id=2639 self.assertAlmostEqual(growth_curve.distr_kurtosis, 0.1735, places=4)
def test_l_skew_weight_same_catchment(self): subject = from_file('floodestimation/tests/data/37017.CD3') analysis = GrowthCurveAnalysis(subject) result = analysis._l_skew_weight(subject) expected = 116.66 # Science Report SC050050, table 6.6, row 1 self.assertAlmostEqual(result, expected, places=1)
def test_dist_param_location(self): catchment = from_file('floodestimation/tests/data/37017.CD3') analysis = GrowthCurveAnalysis(catchment) growth_curve = analysis.growth_curve(method='single_site') self.assertAlmostEqual(growth_curve(0.5), 1)
def test_single_site_gev(self): gauged_catchments = CatchmentCollections(self.db_session) catchment = from_file('floodestimation/tests/data/37017.CD3') analysis = GrowthCurveAnalysis(catchment, gauged_catchments) dist_func = analysis.growth_curve(method='single_site', distr='gev') self.assertAlmostEqual(dist_func(0.5), 1)
def test_invalid_subj_catchment_option(self): subject_catchment = loaders.from_file('floodestimation/tests/data/17002.CD3') # Dummy similarity distance function function = lambda c1, c2: abs(c2.descriptors.altbar - c1.descriptors.altbar) self.assertRaises(ValueError, CatchmentCollections(self.db_session).most_similar_catchments, subject_catchment, function, include_subject_catchment='invalid')
def test_load_catchment_without_amax(self): catchment = loaders.from_file('floodestimation/tests/data/170021.CD3') self.assertEqual([], catchment.amax_records)
def test_load_catchment_from_xml(self): catchment = loaders.from_file('floodestimation/tests/data/NN 04000 48400.xml') self.assertEqual(catchment.area, 30.09) self.assertEqual([], catchment.amax_records)
def test_nearest_catchments(self): subject_catchment = loaders.from_file('floodestimation/tests/data/17002.CD3') catchments = CatchmentCollections(self.db_session).nearest_qmed_catchments(subject_catchment) result = [catchment.id for catchment in catchments] expected = [17001, 10001, 10002] self.assertEqual(expected, result)
def test_catchment_by_number(self): expected = loaders.from_file('floodestimation/tests/data/17002.CD3') self.db_session.add(expected) result = CatchmentCollections(self.db_session).catchment_by_number(17002) self.assertIs(expected, result)
def test_load_catchment(self): catchment = loaders.from_file('floodestimation/tests/data/17002.CD3') self.assertEqual(17002, catchment.id) self.assertEqual(4, len(catchment.amax_records)) self.assertEqual(146, len(catchment.pot_dataset.pot_records))