def test_all_optional_args_multiple_sites(): """Test we create a datafram even when we have all optional arguements set and multiple spots""" # Set up expected dataframe. data = [[600, "air_temperature", 0, 280.], [600, "air_temperature", 1, 280.], [600, "air_temperature", 2, 280.]] columns = ["validity_time", "cf_name", "site", "T+000"] expected_df = pd.DataFrame(data, columns=columns) expected_df.set_index(["validity_time", "cf_name", "site"], inplace=True) expected_df.columns.name = "forecast_period" plugin = SpotDatabase( "csv", "output", "improver", "time", primary_map=['validity_time'], primary_func=[lambda x: dt.utcfromtimestamp(x).hour * 100], pivot_dim='forecast_period', pivot_map=lambda x: 'T+{:03d}'.format(int(x / 3600)), column_dims=['name', "index"], column_maps=['cf_name', "site"], coord_to_slice_over="index") # Call the method. cube = set_up_spot_cube( 280, number_of_sites=3, ) plugin.to_dataframe(CubeList([cube])) assert_frame_equal(plugin.df, expected_df)
def test_all_optional_args_multiple_input_cubes(self): """Test we create a dataframe even when we have no optional arguements set and multiple cubes""" # Set up expected dataframe. data = [[600, "air_temperature", 280., np.nan, np.nan], [700, "air_temperature", np.nan, 281., np.nan], [800, "air_temperature", np.nan, np.nan, 282.]] columns = ["validity_time", "cf_name", "T+000", "T+001", "T+002"] expected_df = pd.DataFrame(data, columns=columns) expected_df.set_index(["validity_time", "cf_name"], inplace=True) expected_df.columns.name = "forecast_period" plugin = SpotDatabase( "csv", "output", "improver", "time", primary_map=['validity_time'], primary_func=[lambda x: dt.utcfromtimestamp(x).hour * 100], pivot_dim='forecast_period', pivot_map=lambda x: 'T+{:03d}'.format(int(x / 3600)), column_dims=['name'], column_maps=['cf_name'], coord_to_slice_over="index") # Call the method. plugin.to_dataframe(self.cubelist_multiple) assert_frame_equal(plugin.df, expected_df)
class Test_determine_schema(IrisTest): """A set of tests for the determine_schema method""" def setUp(self): """Set up the plugin and dataframe needed for this test""" cubes = CubeList([set_up_spot_cube(280)]) self.plugin = SpotDatabase("csv", "output", "improver", "time", "index") self.dataframe = self.plugin.to_dataframe(cubes) def test_full_schema(self): """Basic test using a basic dataframe as input""" schema = self.plugin.determine_schema("improver") expected_schema = 'CREATE TABLE "improver" (\n"index" INTEGER,\n '\ '"values" REAL,\n CONSTRAINT improver_pk '\ 'PRIMARY KEY ("index")\n)' self.assertEqual(schema, expected_schema)
class Test_to_dataframe(IrisTest): """Test the to_dataframe method""" def setUp(self): """Set up the plugin and dataframe needed for these tests""" self.cube = set_up_spot_cube(280, number_of_sites=1) self.cube2 = set_up_spot_cube(281, number_of_sites=1, validity_time=1487311200 + 3600, forecast_period=1) self.cube3 = set_up_spot_cube(282, number_of_sites=1, validity_time=1487311200 + 7200, forecast_period=2) self.cubelist = CubeList([self.cube]) self.cubelist_multiple = CubeList([self.cube, self.cube2, self.cube3]) self.plugin = SpotDatabase("csv", "output", "improver", "time", "index") data = [[1487311200, 280.]] columns = ["time", "values"] self.input_df = pd.DataFrame(data, columns=columns) @ManageWarnings(ignored_messages=IGNORED_MESSAGES, warning_types=WARNING_TYPES) def test_no_optional_args(self): """Test we create a datafram even when we have no optional arguements set""" # Set up expected dataframe. data = [[280.]] columns = ["values"] expected_df = pd.DataFrame(data, index=[1487311200], columns=columns) # Call the method. self.plugin.to_dataframe(self.cubelist) assert_frame_equal(self.plugin.df, expected_df) @ManageWarnings(ignored_messages=IGNORED_MESSAGES, warning_types=WARNING_TYPES) def test_all_optional_args(self): """Test we create a datafram even when we have all optional arguements set""" # Set up expected dataframe. data = [[600, "air_temperature", 280.]] columns = ["validity_time", "cf_name", "T+000"] expected_df = pd.DataFrame(data, columns=columns) expected_df.set_index(["validity_time", "cf_name"], inplace=True) expected_df.columns.name = "forecast_period" plugin = SpotDatabase( "csv", "output", "improver", "time", primary_map=['validity_time'], primary_func=[lambda x: dt.utcfromtimestamp(x).hour * 100], pivot_dim='forecast_period', pivot_map=lambda x: 'T+{:03d}'.format(int(x / 3600)), column_dims=['name'], column_maps=['cf_name'], coord_to_slice_over="index") # Call the method. plugin.to_dataframe(self.cubelist) assert_frame_equal(plugin.df, expected_df) @ManageWarnings(ignored_messages=IGNORED_MESSAGES, warning_types=WARNING_TYPES) def test_all_optional_args_multiple_input_cubes(self): """Test we create a dataframe even when we have no optional arguements set and multiple cubes""" # Set up expected dataframe. data = [[600, "air_temperature", 280., np.nan, np.nan], [700, "air_temperature", np.nan, 281., np.nan], [800, "air_temperature", np.nan, np.nan, 282.]] columns = ["validity_time", "cf_name", "T+000", "T+001", "T+002"] expected_df = pd.DataFrame(data, columns=columns) expected_df.set_index(["validity_time", "cf_name"], inplace=True) expected_df.columns.name = "forecast_period" plugin = SpotDatabase( "csv", "output", "improver", "time", primary_map=['validity_time'], primary_func=[lambda x: dt.utcfromtimestamp(x).hour * 100], pivot_dim='forecast_period', pivot_map=lambda x: 'T+{:03d}'.format(int(x / 3600)), column_dims=['name'], column_maps=['cf_name'], coord_to_slice_over="index") # Call the method. plugin.to_dataframe(self.cubelist_multiple) assert_frame_equal(plugin.df, expected_df) @staticmethod @ManageWarnings(ignored_messages=IGNORED_MESSAGES, warning_types=WARNING_TYPES) def test_all_optional_args_multiple_sites(): """Test we create a datafram even when we have all optional arguements set and multiple spots""" # Set up expected dataframe. data = [[600, "air_temperature", 0, 280.], [600, "air_temperature", 1, 280.], [600, "air_temperature", 2, 280.]] columns = ["validity_time", "cf_name", "site", "T+000"] expected_df = pd.DataFrame(data, columns=columns) expected_df.set_index(["validity_time", "cf_name", "site"], inplace=True) expected_df.columns.name = "forecast_period" plugin = SpotDatabase( "csv", "output", "improver", "time", primary_map=['validity_time'], primary_func=[lambda x: dt.utcfromtimestamp(x).hour * 100], pivot_dim='forecast_period', pivot_map=lambda x: 'T+{:03d}'.format(int(x / 3600)), column_dims=['name', "index"], column_maps=['cf_name', "site"], coord_to_slice_over="index") # Call the method. cube = set_up_spot_cube( 280, number_of_sites=3, ) plugin.to_dataframe(CubeList([cube])) assert_frame_equal(plugin.df, expected_df)