def test_get_long_and_short_gap_annotations(self): annotation_data = pd.DataFrame({ 'city': ['a'] * 2, 'date': [date(2016, 2, 1), date(2017, 9, 1)], 'annotation_date': [date(2016, 1, 1), date(2017, 1, 1)], 'damage': [1, 1], }) threshold = timedelta(days=30 * 6) output = self.annotation_maker._get_long_and_short_gap_annotations( annotation_data, threshold) expected_output_0 = pd.DataFrame({ 'city': ['a'], 'date': [date(2017, 9, 1)], 'annotation_date': [date(2017, 1, 1)], 'damage': [1], }) expected_output_1 = pd.DataFrame({ 'city': ['a'], 'date': [date(2016, 2, 1)], 'annotation_date': [date(2016, 1, 1)], 'damage': [1], }) assertFrameEqual(output[0], expected_output_0) assertFrameEqual(output[1], expected_output_1)
def test_combine_annotations_and_rasters(self): annotation_data = pd.DataFrame({ 'city': ['a', 'a'], 'patch_id': ['1', '2'], 'date': [ date(2017, 5, 20), date(2017, 5, 20), ], 'damage_num': [1, 3], }).set_index(['city', 'patch_id', 'date']) raster_data = pd.DataFrame({ 'city': ['a'] * 9, 'patch_id': ['1', '2', '3', '1', '2', '3', '1', '2', '3'], 'date': [ date(2016, 5, 20), date(2016, 5, 20), date(2016, 5, 20), date(2017, 5, 20), date(2017, 5, 20), date(2017, 5, 20), date(2018, 5, 20), date(2018, 5, 20), date(2018, 5, 20), ], 'image': [f'image_{i}' for i in range(9)], }).set_index(['city', 'patch_id', 'date']) expected_output = pd.DataFrame({ 'city': ['a'] * 9, 'patch_id': ['1', '2', '3', '1', '2', '3', '1', '2', '3'], 'date': [ date(2016, 5, 20), date(2016, 5, 20), date(2016, 5, 20), date(2017, 5, 20), date(2017, 5, 20), date(2017, 5, 20), date(2018, 5, 20), date(2018, 5, 20), date(2018, 5, 20), ], 'annotation_date': [ np.nan, np.nan, np.nan, date(2017, 5, 20), date(2017, 5, 20), date(2017, 5, 20), np.nan, np.nan, np.nan, ], 'destroyed': [0, np.nan, 0, 0, 1, 0, np.nan, 1, np.nan], 'image': [f'image_{i}' for i in range(9)], }).sort_values(['city', 'patch_id', 'date']) expected_output['annotation_date'] = pd.to_datetime( expected_output['annotation_date']) expected_output['date'] = pd.to_datetime(expected_output['date']) output = self.annotation_maker._combine_annotations_and_rasters( annotation_data, raster_data) assertFrameEqual(output, expected_output)
def test_get_raster_dates_with_annotation_data(self): raster_dates = pd.DataFrame({ 'city': ['a'] * 3, 'raster_date': [date(2015, 1, 1), date(2016, 2, 1), date(2017, 9, 1)], 'annotation_date': [None, date(2016, 1, 1), date(2017, 1, 1)], }) annotation_data = pd.DataFrame({ 'date': [ date(2016, 1, 1), date(2017, 1, 1), date(2017, 10, 1), date(2018, 1, 1) ], 'city': ['a'] * 4, 'damage': [1] * 4, }) output = self.annotation_maker._get_raster_dates_with_annotation_data( raster_dates, annotation_data) expected_output = pd.DataFrame({ 'city': ['a'] * 2, 'date': [date(2016, 2, 1), date(2017, 9, 1)], 'annotation_date': [date(2016, 1, 1), date(2017, 1, 1)], 'damage': [1, 1], }) assertFrameEqual(output, expected_output)
def test_make_raster_pairs_all_cities_expected_input_returns_expected_output( self): raster_data = pd.DataFrame({ 'patch_id': [0, 0], 'image': [ np.array([[[0, 0, 0], [0, 0, 0], [0, 0, 0]]]), np.array([[[1, 1, 1], [1, 1, 1], [1, 1, 1]]]) ], 'date': [date(2017, 1, 1), date(2018, 1, 1)], 'raster_date': [date(2017, 1, 1), date(2018, 1, 1)], 'city': ['a', 'a'], }) output = self.raster_pair_maker._make_raster_pairs_all_cities( raster_data) expected_output = pd.DataFrame({ 'patch_id': [0], 'image': [ np.array([[[1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0]]]) ], 'date': [date(2018, 1, 1)], 'raster_date': [date(2018, 1, 1)], 'city': ['a'], }) assertFrameEqual(output, expected_output)
def test_make_single_raster_pair_expected_input_returns_expected_output( self): first_raster = pd.DataFrame({ 'city': ['a'], 'patch_id': [0], 'image': [np.array([[[0, 0, 0], [0, 0, 0], [0, 0, 0]]])], 'date': [date(2018, 1, 1)], }) posterior_raster = pd.DataFrame({ 'city': ['a'], 'patch_id': [0], 'image': [np.array([[[1, 1, 1], [1, 1, 1], [1, 1, 1]]])], 'raster_date': [date(2018, 1, 1)], 'date': [date(2018, 1, 1)], }) raster_pair = self.raster_pair_maker._make_single_raster_pair( first_raster, posterior_raster) expected_output = pd.DataFrame({ 'city': ['a'], 'patch_id': [0], 'image': [ np.array([[[1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0]]]) ], 'raster_date': [date(2018, 1, 1)], 'date': [date(2018, 1, 1)], }) assertFrameEqual(raster_pair, expected_output)
def test_combine_long_and_short_gap_annotations_with_raster_data(self): short_gap = pd.DataFrame({ 'city': ['a'] * 2, 'date': [date(2016, 2, 1), date(2016, 2, 1)], 'annotation_date': [date(2016, 1, 1), date(2016, 1, 1)], 'location_index': [1, 2], 'damage_num': [1, 1], }) long_gap = pd.DataFrame({ 'city': ['a'] * 2, 'date': [date(2017, 9, 1), date(2017, 9, 1)], 'annotation_date': [date(2017, 1, 1), date(2017, 1, 1)], 'location_index': [0, 1], 'damage_num': [1, 1], }) raster_data = pd.DataFrame({ 'city': ['a'] * 4, 'date': [ date(2016, 2, 1), date(2016, 2, 1), date(2017, 9, 1), date(2017, 9, 1) ], 'location_index': [1, 3, 1, 2], 'patch_id': ['a', 'b', 'a', 'b'], }) expected_output = pd.DataFrame({ 'city': ['a'] * 3, 'date': [date(2016, 2, 1), date(2016, 2, 1), date(2017, 9, 1)], 'annotation_date': [date(2016, 1, 1), np.nan, date(2017, 1, 1)], 'location_index': [1, 3, 1], 'damage_num': [1., 0., 1.], 'patch_id': ['a', 'b', 'a'], }) output = self.annotation_maker._combine_long_and_short_gap_annotations_with_raster_data( long_gap, short_gap, raster_data) assertFrameEqual(output, expected_output)
def test_combine_annotation_data(self): city_a_data = pd.DataFrame({ 'city': ['a'], 'dmg': ['Destroyed'], }) city_b_data = pd.DataFrame({ 'city': ['b'], 'dmg': ['Severe Damage'], }) annotation_data = { 'annotation_city_a': city_a_data, 'annotation_city_b': city_b_data, } expected_output = pd.DataFrame({ 'city': ['a', 'b'], 'dmg': ['Destroyed', 'Severe Damage'], }) output = self.annotation_maker._combine_annotation_data( annotation_data) assertFrameEqual(output, expected_output)