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)
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_unknown_output_type(self): """Test what happens if you give an unknown output type.""" plugin = SpotDatabase("kitten", self.data_directory + "/test.csv", "improver", "time", "index") message = ("Unrecognised output type. Current options are 'sqlite'" " or 'csv', 'kitten' given.") with self.assertRaisesRegex(ValueError, message): plugin.process(self.cubes)
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.plugin = SpotDatabase("csv", "output", "improver", "time", "index") data = [[1487311200, 280.]] columns = ["time", "values"] self.input_df = pd.DataFrame(data, columns=columns)
def setUp(self): """Set up the plugin and dataframe needed for these tests""" self.cube = set_up_spot_cube(280, number_of_sites=1) second_cube = self.cube.copy() second_cube.coord("percentile").points = np.array([60.0]) cubelist = CubeList([self.cube, second_cube]) self.cubes = cubelist.concatenate() self.plugin = SpotDatabase("csv", "output", "improver", "time", "IMPRO", 0)
def test_no_exception_if_pivot_dim_set(self): """Test it raises an exception if a 2D cube is input and no pivot_dim.""" plugin = SpotDatabase("csv", "output", "improver", "time", "IMPRO", 0, pivot_dim="percentile") self.assertFalse(plugin.check_input_dimensions(self.cubes[0]))
def test_basic_repr(self): """Basic test of string representation""" expected_result = ('<SpotDatabase: csv, output, improver, time, index,' ' primary_map=None, ' 'primary_func=None, ' 'pivot_dim=None, ' 'pivot_map=None, ' 'column_dims=None, ' 'column_maps=None>') result = str(SpotDatabase("csv", "output", "improver", "time", "index")) self.assertEqual(expected_result, result)
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.plugin = SpotDatabase( "csv", "output", "improver", "time", "index", pivot_map=lambda x: 'T+{:03d}'.format(int(x / 3600)), pivot_dim="forecast_period") data = [[1487311200, 280.]] columns = ["time", "values"] self.input_df = pd.DataFrame(data, columns=columns)
def test_repr_after_dataframe_created(self): """Basic test of string representation after a database has been created and added to self.""" expected_result = ('<SpotDatabase: csv, output, improver, time, index,' ' primary_map=None, ' 'primary_func=None, ' 'pivot_dim=None, ' 'pivot_map=None, ' 'column_dims=None, ' 'column_maps=None>') plugin = SpotDatabase("csv", "output", "improver", "time", "index") plugin.df = pd.DataFrame() result = str(plugin) self.assertEqual(expected_result, result)
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)
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.plugin = SpotDatabase( "csv", "output", "improver", "time", "index", primary_map=['validity_date', 'validity_time'], primary_func=[ lambda x: dt.utcfromtimestamp(x).date(), lambda x: dt.utcfromtimestamp(x).hour * 100 ]) data = [[1487311200, 280.]] columns = ["time", "values"] self.input_df = pd.DataFrame(data, columns=columns) self.input_df = self.input_df.set_index(["time"])
def test_exception_if_pivot_dim_set(self): """Test it raises an exception if a 2D cube is input and no pivot_dim.""" plugin = SpotDatabase("csv", "output", "improver", "time", "IMPRO", 0, pivot_dim="percentile") cube = set_up_spot_cube(280, number_of_sites=3) second_cube = cube.copy() second_cube.coord("percentile").points = np.array([60.0]) cubelist = CubeList([cube, second_cube]) cubes = cubelist.concatenate() message = "Dimensions that are not described by the pivot_dim or "\ "coord_to_slice_over must only have one point in. "\ "Dimension '2' has length '3' and is associated with the "\ "'index' coordinate." with self.assertRaisesRegex(ValueError, message): plugin.check_input_dimensions(cubes[0])
def setUp(self): """Set up the plugin and dataframe needed for this test""" self.cubes = CubeList([set_up_spot_cube(280)]) self.data_directory = mkdtemp() self.plugin = SpotDatabase("csv", self.data_directory + "/test.csv", "improver", "time", "index")
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)