def test_fail_error(self): g = og('', vv=[ov('v', dd=[od('x')])]) with self.assertRaises(IncompleteStructureError) as err_context: has_no_missing_dims(g, fail_if_not=True) msg = err_context.exception.message self.check_all_in_str(msg, [ 'Variable "/v"', 'dimension "x"', 'no definition exists'])
def _fake_complete(group): """ 'Complete' a group which is lacking some dimension lengths or data types. As the examples in simple_semantic_containers don't contain data, they don't have data types or lengths of unlimited dimensions (e.g. 'time'). This operation sets missing lengths to 1, and fakes the data. This allows CDL to be generated (which needs the dtype), and variables to be compared (via "np.all(x.data == y.data)"). This allows the examples builder functions to omit variables data, which is neater and saves space. """ # Check all dims have definitions defined in the build code (for clarity). ncg.has_no_missing_dims(group, fail_if_not=True) # Fix up any top-level dims with no length (e.g. unlimited 'time'). for dim in group.dimensions: if dim.length is None: dim.length = 1 # Patch it all together. Should work, now we have all the lengths. ncg.complete(group) for var in ncg.all_variables(group): if var.data is None: # NOTE: this might be better be done with numpy stride tricks ? var.data = _dummy_data(dim.length for dim in var.dimensions)
def test_grouped_fail(self): g = og('', dd=[od('x'), od('y')], vv=[ov('v1', dd=[od('x')]), ov('v2', dd=[od('x'), od('y')])], gg=[og('subgroup', dd=[od('zz')], vv=[ov('sv1', dd=[od('x')]), ov('sv2', dd=[od('y'), od('z')])])]) with self.assertRaises(IncompleteStructureError) as err_context: has_no_missing_dims(g, fail_if_not=True) msg = err_context.exception.message self.check_all_in_str(msg, [ 'Variable "/subgroup/sv2"', 'dimension "z"', 'no definition exists'])
def do_complete(self, group): # complete a group, and check that re-calling has no effect complete(group) self.assertTrue(has_no_missing_dims(group)) firstpass_result = group.detached_copy() complete(group) self.assertEqual(group, firstpass_result)
def test_grouped_okay(self): g = og('', dd=[od('x'), od('y')], vv=[ov('v1', dd=[od('x')]), ov('v2', dd=[od('x'), od('y')])], gg=[og('subgroup', dd=[od('z')], vv=[ov('sv1', dd=[od('x')]), ov('sv2', dd=[od('y'), od('z')])])]) self.assertTrue(has_no_missing_dims(g))
def test_simple_to_path(self): test_outfile_name = 'test_simple.nc' test_outfile_path = os.path.join(testdata_dirpath, test_outfile_name) array = np.arange(4) var = ov('v1', dd=[od('x')], aa=[oa('v_att', 'this')], data=array) g = og('', vv=[var]) self.assertFalse(ncg.has_no_missing_dims(g)) try: write(test_outfile_path, g) self.assertTrue(ncg.has_no_missing_dims(g)) # Read it back again and check. with netCDF4.Dataset(test_outfile_path) as ds: g_back = read(ds) self.assertEqual(g_back, g) finally: if not leave_output: # Remove temporary file if os.path.exists(test_outfile_path): os.remove(test_outfile_path)
def test_simple_fail(self): g = og('', vv=[ov('v', dd=[od('x')])]) self.assertFalse(has_no_missing_dims(g))
def test_empty(self): g = og('') self.assertTrue(has_no_missing_dims(g))