Example #1
0
 def test_get_flu_model_for_model_id_and_dates(self):
     """
     Scenario: Get model data and scores for model_id and dates
     """
     with self.app.app_context():
         flu_model = FluModel()
         flu_model.id = 1
         flu_model.is_displayed = True
         flu_model.is_public = True
         flu_model.calculation_parameters = ''
         flu_model.name = 'Model 1'
         flu_model.source_type = 'google'
         flu_model.save()
         model_function = ModelFunction()
         model_function.flu_model_id = 1
         model_function.has_confidence_interval = True
         model_function.function_name = 'Function name'
         model_function.average_window_size = 7
         model_function.save()
         for i in range(1, 32):
             model_score = ModelScore()
             model_score.flu_model_id = 1
             model_score.score_date = date(2018, 1, i)
             model_score.score_value = i / (10 + i)
             model_score.region = 'e'
             model_score.save()
         result = get_flu_model_for_model_id_and_dates(
             1, date(2018, 1, 2), date(2018, 1, 31))
         self.assertEqual(result[0]['average_score'], 0.5723146873461765)
         self.assertEqual(result[0]['start_date'], date(2018, 1, 2))
         self.assertEqual(result[0]['end_date'], date(2018, 1, 31))
         self.assertEqual(result[0]['name'], 'Model 1')
         self.assertEqual(result[0]['has_confidence_interval'], True)
         self.assertEqual(result[0]['id'], 1)
         self.assertEqual(len(result[1]), 30)
 def test_run_model(self):
     """
     Given FluModel can be found
     Then Scheduler executes run_model for that particular FluModel id
     """
     flumodel = FluModel()
     flumodel.id = 1
     flumodel.name = 'Test Model'
     flumodel.is_public = True
     flumodel.is_displayed = True
     flumodel.source_type = 'google'
     flumodel.calculation_parameters = 'matlab_model,1'
     datapoint = ModelScore()
     datapoint.region = 'e'
     datapoint.score_date = date(2018, 6, 29)
     datapoint.calculation_timestamp = datetime.now()
     datapoint.score_value = '1.23'
     flumodel.model_scores = [datapoint]
     with self.app.app_context():
         flumodel.save()
         with patch.object(Scheduler, '__init__', return_value=None):
             scheduler = Scheduler()
             scheduler.flask_app = self.app
             from apscheduler.schedulers.background import BackgroundScheduler
             scheduler.scheduler = BackgroundScheduler()
             scheduler.run_model([flumodel.id], "* * * * *")
Example #3
0
 def test_get_flu_models_for_ids(self):
     """
     Scenario: Get a list of FluModels from an array of ids
     """
     with self.app.app_context():
         for idx in range(1, 4):
             flu_model = FluModel()
             flu_model.id = idx
             flu_model.name = 'Test model %d' % idx
             flu_model.source_type = 'google'
             flu_model.is_public = True
             flu_model.is_displayed = True
             flu_model.calculation_parameters = 'matlab_function,1'
             flu_model.save()
         result = get_flu_models_for_ids([1, 2])
         self.assertEqual(len(result), 2)
Example #4
0
 def test_get_flu_model(self):
     """
     Scenario: Get a FluModel entity from its id
     """
     with self.app.app_context():
         flu_model = FluModel()
         flu_model.id = 1
         flu_model.name = 'Test model 1'
         flu_model.source_type = 'google'
         flu_model.is_public = True
         flu_model.is_displayed = True
         flu_model.calculation_parameters = 'matlab_function,1'
         flu_model.save()
         result = get_flu_model_for_id(1)
         self.assertIsNotNone(result)
         self.assertIsInstance(result, FluModel)
Example #5
0
 def test_get_all_flu_models(self):
     """
     Scenario: Retrieve a list of all FluModel entities
     """
     with self.app.app_context():
         for idx in range(1, 3):
             flu_model = FluModel()
             flu_model.id = idx
             flu_model.name = 'Test model %d' % idx
             flu_model.source_type = 'google'
             flu_model.is_public = idx % 2 == 0
             flu_model.is_displayed = True
             flu_model.calculation_parameters = 'matlab_function,1'
             flu_model.save()
         result = get_all_flu_models()
         self.assertIsNotNone(result)
         self.assertEquals(len(result), 2)
         self.assertIsInstance(result[0], FluModel)
Example #6
0
 def test_get_default_flu_model(self):
     """
     Scenario: Get the default flu model
     """
     with self.app.app_context():
         flu_model = FluModel()
         flu_model.id = 1
         flu_model.is_displayed = True
         flu_model.is_public = True
         flu_model.calculation_parameters = ''
         flu_model.name = 'Model 1'
         flu_model.source_type = 'google'
         flu_model.save()
         default_model = DefaultFluModel()
         default_model.flu_model_id = 1
         DB.session.add(default_model)
         DB.session.commit()
         result = get_default_flu_model()
         self.assertEqual(result.name, 'Model 1')
Example #7
0
 def test_post_config(self):
     flumodel = FluModel()
     flumodel.id = 1
     flumodel.name = 'Test Model'
     flumodel.is_public = True
     flumodel.is_displayed = True
     flumodel.source_type = 'google'
     flumodel.calculation_parameters = 'matlab_model,1'
     token_info = TokenInfo()
     token_info.token_id = 1
     token_info.token = '79e11f5137ab996c5e202dc0166a68d4e3bece0af5b39c30705905210ee6e9a4'
     token_info.is_valid = True
     token_info.token_user = '******'
     with self.app.app_context():
         flumodel.save()
         token_info.save()
     response = self.client().post(
         '/config',
         data=dict(model_id=1, is_displayed=True),
         headers={
             'Authorization': 'Token 5GOngP4EbHiwA4R32bv516tpKkBEAOl8'
         })
     self.assertEqual(response.status_code, 200)
Example #8
0
 def test_csv(self):
     flumodel = FluModel()
     flumodel.id = 1
     flumodel.name = 'Test Model'
     flumodel.is_public = True
     flumodel.is_displayed = True
     flumodel.source_type = 'google'
     flumodel.calculation_parameters = 'matlab_model,1'
     dates = [date(2018, 6, d) for d in range(1, 30)]
     datapoints = []
     for d in dates:
         entry = ModelScore()
         entry.region = 'e'
         entry.score_date = d
         entry.calculation_timestamp = datetime.now()
         entry.score_value = 1 + 10 / d.day
         entry.confidence_interval_lower = 0
         entry.confidence_interval_upper = 2
         datapoints.append(entry)
     flumodel.model_scores = datapoints
     model_function = ModelFunction()
     model_function.id = 1
     model_function.function_name = 'matlab_model'
     model_function.average_window_size = 1
     model_function.flu_model_id = 1
     model_function.has_confidence_interval = True
     with self.app.app_context():
         flumodel.save()
         model_function.save()
         response = self.client().get(
             '/csv?id=1&tartDate=2018-06-01&endDate=2018-06-07&resolution=week'
         )
         expected_header = r'attachment; filename=RawScores-\d{13}\.csv'
         expected_data = b'score_date,score_Test Model\r\n2018-06-03,4.333333333333334\r\n'
         self.assertEquals(response.data, expected_data)
         self.assertRegexpMatches(response.headers['Content-Disposition'],
                                  expected_header)
Example #9
0
 def test_get_default_model_30days(self):
     """
     Scenario: Get the last 30 days of data of the default flu model
     """
     with self.app.app_context():
         flu_model = FluModel()
         flu_model.id = 1
         flu_model.is_displayed = True
         flu_model.is_public = True
         flu_model.calculation_parameters = ''
         flu_model.name = 'Model 1'
         flu_model.source_type = 'google'
         flu_model.save()
         default_model = DefaultFluModel()
         default_model.flu_model_id = 1
         model_function = ModelFunction()
         model_function.flu_model_id = 1
         model_function.has_confidence_interval = True
         model_function.function_name = 'Function name'
         model_function.average_window_size = 7
         model_function.save()
         DB.session.add(default_model)
         DB.session.commit()
         for i in range(1, 32):
             model_score = ModelScore()
             model_score.flu_model_id = 1
             model_score.score_date = date(2018, 1, i)
             model_score.score_value = i / (10 + i)
             model_score.region = 'e'
             model_score.save()
         result = get_default_flu_model_30days()
         self.assertEqual(result[0]['average_score'], 0.5723146873461765)
         self.assertEqual(result[0]['start_date'], date(2018, 1, 2))
         self.assertEqual(result[0]['end_date'], date(2018, 1, 31))
         self.assertEqual(result[0]['name'], 'Model 1')
         self.assertEqual(result[0]['id'], 1)
         self.assertEqual(len(result[1]), 30)
Example #10
0
 def test_build_root_plink_twlink_response(self):
     model_list = [{
         'id': 1,
         'name': 'Test Model'
     }, {
         'id': 2,
         'name': 'Another Test Model'
     }]
     rate_thresholds = {
         'very_high_value': {
             'label': 'Very high epidemic rate',
             'value': 0.4
         },
         'high_value': {
             'label': 'High epidemic rate',
             'value': 0.3
         },
         'medium_value': {
             'label': 'Medium epidemic rate',
             'value': 0.2
         },
         'low_value': {
             'label': 'Low epidemic rate',
             'value': 0.1
         }
     }
     data_points = [{
         'score_date': '2019-01-01',
         'score_value': 0.2,
         'confidence_interval_lower': 0.1,
         'confidence_interval_upper': 0.5
     }, {
         'score_date': '2019-01-02',
         'score_value': 0.4,
         'confidence_interval_lower': 0.1,
         'confidence_interval_upper': 0.5
     }]
     model_data = [{
         'id': 1,
         'name': 'Test Model',
         'start_date': '2019-01-01',
         'end_date': '2019-01-02',
         'average_score': 0.3,
         'has_confidence_interval': True,
         'data_points': data_points
     }]
     expected = {
         'model_list': model_list,
         'rate_thresholds': rate_thresholds,
         'model_data': model_data
     }
     flu_model_1, flu_model_2 = FluModel(), FluModel()
     flu_model_1.id, flu_model_2.id = 1, 2
     flu_model_1.name, flu_model_2.name = 'Test Model', 'Another Test Model'
     flu_model_list = [flu_model_1, flu_model_2]
     flu_model_score_1, flu_model_score_2 = ModelScore(), ModelScore()
     flu_model_score_1.score_date, flu_model_score_2.score_date = date(
         2019, 1, 1), date(2019, 1, 2)
     flu_model_score_1.score_value, flu_model_score_2.score_value = 0.2, 0.4
     flu_model_score_1.confidence_interval_lower, flu_model_score_2.confidence_interval_lower = 0.1, 0.1
     flu_model_score_1.confidence_interval_upper, flu_model_score_2.confidence_interval_upper = 0.5, 0.5
     flu_model_data = [({
         'id': 1,
         'name': 'Test Model',
         'start_date': date(2019, 1, 1),
         'end_date': date(2019, 1, 2),
         'has_confidence_interval': True,
         'average_score': 0.3
     }, [flu_model_score_1, flu_model_score_2])]
     result = build_root_plink_twlink_response(flu_model_list,
                                               rate_thresholds,
                                               flu_model_data)
     self.assertEquals(result, expected)
Example #11
0
 def test_get_twlink_current_link(self):
     flumodel = FluModel()
     flumodel.id = 1
     flumodel.name = 'Test Model'
     flumodel.is_public = True
     flumodel.is_displayed = True
     flumodel.source_type = 'google'
     flumodel.calculation_parameters = 'matlab_model,1'
     flumodel.model_region_id = '1-e'
     dates = [date(2018, 6, d) for d in range(1, 30)]
     datapoints = []
     for d in dates:
         entry = ModelScore()
         entry.region = 'e'
         entry.score_date = d
         entry.calculation_timestamp = datetime.now()
         entry.score_value = 1.23
         entry.confidence_interval_lower = 0.81
         entry.confidence_interval_upper = 1.65
         datapoints.append(entry)
     flumodel.model_scores = datapoints
     model_function = ModelFunction()
     model_function.id = 1
     model_function.function_name = 'matlab_model'
     model_function.average_window_size = 1
     model_function.flu_model_id = 1
     model_function.has_confidence_interval = True
     default_model = DefaultFluModel()
     default_model.flu_model_id = 1
     rate_thresholds = RateThresholdSet()
     rate_thresholds.low_value = 0.1
     rate_thresholds.medium_value = 0.2
     rate_thresholds.high_value = 0.3
     rate_thresholds.very_high_value = 0.4
     rate_thresholds.valid_from = date(2010, 1, 1)
     with self.app.app_context():
         flumodel.save()
         model_function.save()
         rate_thresholds.save()
         DB.session.add(default_model)
         DB.session.commit()
     response = self.client().get(
         '/twlink?id=1&start=2018-06-01&end=2018-06-10')
     self.assertListEqual(response.get_json()['model_list'],
                          [{
                              'id': 1,
                              'name': 'Test Model'
                          }])
     self.assertEqual(
         len(response.get_json()['model_data'][0]['data_points']), 10)
     expected = {
         'low_value': {
             'label': 'Low epidemic rate',
             'value': 0.1
         },
         'medium_value': {
             'label': 'Medium epidemic rate',
             'value': 0.2
         },
         'high_value': {
             'label': 'High epidemic rate',
             'value': 0.3
         },
         'very_high_value': {
             'label': 'Very high epidemic rate',
             'value': 0.4
         }
     }
     self.assertDictEqual(response.get_json()['rate_thresholds'], expected)