def test_get_data(self): saved_keys = self.saved_keys data = create_data_container(restrict=saved_keys) data1 = DataContainer(data) key = saved_keys[0] data[key] = dummy_cuba_value(key) + dummy_cuba_value(key) with self.new_table('my_data_table') as table: table.append(data) table.append(data1) with self.open_table('my_data_table') as table: self.assertEqual(len(table), 2) self.assertDataContainersEqual(table[0], data) self.assertDataContainersEqual(table[1], data1)
def test_delete_data(self): saved_keys = self.saved_keys data = create_data_container(restrict=saved_keys) with self.new_table('my_data_table') as table: uid0 = table.append(data) new_data = DataContainer(data) key = saved_keys[0] data[key] = dummy_cuba_value(key) + dummy_cuba_value(key) uid1 = table.append(new_data) with self.open_table('my_data_table', mode='a') as table: del table[uid0] loaded_data = table[uid1] self.assertEqual(len(table), 1) self.assertDataContainersEqual(loaded_data, new_data)
def test_accumulate_and_expand(self): accumulator = CUBADataAccumulator() accumulator.append(create_data_container(restrict=[CUBA.NAME])) accumulator.append( create_data_container(restrict=[CUBA.NAME, CUBA.TEMPERATURE])) self.assertEqual(len(accumulator), 2) self.assertEqual(accumulator.keys, set([CUBA.NAME, CUBA.TEMPERATURE])) assert_array_equal( accumulator[CUBA.TEMPERATURE], [None, dummy_cuba_value(CUBA.TEMPERATURE)]) assert_array_equal( accumulator[CUBA.NAME], [dummy_cuba_value(CUBA.NAME)] * 2)
def test_delete_data_with_invalid_uid(self): saved_keys = self.saved_keys data = create_data_container(restrict=saved_keys) with self.new_table('my_data_table') as table: uid0 = table.append(data) new_data = DataContainer(data) key = saved_keys[0] data[key] = dummy_cuba_value(key) + dummy_cuba_value(key) table.append(new_data) with self.open_table('my_data_table', mode='a') as table: del table[uid0] with self.assertRaises(KeyError): del table[uuid.uuid4()] with self.assertRaises(KeyError): del table[uid0]
def test_accumulate_with_missing_values(self): accumulator = CUBADataAccumulator() accumulator.append(create_data_container()) accumulator.append( create_data_container(restrict=[CUBA.NAME, CUBA.TEMPERATURE])) self.assertEqual(len(accumulator), 2) self.assertEqual(accumulator.keys, set(CUBA)) for cuba in CUBA: if cuba in [CUBA.NAME, CUBA.TEMPERATURE]: assert_array_equal( accumulator[cuba], [dummy_cuba_value(cuba)] * 2) else: assert_array_equal( accumulator[cuba][0], dummy_cuba_value(cuba)) self.assertIsNone(accumulator[cuba][1])
def load_onto_vtk(self, vtk_data): """ Load the stored information onto a vtk data container. Parameters ---------- vtk_data : vtkPointData or vtkCellData The vtk container to load the value onto. Data are loaded onto the vtk container based on their data type. The name of the added array is the name of the CUBA key (i.e. :samp:`{CUBA}.name`). Currently only scalars and three dimensional vectors are supported. """ def replacer(data): return nan if data is None else data for cuba in self.keys: default = dummy_cuba_value(cuba) if (numpy.issubdtype(type(default), numpy.float) or numpy.issubdtype(type(default), numpy.int)): data = numpy.array(self._data[cuba], dtype=float) index = vtk_data.add_array(data) vtk_data.get_array(index).name = cuba.name elif isinstance(default, numpy.ndarray) and default.size == 3: nan = numpy.array([None, None, None], dtype=float) data = numpy.array( tuple(replacer(data) for data in self._data[cuba]), dtype=numpy.float) index = vtk_data.add_array(data) vtk_data.get_array(index).name = cuba.name else: message = 'property {!r} is currently ignored' warnings.warn(message.format(cuba))
def test_accumulate_on_keys(self): cuds_data = [create_data_container() for i in range(10)] accumulator = CUBADataAccumulator(keys=[CUBA.NAME, CUBA.TEMPERATURE]) for data in cuds_data: accumulator.append(data) self.assertEqual(len(accumulator), 10) self.assertEqual(accumulator.keys, set([CUBA.NAME, CUBA.TEMPERATURE])) for cuba in [CUBA.NAME, CUBA.TEMPERATURE]: assert_array_equal( accumulator[cuba], [dummy_cuba_value(cuba)] * 10)
def test_accumulate(self): cuds_data = [create_data_container() for i in range(10)] accumulator = CUBADataAccumulator() for data in cuds_data: accumulator.append(data) self.assertEqual(len(accumulator), 10) self.assertEqual(accumulator.keys, set(CUBA)) for cuba in CUBA: assert_array_equal( accumulator[cuba], [dummy_cuba_value(cuba)] * 10)
def test_convert_all(self): for cuba in CUBA: # given original_value = dummy_cuba_value(cuba) # when file_value = convert_to_file_type(original_value, cuba) # then assert_array_equal(original_value, convert_from_file_type(file_value, cuba))
def test_load_scalars_onto_vtk(self): accumulator = CUBADataAccumulator() accumulator.append(create_data_container(restrict=[CUBA.NAME])) accumulator.append( create_data_container(restrict=[CUBA.NAME, CUBA.TEMPERATURE])) vtk_data = tvtk.PointData() accumulator.load_onto_vtk(vtk_data) self.assertEqual(vtk_data.number_of_arrays, 1) expected = numpy.array( [None, dummy_cuba_value(CUBA.TEMPERATURE)], dtype=float) assert_array_equal(vtk_data.get_array(0), expected) assert_array_equal(vtk_data.get_array(CUBA.TEMPERATURE.name), expected)
def test_load_vectors_onto_vtk(self): accumulator = CUBADataAccumulator() accumulator.append(create_data_container(restrict=[CUBA.NAME])) accumulator.append( create_data_container(restrict=[CUBA.NAME, CUBA.VELOCITY, CUBA.LATTICE_VECTORS])) vtk_data = tvtk.PointData() accumulator.load_onto_vtk(vtk_data) self.assertEqual(vtk_data.number_of_arrays, 1) expected = numpy.array( [[None, None, None], dummy_cuba_value(CUBA.VELOCITY)], dtype=float) assert_array_equal(vtk_data.get_array(0), expected) assert_array_equal(vtk_data.get_array(CUBA.VELOCITY.name), expected)
def test_accumulate_and_expand(self): # given accumulator = CUBADataAccumulator() # when accumulator.append(create_data_container(restrict=[CUBA.MASS])) # then self.assertEqual(len(accumulator), 1) # when accumulator.append( create_data_container(restrict=[CUBA.NAME, CUBA.TEMPERATURE])) # then self.assertEqual(len(accumulator), 2) self.assertEqual(accumulator.keys, set([CUBA.MASS, CUBA.TEMPERATURE])) assert_array_equal( vtk_to_numpy(accumulator[CUBA.TEMPERATURE]), [numpy.nan, dummy_cuba_value(CUBA.TEMPERATURE)]) assert_array_equal( vtk_to_numpy(accumulator[CUBA.MASS]), [dummy_cuba_value(CUBA.MASS), numpy.nan])
def test_accumulate(self): # given cuds_data = [create_data_container(constant=i) for i in range(10)] # when accumulator = CUBADataAccumulator() for data in cuds_data: accumulator.append(data) # then self.assertEqual(len(accumulator), 10) expected_cuba = set(CUBA) & supported_cuba() self.assertEqual(accumulator.keys, expected_cuba) for cuba in expected_cuba: default = dummy_cuba_value(cuba) if isinstance(default, numpy.ndarray): new_shape = (10,) + default.shape assert_array_equal( vtk_to_numpy(accumulator[cuba]).reshape(new_shape), [dummy_cuba_value(cuba, constant=i) for i in range(10)]) else: assert_array_equal( vtk_to_numpy(accumulator[cuba]), [dummy_cuba_value(cuba, constant=i) for i in range(10)])
def test_dummy_cuba_value_with_constant(self): constant = 18 for cuba in CUBA: value = dummy_cuba_value(cuba, constant=18) keyword = KEYWORDS[CUBA(cuba).name] if numpy.issubdtype(keyword.dtype, str): self.assertEqual(value, keyword.name + str(constant)) else: shape = keyword.shape if shape == [1]: self.assertEqual(value, keyword.dtype(cuba + constant)) else: data = numpy.arange(numpy.prod(shape)) * (cuba + constant) data = numpy.reshape(data, shape) self.assertEqual(value.dtype.type, keyword.dtype) assert_array_equal(value, data)
def test_dummy_cuba_value(self): constant = 3 for cuba in CUBA: value = dummy_cuba_value(cuba) keyword = KEYWORDS[CUBA(cuba).name] if keyword.dtype is uuid.UUID: self.assertEqual(value, uuid.UUID(int=constant, version=4)) elif numpy.issubdtype(keyword.dtype, str): self.assertEqual(value, keyword.name + str(constant)) else: shape = keyword.shape if shape == [1]: self.assertEqual(value, keyword.dtype(cuba + constant)) else: data = numpy.arange(numpy.prod(shape)) * (cuba + constant) data = numpy.reshape(data, shape) self.assertEqual(value.dtype.type, keyword.dtype) assert_array_equal(value, data)
def test_dummy_cuba_value_with_constant(self): constant = 18 for cuba in [c for c in CUBA if c in KEYWORDS]: value = dummy_cuba_value(cuba, constant=18) keyword = KEYWORDS[CUBA(cuba).name] if keyword.dtype is uuid.UUID: self.assertEqual(value, uuid.UUID(int=constant, version=4)) elif numpy.issubdtype(keyword.dtype, str): self.assertEqual(value, keyword.name + str(constant)) else: shape = keyword.shape if shape == [1]: self.assertEqual(value, keyword.dtype(cuba + constant)) else: data = numpy.arange(numpy.prod(shape)) * (cuba + constant) data = numpy.reshape(data, shape) self.assertEqual(value.dtype.type, keyword.dtype) assert_array_equal(value, data)
def test_accumulate_with_missing_values(self): # given accumulator = CUBADataAccumulator() # when accumulator.append(DataContainer()) # then self.assertEqual(len(accumulator), 1) # when accumulator.append( create_data_container( restrict=[CUBA.NAME, CUBA.TEMPERATURE], constant=7)) # then self.assertEqual(len(accumulator), 2) self.assertEqual(accumulator.keys, set([CUBA.TEMPERATURE])) array = vtk_to_numpy(accumulator[CUBA.TEMPERATURE]) assert_array_equal( array, [numpy.nan, dummy_cuba_value(CUBA.TEMPERATURE, constant=7)])
def test_accumulate_on_keys(self): # given cuds_data = [create_data_container(constant=i) for i in range(10)] # when accumulator = CUBADataAccumulator(keys=[CUBA.NAME, CUBA.TEMPERATURE]) # then self.assertEqual(len(accumulator), 0) self.assertEqual(accumulator.keys, set([CUBA.TEMPERATURE])) # when for data in cuds_data: accumulator.append(data) # then self.assertEqual(len(accumulator), 10) self.assertEqual(accumulator.keys, set([CUBA.TEMPERATURE])) assert_array_equal( vtk_to_numpy(accumulator[CUBA.TEMPERATURE]), [dummy_cuba_value( CUBA.TEMPERATURE, constant=i) for i in range(10)])
def test_create_data_container_with_constant(self): data = create_data_container(constant=7) keys = set(CUBA) self.assertEqual(set(data), keys) for cuba, value in data.iteritems(): assert_array_equal(value, dummy_cuba_value(cuba, 7))
def test_create_data_container_with_restrict(self): keys = set([CUBA.STATUS, CUBA.MASS]) data = create_data_container(restrict=keys) self.assertEqual(set(data), keys) for cuba, value in data.iteritems(): assert_array_equal(value, dummy_cuba_value(cuba, None))