def test_max_processors(self):
    """
    Check that creating new RegionEstimator with incorrect max_processors
    """

    with self.assertRaises(AssertionError):
        # Test that loading incorrect max_processors raises assertion
        estimation_data = EstimationData(self.sites, self.regions, self.actuals)
        estimator = ConcentricRegionsEstimator(estimation_data, verbose=0, max_processors=0)
        estimator = ConcentricRegionsEstimator(estimation_data, verbose=0, max_processors=500)
 def test_site_and_region_index_names(self):
   """
   Check that creating new RegionEstimator with incorrectly named indexes for sites and regions
   will return error  (should be site_id and region_id)
   """
   with self.assertRaises(AssertionError):
       # Test that loading incorrect sites index name raises assertion
       sites = pd.DataFrame(self.sites)
       sites.index = sites.index.rename('site_id')
       estimator = ConcentricRegionsEstimator(sites, self.regions, self.actuals)
       # Test that loading incorrect regions index name raises assertion
       regions = pd.DataFrame(self.regions)
       regions.index = regions.index.rename('postcode')
       estimation_data = EstimationData(self.sites, regions, self.actuals)
       estimator = ConcentricRegionsEstimator(estimation_data)
  def test_ignore_sites(self):
    """
    Test that a ConcentricRegionsEstimator object can be initialized with region data containing regions that are all touching
    and that the results are as expected when ignoring sites
    """
    estimation_data = EstimationData(self.sites, self.regions, self.actuals)
    estimator = ConcentricRegionsEstimator(estimation_data, verbose=0)
    result = estimator.get_estimations('NO2_mean', None, '2019-10-15', ignore_site_ids=['Camden Kerbside [AQ]'])

    #print('Result: \n {}'.format(result))
    #print('Target: \n {}'.format(self.results_ignore_sites))

    self.assertIsNotNone(estimator)
    self.assertIsNotNone(result)
    self.assertIsInstance(result, pd.DataFrame)
    self.assertTrue(result.equals(self.results_ignore_sites))
 def test_load_bad_verbose(self):
   """
   Test that a ConcentricRegionsEstimator and DistanceSimpleEstimator objects can
   be initialized with bad verbose.
   """
   estimation_data = EstimationData(self.sites, self.regions, self.actuals)
   with self.assertRaises(AssertionError):
     ConcentricRegionsEstimator(estimation_data, 'bad')
     DistanceSimpleEstimator(estimation_data, 2.14)
  def test_empty_measurements(self):
    """
        Test that a ConcentricRegionsEstimator object can be initialized with actuals containing empty measurment values
        and that the results are as expected
        """
    estimation_data = EstimationData(self.sites_empty_measurements, self.regions_empty_measurements,
                                     self.actuals_empty_measurements)
    estimator = ConcentricRegionsEstimator(estimation_data, verbose=0)

    result = estimator.get_estimations('alnus', None, '2017-06-15')

    #print('Result: \n {}'.format(result))
    #print('Target: \n {}'.format(self.results_empty_measurements))

    self.assertIsNotNone(estimator)
    self.assertIsNotNone(result)
    self.assertIsInstance(result, pd.DataFrame)
    self.assertTrue(result.fillna(np.NaN).equals(self.results_empty_measurements))
  def test_non_touching(self):
    """
    Test that a ConcentricRegionsEstimator object can be initialized with region data containing regions that are
    not  touching and that the results are as expected
    """
    estimation_data = EstimationData(self.sites_non_touching, self.regions_non_touching, self.actuals_non_touching)
    estimator_non_touching = ConcentricRegionsEstimator(estimation_data, verbose=0)
    self.assertEqual(estimator_non_touching.estimation_data.get_adjacent_regions(['PE']), [])
    self.assertEqual(estimator_non_touching.estimation_data.get_adjacent_regions(['TD']), [])
    result = estimator_non_touching.get_estimations('NO2_mean', None, '2019-10-15').fillna(value=np.NaN)

    #print('Non Touching: \n {}'.format(result))
    #print('Non Touching target: \n {}'.format(self.results_non_touching))

    self.assertIsNotNone(estimator_non_touching)
    self.assertIsNotNone(result)
    self.assertIsInstance(result, pd.DataFrame)
    self.assertTrue(result.equals(self.results_non_touching))
  def test_islands(self):
    """
    Test that a ConcentricRegionsEstimator object can be initialized with region data containing islands
    and that the results are as expected for islands
    """
    estimation_data = EstimationData(self.sites_islands, self.regions_islands, self.actuals_islands)
    estimator_islands = ConcentricRegionsEstimator(estimation_data, verbose=0)
    self.assertEqual(estimator_islands.estimation_data.get_adjacent_regions(['IM']), [])
    self.assertEqual(estimator_islands.estimation_data.get_adjacent_regions(['BT']), [])
    result = estimator_islands.get_estimations('NO2_mean', None, '2019-10-15').fillna(value=np.NaN)

    #print('Islands results: \n {}'.format(result))
    #print('Islands target: \n {}'.format(self.results_islands))

    self.assertIsNotNone(estimator_islands)
    self.assertIsNotNone(result)
    self.assertIsInstance(result, pd.DataFrame)
    self.assertTrue(result.equals(self.results_islands))
  def test_overlapping(self):
    """
    Test that a ConcentricRegionsEstimator object can be initialized with region data containing regions that are overlapping
    and that the results are as expected
    """
    estimation_data = EstimationData(self.sites_overlap, self.regions_overlap, self.actuals_overlap)
    estimator_overlap = ConcentricRegionsEstimator(estimation_data, verbose=0)
    self.assertEqual(estimator_overlap.estimation_data.get_adjacent_regions(['AB']), ['DD', 'IV', 'PH'])
    self.assertEqual(estimator_overlap.estimation_data.get_adjacent_regions(['AB2']), ['DD', 'IV', 'PH'])
    self.assertEqual(estimator_overlap.estimation_data.get_adjacent_regions(['DD']), ['AB','AB2', 'AB3', 'PH'])
    result = estimator_overlap.get_estimations('NO2_mean', None, '2019-10-15')

    #print('Overlap: \n {}'.format(result))
    #print('Overlap target: \n {}'.format(self.results_overlap))

    self.assertIsNotNone(estimator_overlap)
    self.assertIsNotNone(result)
    self.assertIsInstance(result, pd.DataFrame)
    self.assertTrue(result.equals(self.results_overlap))
  def test_ok_files(self):
    """
    Test that a ConcentricRegionsEstimator object can be initialized with region data containing regions
    that are all touching and that the results are as expected
    """
    estimation_data = EstimationData(self.sites, self.regions, self.actuals)
    estimator = ConcentricRegionsEstimator(estimation_data, verbose=0, progress_callback=process_progress)

    self.assertIsNotNone(estimator.regions['neighbours'])
    self.assertEqual(estimator.estimation_data.get_adjacent_regions(['SW']), ['CR', 'KT', 'SE', 'SM', 'TW', 'W', 'WC'])
    result = estimator.get_estimations('NO2_mean', None, '2019-10-15')

    #print('Result: \n {}'.format(result))
    #print('Target: \n {}'.format(self.results))
    #print('Compare: \n {}'.format(result.compare(self.results)))

    self.assertIsNotNone(estimator)
    self.assertIsNotNone(result)
    self.assertIsInstance(result, pd.DataFrame)
    self.assertTrue(result.equals(self.results))
  def test_load_regions_with_bad_data(self):
    """
    Check that loading bad regions data will fail.
    """
    bad_files = [
      'regions_no_geometry.csv'
    ]

    for file in bad_files:
      with self.subTest(file=file):
        with self.assertRaises(AssertionError):
          bad_regions = pd.read_csv(path.join(self.load_data, file))
          estimation_data = EstimationData(self.sites, bad_regions, self.actuals)
          estimator = ConcentricRegionsEstimator(estimation_data)
  def test_load_actuals_with_bad_data(self):
    """
    Check that loading bad actuals data will fail.
    """
    bad_files = [
      'actuals_no_id.csv',
      'actuals_no_timestamp.csv',
      'actuals_no_measurements.csv',
      'actuals_bad_values.csv'
    ]

    for file in bad_files:
      with self.subTest(file=file):
        with self.assertRaises(AssertionError):
          bad_actuals = pd.read_csv(path.join(self.load_data, file))
          estimation_data = EstimationData(self.sites, self.regions, bad_actuals)
          estimator = ConcentricRegionsEstimator(estimation_data)
  def test_get_region_sites(self):
    """
    Check that get_region_sites works with good and bad inputs
    """
    estimation_data = EstimationData(self.sites, self.regions, self.actuals)
    estimator = ConcentricRegionsEstimator(estimation_data)

    with self.assertRaises(AssertionError):
        # Test that region_id not in regions raises assertion error
        estimator.estimation_data.get_region_sites('WC')

    # Test a region_id known to be present in regions and has sites
    region_sites = estimator.estimation_data.get_region_sites('DG')
    self.assertEqual(region_sites, ['1023 [POLLEN]', '1023 [WEATHER]'])

    # Test a region_id known to be present in regions and does not contain sites
    region_no_sites = estimator.estimation_data.get_region_sites('AB')
    self.assertEqual(region_no_sites, [])
 def test_site_datapoint_count(self):
   estimation_data = EstimationData(self.sites, self.regions, self.actuals)
   estimator = ConcentricRegionsEstimator(estimation_data, verbose=0)
   self.assertTrue(estimator.estimation_data.site_datapoint_count('NO2_mean', '2019-10-15') > 0)