def test_set_arrays_access_via_attribute(self): x_points = np.array(range(0, 2)) y_points = np.array(range(0, 2)) x = DataArray(name="x", label="x-axis", unit="mV", is_setpoint=True, preset_data=x_points) y = DataArray(name="y", label="y-axis", unit="mV", is_setpoint=True, preset_data=np.tile(np.array(y_points), [x.size, 1])) z = DataArray(name="z", label="z-axis", unit="ma", set_arrays=(x, y), preset_data=np.NaN * np.ones( (x_points.size, y_points.size))) data_set = DataSet(data_arrays=[z]) attrs = dir(data_set) self.assertIn('x', attrs) self.assertIn('y', attrs) self.assertEqual(data_set.x, x) self.assertEqual(data_set.y, y)
def test_1d_raise_error(self): setpoints = DataArray('x', 'setpoints', 'V', is_setpoint=True, preset_data=self.x_points) with self.assertRaises(ValueError) as error: DataArray('x_data', 'data', 'A', shape=(9, ), set_arrays=[setpoints]) self.assertEqual( ("Dimensions of 'set_arrays' and 'data' do not match.", ), error.exception.args) with self.assertRaises(ValueError) as error: DataArray('x_data', 'data', 'A', shape=(11, ), set_arrays=[setpoints]) self.assertEqual( ("Dimensions of 'set_arrays' and 'data' do not match.", ), error.exception.args)
def test_constructor(self): storage = MagicMock(spec=MemoryDataSetIOWriter) name = 'ItsAName' array_name = 'ItsAnArray' time_stamp = datetime.datetime(2018, 12, 24, 18, 0) user_data = {'some': 'data'} set_arrays = DataArray('setpoints', 'X', is_setpoint=True, preset_data=np.array([1, 2, 3, 4, 5])) data_arrays = DataArray(array_name, 'results', shape=(5, ), set_arrays=[set_arrays]) data_set = DataSet(storage_writer=storage, name=name, time_stamp=time_stamp, user_data=user_data, data_arrays=data_arrays, set_arrays=set_arrays) self.assertEqual([storage], data_set.storage) self.assertEqual(name, data_set.name) self.assertEqual(time_stamp, data_set.time_stamp) self.assertDictEqual(user_data, data_set.user_data) self.assertEqual(data_arrays, data_set.data_arrays[array_name]) self.assertEqual(array_name, data_set.default_array_name)
def test_array_interface(self): data_array = DataArray(name='x', label='x-axis', unit='mV', shape=(5, 5)) array_interface = data_array.__array_interface__ self.assertIn('data', array_interface) self.assertIn('strides', array_interface) self.assertIn('descr', array_interface) self.assertIn('typestr', array_interface) self.assertIn('shape', array_interface) self.assertIn('version', array_interface) self.assertTupleEqual((5, 5), array_interface['shape']) self.assertTupleEqual((5, 5), data_array.shape) data_np_array = DataArray(name='np', label='test', preset_data=np.array([5, 6, 3, 7, 2, 1, 4])) mean = np.mean(data_np_array) self.assertEqual(4, mean) data_np_array.sort() self.assertListEqual([1, 2, 3, 4, 5, 6, 7], list(data_np_array)) np_data_array = np.array(data_np_array) self.assertListEqual(list(data_np_array), list(np_data_array))
def test_2d_set_array_with_set_array_out_of_order(self): x_points = np.array(range(0, 10)) y_points = np.array(range(0, 5)) x = DataArray(name='x', label='x-axis', unit='mV', is_setpoint=True, preset_data=np.array(x_points)) y = DataArray(name='y', label='y-axis', unit='mV', is_setpoint=True, set_arrays=[ x, ], preset_data=np.tile(np.array(y_points), [x.size, 1])) z = DataArray(name='z', label='z-axis', unit='ma', set_arrays=[y, x], preset_data=np.NaN * np.ones( (x_points.size, y_points.size))) self.assertEqual('z', z.name) self.assertEqual('z-axis', z.label) self.assertEqual('ma', z.unit) self.assertFalse(z.is_setpoint) self.assertIs(x, z.set_arrays[1]) self.assertIs(y, z.set_arrays[0])
def test_attribute_error(self): data_array = DataArray('x', 'setpoints', 'V', is_setpoint=True, shape=(5, )) with self.assertRaises(AttributeError) as error: data_array.update() self.assertEqual(("DataArray has no attribute 'update'", ), error.exception.args)
def test_constructor_with_preset_data(self): setpoints = DataArray('x', 'setpoints', 'V', is_setpoint=True, preset_data=self.x_points) data_array = DataArray('x_data', 'data', 'A', preset_data=self.x_points, set_arrays=[setpoints]) self.assertIs(setpoints, data_array.set_arrays[0])
def test_str(self): preset_data = np.ndarray((3, ), buffer=np.array([1, 2, 3]), dtype=int) set_array = DataArray('x', 'setpoints', 'V', is_setpoint=True, preset_data=preset_data) data_array = DataArray('x', 'setpoints', 'V', set_arrays=[set_array], preset_data=np.ones((3, ))) expected_string = "DataArray (3,): x\ndata: [1. 1. 1.]\nset_arrays:['x']" self.assertEqual(expected_string, data_array.__str__())
def test_add_array_with_bad_name(self): data_array = DataArray('this is not a good name', 'label', preset_data=np.array([1, 2, 3, 4, 5])) data_set = DataSet() with self.assertRaisesRegex( SyntaxError, "'this is not a good name' is an invalid name for an identifier." ): data_set.add_array(data_array) data_array.name = 99 with self.assertRaisesRegex( ValueError, "Array name has to be string, not <class 'int'>"): data_set.add_array(data_array)
def test_properties(self): data_array = DataArray(name='x', label='x-axis', unit='mV', shape=(5, 5)) self.assertEqual('x', data_array.name) self.assertEqual('x-axis', data_array.label) self.assertEqual('mV', data_array.unit) data_array.name = 'y' self.assertEqual('y', data_array.name) data_array.label = 'y-axis' self.assertEqual('y-axis', data_array.label) data_array.unit = 'Hz' self.assertEqual('Hz', data_array.unit)
def test_constructor_multiple_data_arrays(self): storage = MagicMock(spec=MemoryDataSetIOWriter) name = 'ItsAName' array_name = 'ItsAnArray' user_data = {'some': 'data'} x_points = np.array(range(0, 10)) y_points = np.array(range(0, 5)) x = DataArray(name='x', label='x-axis', unit='mV', is_setpoint=True, preset_data=np.array(x_points)) y = DataArray(name='y', label='y-axis', unit='mV', is_setpoint=True, set_arrays=(x, ), preset_data=np.tile(np.array(y_points), [x.size, 1])) z = DataArray(name=array_name, label='z-axis', unit='ma', set_arrays=[y, x], preset_data=np.NaN * np.ones( (x_points.size, y_points.size))) other_z = DataArray(name='other_array', label='z-axis', unit='ma', set_arrays=[y, x], preset_data=np.NaN * np.ones( (x_points.size, y_points.size))) data_set = DataSet(storage_writer=storage, name=name, user_data=user_data, data_arrays=[z, other_z], set_arrays=[y, x]) self.assertEqual([storage], data_set.storage) self.assertEqual(name, data_set.name) self.assertDictEqual(user_data, data_set.user_data) self.assertEqual(z, data_set.data_arrays[array_name]) self.assertEqual(array_name, data_set.default_array_name) with self.assertRaises(TypeError) as error: DataSet(data_arrays=np.array([1, 2, 3, 4])) self.assertIn( "'data_arrays' have to be of type 'DataArray', not <class 'numpy.ndarray'>", error.exception.args)
def test_sync_from_storage(self): io_reader, io_writer = MemoryDataSetIOFactory.get_reader_writer_pair() data_set_consumer = DataSet(storage_reader=io_reader, name='consumer') some_array = DataArray('some_array', 'label', shape=(5, 1)) io_writer.sync_add_data_array_to_storage(some_array) data_set_consumer.sync_from_storage(-1) self.assertTrue(hasattr(data_set_consumer, 'some_array'))
def test_run_process_multiple_signal_processors(self): data_set = DataSet(data_arrays=DataArray('x', 'x', preset_data=array([1, 2, 3, 4, 5]))) class PlusOneSignalProcessor(SignalProcessorInterface): def __init__(self): self._signal_data = None def run_process(self, signal_data: DataSet) -> DataSet: self._signal_data = signal_data signal_data.data_arrays['x'] += 1 return signal_data class TimesTwoSignalProcessor(SignalProcessorInterface): def __init__(self): self._signal_data = None def run_process(self, signal_data: DataSet) -> DataSet: self._signal_data = signal_data signal_data.data_arrays['x'] *= 2 return signal_data signal_processor_runner = SignalProcessorRunner() signal_processor_runner.add_signal_processor(PlusOneSignalProcessor()) signal_processor_runner.add_signal_processor(TimesTwoSignalProcessor()) new_data_set = signal_processor_runner.run(data_set) self.assertIs(data_set.data_arrays['x'], new_data_set.data_arrays['x']) self.assertTrue(array_equal(new_data_set.data_arrays['x'], array([4, 6, 8, 10, 12])))
def test_run_process_without_signal_processor(self): data_set = DataSet(data_arrays=DataArray('x', 'x', preset_data=array([1, 2, 3, 4, 5]))) signal_processor_runner = SignalProcessorRunner() new_data_set = signal_processor_runner.run(data_set) self.assertIs(data_set.data_arrays['x'], new_data_set.data_arrays['x']) self.assertTrue(array_equal(new_data_set.data_arrays['x'], array([1, 2, 3, 4, 5])))
def test_indexing_support(self): preset_data = np.array(range(10)) data_array = DataArray('name', 'label', preset_data=preset_data.copy()) self.assertEqual(preset_data[0], data_array[0]) for i in range(10): data_array[i] = i + 10 for i in range(10, 20): self.assertEqual(i, data_array[i - 10])
def test_repr(self): preset_data = np.ndarray((3, ), buffer=np.array([1, 2, 3]), dtype=int) set_array = DataArray('x', 'setpoints', 'V', is_setpoint=True, preset_data=preset_data) data_array = DataArray('x', 'setpoints', 'V', set_arrays=[set_array], preset_data=np.ones((3, ))) expected_string = "DataArray(id={}, name='x', label='setpoints', unit='V', is_setpoint=False, " \ "data=array([1., 1., 1.]), set_arrays=[DataArray(id={}, name='x', label='setpoints', " \ "unit='V', is_setpoint=True, data=array([1, 2, 3]), " \ "set_arrays=[])])".format(id(data_array), id(data_array.set_arrays[0])) self.assertEqual(expected_string, data_array.__repr__())
def test_1d_constructor_with_shape(self): setpoints = DataArray('x', 'setpoints', 'V', is_setpoint=True, preset_data=self.x_points) data_array = DataArray('x_data', 'data', 'A', shape=(10, ), set_arrays=[setpoints]) self.assertIs(setpoints, data_array.set_arrays[0]) self.assertEqual('x_data', data_array.name) self.assertEqual('data', data_array.label) self.assertEqual('A', data_array.unit) self.assertFalse(data_array.is_setpoint) self.assertTrue(setpoints.is_setpoint)
def test_3d_raise_error(self): x_setpoints = DataArray('x', 'setpoints', 'V', is_setpoint=True, preset_data=self.x_points) y_setpoints = DataArray('y', 'setpoints', 'V', is_setpoint=True, preset_data=np.tile(np.array(self.y_points), [self.x_points.size, 1])) z_setpoints = DataArray('z', 'setpoints', 'V', is_setpoint=True, preset_data=np.ndarray((10, 5, 10))) with self.assertRaises(ValueError) as error: DataArray('x_data', 'data', 'A', shape=(10, 5, 9), set_arrays=[x_setpoints, y_setpoints, z_setpoints]) self.assertEqual( ("Dimensions of 'set_arrays' and 'data' do not match.", ), error.exception.args) with self.assertRaises(ValueError) as error: DataArray('x_data', 'data', 'A', shape=(10, 6, 10), set_arrays=[x_setpoints, z_setpoints, y_setpoints]) self.assertEqual( ("Dimensions of 'set_arrays' and 'data' do not match.", ), error.exception.args) with self.assertRaises(ValueError) as error: DataArray('x_data', 'data', 'A', shape=(10, 5, 10), set_arrays=[x_setpoints, y_setpoints]) self.assertEqual( ("Dimensions of 'set_arrays' and 'data' do not match.", ), error.exception.args) with self.assertRaises(ValueError) as error: DataArray('x_data', 'data', 'A', shape=( 10, 5, ), set_arrays=[x_setpoints, y_setpoints, z_setpoints]) self.assertEqual( ("Dimensions of 'set_arrays' and 'data' do not match.", ), error.exception.args)
def test_add_data(self): some_array = DataArray('some_array', 'label', shape=(5, 5)) data_set = DataSet(data_arrays=some_array) data_set.add_data((4, 4), {'some_array': 42}) self.assertEqual(42, some_array[4][4]) data_set.add_data(3, {'some_array': [1, 2, 3, 4, 5]}) self.assertListEqual([1, 2, 3, 4, 5], list(some_array[3]))
def __dummy_data_array(set_array: DataArray, scope_data: np.ndarray, channel_index: int = 1, trace_number: int = 1): idenifier = 'ScopeTrace_{:03d}'.format(trace_number) label = 'Channel_{}'.format(channel_index) return DataArray(idenifier, label, preset_data=scope_data, set_arrays=[set_array])
def _construct_data_array(self, array: Dict[str, Any]) -> DataArray: set_arrays = [self._set_arrays[name] for name in array['set_arrays']] data_array = DataArray(name=array['name'], label=array['label'], unit=array['unit'], is_setpoint=array['is_setpoint'], preset_data=MongoDataSetIO.decode_numpy_array( array['preset_data']), set_arrays=set_arrays) return data_array
def test_wrong_set_array_with_set_array_raises_error(self): x_points = np.array(range(0, 10)) y_points = np.array(range(0, 5)) x = DataArray(name='x', label='x-axis', unit='mV', is_setpoint=True, preset_data=np.array(x_points)) with self.assertRaises(ValueError) as error: DataArray(name='y', label='y-axis', unit='mV', is_setpoint=True, set_arrays=[ x, ], preset_data=np.tile(np.array(y_points), [x.size + 1, 1])) self.assertEqual(("Dimensions of 'set_arrays' do not match.", ), error.exception.args)
def test_matmul(self): np_array = np.array(range(10)) data_array = DataArray(name='x', label='x-axis', unit='mV', is_setpoint=True, preset_data=np_array.copy()) self.assertListEqual(list(np_array), list(data_array)) expected_result = np_array @ np_array result = data_array @ data_array self.assertEqual(expected_result, result)
def _test_binary_operation(self, op, rhs): np_array = np.array(range(10)) data_array = DataArray(name='x', label='x-axis', unit='mV', is_setpoint=True, preset_data=np_array) self.assertListEqual(list(np_array), list(data_array)) expected_result = op(np_array, rhs) result = op(data_array, rhs) self.assertListEqual(list(expected_result), list(result))
def test_add_array(self): array_name = 'some_array' data_array = DataArray(array_name, 'label', preset_data=np.array([1, 2, 3, 4, 5])) data_set = DataSet() data_set.add_array(data_array) self.assertListEqual(list(data_array), list(data_set.some_array)) self.assertListEqual(list(data_array), list(data_set.data_arrays[array_name])) self.assertIs(data_set.some_array, data_set.data_arrays[array_name])
def test_string(self): name = 'ItsAName' array_name = 'ItsAnArray' user_data = {'some': 'data'} x_points = np.array(range(0, 10)) y_points = np.array(range(0, 5)) x = DataArray(name='x', label='x-axis', unit='mV', is_setpoint=True, preset_data=np.array(x_points)) y = DataArray(name='y', label='y-axis', unit='mV', is_setpoint=True, set_arrays=(x, ), preset_data=np.tile(np.array(y_points), [x.size, 1])) z = DataArray(name=array_name, label='z-axis', unit='ma', set_arrays=[y, x], preset_data=np.NaN * np.ones( (x_points.size, y_points.size))) other_z = DataArray(name='other_array', label='z-axis', unit='ma', set_arrays=[y, x], preset_data=np.NaN * np.ones( (x_points.size, y_points.size))) data_set = DataSet(name=name, user_data=user_data, data_arrays=[z, other_z], set_arrays=[y, x]) expected = "DataSet: ItsAName\n name | label | unit | shape | setpoint\n ItsAnArray | z-axis | " \ "ma | (10, 5) | False\n other_array | z-axis | ma | (10, 5) | False\n y | y-axis |" \ " mV | (10, 5) | True\n x | x-axis | mV | (10,) | True" actual = str(data_set) self.assertEqual(expected, actual)
def test_copy(self): data_array = DataArray(name='x', label='x-axis', unit='mV', shape=(5, 5)) copied_array = copy(data_array) self.assertEqual(len(data_array), len(copied_array)) self.assertEqual(data_array.label, copied_array.label) self.assertEqual(data_array.name, copied_array.name) self.assertEqual(data_array.unit, copied_array.unit) self.assertIsNot(copied_array, data_array)
def test_integrate_with_data_set_io_add_array(self): io_reader, io_writer = MemoryDataSetIOFactory.get_reader_writer_pair() data_set_consumer = DataSet(storage_reader=io_reader) some_array = DataArray('some_array', 'label', shape=(5, 5)) data_set_producer = DataSet(storage_writer=io_writer) data_set_producer.add_array(some_array) data_set_consumer.sync_from_storage(-1) self.assertTrue(hasattr(data_set_consumer, 'some_array')) self.assertEqual('some_array', data_set_consumer.some_array.name) self.assertEqual((5, 5), data_set_consumer.some_array.shape) self.assertIsNot(some_array, data_set_consumer.some_array)
def test_sync_add_data_array_to_storage(self): array = DataArray(name='stuffsi', label='V', shape=(1, 1)) array[0] = 42 self.data_set_io_writer.sync_add_data_array_to_storage(array) data_type, storage_array = self.queue.get(block=False) self.assertEqual(42, storage_array[0]) self.assertEqual(data_type, DataSetIOReader.DATA_ARRAY) self.assertEqual(array.name, storage_array.name) self.assertEqual(array.shape, storage_array.shape) self.assertEqual(array.label, storage_array.label)
def test_dir(self): np_array = np.ndarray((1, 2)) np_dir = dir(np_array) data_array = DataArray('x', 'setpoints', 'V', is_setpoint=True, shape=(1, 2)) data_array_dir = dir(data_array) self.assertLess(data_array_dir, np_dir) for value in np_dir: self.assertIn(value, data_array_dir)