class AcquisitionTestCase(BasePoolTestCase): def setUp(self): """Create dummy controllers and elements.""" BasePoolTestCase.setUp(self) self.acquisition = None self.synchronization = None self.data_listener = AttributeListener() self.main_element = FakeElement(self.pool) self.tg_1_1 = self.tgs['_test_tg_1_1'] self.tg_ctrl_1 = self.tg_1_1.get_controller() self.ct_1_1 = self.cts['_test_ct_1_1'] self.ct_ctrl_1 = self.ct_1_1.get_controller() self.channel_names = ['_test_ct_1_1'] def create_action(self, class_, elements): action = class_(self.main_element) for element in elements: action.add_element(element) return action def add_listeners(self, chn_list): for chn in chn_list: chn.add_listener(self.data_listener) def wait_finish(self): # waiting for acquisition and synchronization to finish while (self.acquisition.is_running() or self.synchronization.is_running()): time.sleep(.1) def do_asserts(self, repetitions, jobs_before): # print acquisition records table = self.data_listener.get_table() header = table.dtype.names print header n_rows = table.shape[0] for row in xrange(n_rows): print row, table[row] # checking if all channels produced data for channel in self.channel_names: msg = 'data from channel %s were not acquired' % channel self.assertIn(channel, header, msg) # checking if all the data were acquired for ch_name in header: ch_data_len = len(table[ch_name]) msg = 'length of data for channel %s is %d and should be %d' %\ (ch_name, ch_data_len, repetitions) self.assertEqual(ch_data_len, repetitions, msg) # checking if there are no pending jobs jobs_after = get_thread_pool().qsize msg = ('there are %d jobs pending to be done after the acquisition ' + '(before: %d)') % (jobs_after, jobs_before) self.assertEqual(jobs_before, jobs_after, msg) def tearDown(self): BasePoolTestCase.tearDown(self) self.data_listener = None self.channel_names = None self.main_element = None
class AcquisitionTestCase(BasePoolTestCase): CHANNEL_NAME = None # Override this! SYNCHRONIZATION = None # Override this! def setUp(self): """Create dummy controllers and elements.""" BasePoolTestCase.setUp(self) self.acquisition = None self.synchronization = None self.data_listener = AttributeListener() self.main_element = FakeElement(self.pool) self.tg = self.tgs['_test_tg_1_1'] self.tg_ctrl = self.tg.get_controller() self.channel = self.exp_channels[self.CHANNEL_NAME] self.channel_ctrl = self.channel.get_controller() self.channel_names = [self.CHANNEL_NAME] def create_action(self, class_, elements): action = class_(self.main_element) for element in elements: action.add_element(element) return action def add_listeners(self, chn_list): for chn in chn_list: chn.add_listener(self.data_listener) def _prepare(self, integ_time, repetitions, latency_time, nb_starts): pass def prepare(self, integ_time, repetitions, latency_time, nb_starts): self.channel_ctrl.set_ctrl_par("synchronization", self.SYNCHRONIZATION) self._prepare(integ_time, repetitions, latency_time, nb_starts) self.channel_ctrl.ctrl.PrepareOne(self.channel.axis, integ_time, repetitions, latency_time, nb_starts) def wait_finish(self): # waiting for acquisition and synchronization to finish while (self.acquisition._is_busy() or self.synchronization.is_running()): time.sleep(.1) def do_asserts(self, repetitions, jobs_before, strict=True): table = self.data_listener.get_table() header = table.dtype.names # checking if all channels produced data for channel in self.channel_names: msg = 'data from channel %s were not acquired' % channel self.assertIn(channel, header, msg) # checking if data were acquired for ch_name in header: ch_data_len = len(table[ch_name]) if strict: msg = ('length of data for channel %s is %d and should be ' '%d' % (ch_name, ch_data_len, repetitions)) self.assertEqual(repetitions, ch_data_len, msg) else: msg = ('length of data for channel %s is %d and should <= ' '%d' % (ch_name, ch_data_len, repetitions)) self.assertGreaterEqual(repetitions, ch_data_len, msg) for value in table[ch_name]: if (isinstance(value, numpy.ndarray) or is_number(value) or is_pure_str(value)): break else: raise AssertionError('channel %s does not report any ' 'valid data') # checking if there are no pending jobs jobs_after = get_thread_pool().qsize msg = ('there are %d jobs pending to be done after the acquisition ' + '(before: %d)') % (jobs_after, jobs_before) self.assertEqual(jobs_before, jobs_after, msg) def tearDown(self): BasePoolTestCase.tearDown(self) self.data_listener = None self.channel_names = None self.main_element = None
class BaseAcquisition(object): def setUp(self, pool): self.pool = pool self.pmg = None self.attr_listener = None def prepare_meas(self, config): """ Prepare measurement group and returns the channel names""" pool = self.pool # creating mg user configuration and obtaining channel ids mg_conf, channel_ids, channel_names = \ createMGUserConfiguration(pool, config) conf = copy.deepcopy(dummyMeasurementGroupConf01) conf["name"] = 'mg1' conf["full_name"] = 'mg1' conf["user_elements"] = channel_ids self.pmg = createPoolMeasurementGroup(pool, conf) pool.add_element(self.pmg) self.pmg.set_configuration_from_user(mg_conf) return channel_names def prepare_attribute_listener(self): self.attr_listener = AttributeListener() # Add data listener attributes = self.pmg.get_user_elements() for attr in attributes: attr.add_listener(self.attr_listener) def remove_attribute_listener(self): # Remove data listener attributes = self.pmg.get_user_elements() for attr in attributes: attr.remove_listener(self.attr_listener) def acquire(self): """ Run acquisition """ self.pmg.prepare() self.pmg.start_acquisition() acq = self.pmg.acquisition while acq.is_running(): time.sleep(.1) def acq_asserts(self, channel_names, repetitions): # printing acquisition records table = self.attr_listener.get_table() header = table.dtype.names print(header) n_rows = table.shape[0] for row in range(n_rows): print(row, table[row]) # checking if any of data was acquired self.assertTrue(self.attr_listener.data, 'no data were acquired') # checking if all channels produced data for channel in channel_names: msg = 'data from channel %s were not acquired' % channel self.assertIn(channel, header, msg) # checking if all the data were acquired for ch_name in header: ch_data_len = len(table[ch_name]) msg = 'length of data for channel %s is %d and should be %d' %\ (ch_name, ch_data_len, repetitions) self.assertEqual(ch_data_len, repetitions, msg) def meas_double_acquisition(self, config, synch_description, moveable=None): """ Run two acquisition with the same measurement group, first with multiple repetitions and then one repetition. """ channel_names = self.prepare_meas(config) # setting measurement parameters self.pmg.set_synch_description(synch_description) self.pmg.set_moveable(moveable, to_fqdn=False) repetitions = 0 for group in synch_description: repetitions += group[SynchParam.Repeats] self.prepare_attribute_listener() self.acquire() self.acq_asserts(channel_names, repetitions) self.remove_attribute_listener() synch_description = [{ SynchParam.Delay: { SynchDomain.Time: 0 }, SynchParam.Active: { SynchDomain.Time: 0.1 }, SynchParam.Total: { SynchDomain.Time: 0.2 }, SynchParam.Repeats: 1 }] self.pmg.synch_description = synch_description self.acquire() # TODO: implement asserts of Timer acquisition def meas_double_acquisition_samemode(self, config, synch_description, moveable=None): """ Run two acquisition with the same measurement group. """ channel_names = self.prepare_meas(config) self.pmg.set_synch_description(synch_description) self.pmg.set_moveable(moveable, to_fqdn=False) repetitions = 0 for group in synch_description: repetitions += group[SynchParam.Repeats] self.prepare_attribute_listener() self.acquire() self.acq_asserts(channel_names, repetitions) self.remove_attribute_listener() self.prepare_attribute_listener() self.acquire() self.acq_asserts(channel_names, repetitions) self.remove_attribute_listener() # TODO: implement asserts of Timer acquisition def consecutive_acquisitions(self, pool, config, synch_description): # creating mg user configuration and obtaining channel ids mg_conf, channel_ids, channel_names = createMGUserConfiguration( pool, config) # setting mg configuration - this cleans the action cache! self.pmg.set_configuration_from_user(mg_conf) repetitions = 0 for group in synch_description: repetitions += group[SynchParam.Repeats] self.prepare_attribute_listener() self.acquire() self.acq_asserts(channel_names, repetitions) def meas_cont_acquisition(self, config, synch_description, moveable=None, second_config=None): """Executes measurement using the measurement group. Checks the lengths of the acquired data. """ jobs_before = get_thread_pool().qsize channel_names = self.prepare_meas(config) self.pmg.set_synch_description(synch_description) self.pmg.set_moveable(moveable, to_fqdn=False) repetitions = 0 for group in synch_description: repetitions += group[SynchParam.Repeats] self.prepare_attribute_listener() self.acquire() self.acq_asserts(channel_names, repetitions) if second_config is not None: self.consecutive_acquisitions(self.pool, second_config, synch_description) # checking if there are no pending jobs jobs_after = get_thread_pool().qsize msg = ('there are %d jobs pending to be done after the acquisition ' + '(before: %d)') % (jobs_after, jobs_before) self.assertEqual(jobs_before, jobs_after, msg) def stop_acquisition(self): """Method used to abort a running acquisition""" self.pmg.stop() def meas_cont_stop_acquisition(self, config, synch_description, moveable=None): """Executes measurement using the measurement group and tests that the acquisition can be stopped. """ self.prepare_meas(config) self.pmg.synch_description = synch_description self.pmg.set_moveable(moveable, to_fqdn=False) self.prepare_attribute_listener() self.pmg.prepare() self.pmg.start_acquisition() # retrieving the acquisition since it was cleaned when applying mg conf acq = self.pmg.acquisition # starting timer (0.05 s) which will stop the acquisiton threading.Timer(0.2, self.stop_acquisition).start() # waiting for acquisition and tggeneration to be stoped by thread while acq.is_running(): time.sleep(0.05) msg = "acquisition shall NOT be running after stopping it" self.assertEqual(acq.is_running(), False, msg) tp = get_thread_pool() numBW = tp.getNumOfBusyWorkers() msg = "The number of busy workers is not zero; numBW = %s" % (numBW) self.assertEqual(numBW, 0, msg) # print the acquisition records for i, record in \ enumerate(zip(*list(self.attr_listener.data.values()))): print(i, record) def meas_contpos_acquisition(self, config, synch_description, moveable, second_config=None): # TODO: this code is ready only for one group configuration initial = \ synch_description[0][SynchParam.Initial][SynchDomain.Position] total = synch_description[0][SynchParam.Total][SynchDomain.Position] repeats = synch_description[0][SynchParam.Repeats] position = initial + total * repeats mot = self.mots[moveable] mot.set_base_rate(0) mot.set_acceleration(0.1) mot.set_velocity(0.5) channel_names = self.prepare_meas(config) self.pmg.synch_description = synch_description self.pmg.set_moveable(moveable, to_fqdn=False) repetitions = 0 for group in synch_description: repetitions += group[SynchParam.Repeats] self.prepare_attribute_listener() self.pmg.prepare() self.pmg.start_acquisition() mot.set_position(position) acq = self.pmg.acquisition # waiting for acquisition while acq.is_running(): time.sleep(0.1) # time.sleep(3) self.acq_asserts(channel_names, repetitions) def tearDown(self): self.attr_listener = None self.pmg = None
class BaseAcquisition(object): def setUp(self, pool): self.pool = pool self.pmg = None self.attr_listener = None def prepare_meas(self, config): """ Prepare measurement group and returns the channel names""" pool = self.pool # creating mg user configuration and obtaining channel ids mg_conf, channel_ids, channel_names = \ createMGUserConfiguration(pool, config) conf = copy.deepcopy(dummyMeasurementGroupConf01) conf["name"] = 'mg1' conf["full_name"] = 'mg1' conf["user_elements"] = channel_ids self.pmg = createPoolMeasurementGroup(pool, conf) pool.add_element(self.pmg) self.pmg.set_configuration_from_user(mg_conf, to_fqdn=False) return channel_names def prepare_attribute_listener(self): self.attr_listener = AttributeListener() # Add data listener attributes = self.pmg.get_user_elements() for attr in attributes: attr.add_listener(self.attr_listener) def remove_attribute_listener(self): # Remove data listener attributes = self.pmg.get_user_elements() for attr in attributes: attr.remove_listener(self.attr_listener) def acquire(self): """ Run acquisition """ self.pmg.start_acquisition() acq = self.pmg.acquisition while acq.is_running(): time.sleep(.1) def acq_asserts(self, channel_names, repetitions): # printing acquisition records table = self.attr_listener.get_table() header = table.dtype.names print header n_rows = table.shape[0] for row in xrange(n_rows): print row, table[row] # checking if any of data was acquired self.assertTrue(self.attr_listener.data, 'no data were acquired') # checking if all channels produced data for channel in channel_names: msg = 'data from channel %s were not acquired' % channel self.assertIn(channel, header, msg) # checking if all the data were acquired for ch_name in header: ch_data_len = len(table[ch_name]) msg = 'length of data for channel %s is %d and should be %d' %\ (ch_name, ch_data_len, repetitions) self.assertEqual(ch_data_len, repetitions, msg) def meas_double_acquisition(self, config, synchronization, moveable=None): """ Run two acquisition with the same measurement group, first with multiple repetitions and then one repetition. """ channel_names = self.prepare_meas(config) # setting measurement parameters self.pmg.set_synchronization(synchronization) self.pmg.set_moveable(moveable, to_fqdn=False) repetitions = 0 for group in synchronization: repetitions += group[SynchParam.Repeats] self.prepare_attribute_listener() self.acquire() self.acq_asserts(channel_names, repetitions) self.remove_attribute_listener() synchronization = [{SynchParam.Delay: {SynchDomain.Time: 0}, SynchParam.Active: {SynchDomain.Time: 0.1}, SynchParam.Total: {SynchDomain.Time: 0}, SynchParam.Repeats: 1}] self.pmg.synchronization = synchronization self.acquire() # TODO: implement asserts of Timer acquisition def meas_double_acquisition_samemode(self, config, synchronization, moveable=None): """ Run two acquisition with the same measurement group. """ channel_names = self.prepare_meas(config) self.pmg.set_synchronization(synchronization) self.pmg.set_moveable(moveable, to_fqdn=False) repetitions = 0 for group in synchronization: repetitions += group[SynchParam.Repeats] self.prepare_attribute_listener() self.acquire() self.acq_asserts(channel_names, repetitions) self.remove_attribute_listener() self.prepare_attribute_listener() self.acquire() self.acq_asserts(channel_names, repetitions) self.remove_attribute_listener() # TODO: implement asserts of Timer acquisition def consecutive_acquisitions(self, pool, config, synchronization): # creating mg user configuration and obtaining channel ids mg_conf, channel_ids, channel_names = createMGUserConfiguration( pool, config) # setting mg configuration - this cleans the action cache! self.pmg.set_configuration_from_user(mg_conf, to_fqdn=False) repetitions = 0 for group in synchronization: repetitions += group[SynchParam.Repeats] self.prepare_attribute_listener() self.acquire() self.acq_asserts(channel_names, repetitions) def meas_cont_acquisition(self, config, synchronization, moveable=None, second_config=None): """Executes measurement using the measurement group. Checks the lengths of the acquired data. """ jobs_before = get_thread_pool().qsize channel_names = self.prepare_meas(config) self.pmg.set_synchronization(synchronization) self.pmg.set_moveable(moveable, to_fqdn=False) repetitions = 0 for group in synchronization: repetitions += group[SynchParam.Repeats] self.prepare_attribute_listener() self.acquire() self.acq_asserts(channel_names, repetitions) if second_config is not None: self.consecutive_acquisitions( self.pool, second_config, synchronization) # checking if there are no pending jobs jobs_after = get_thread_pool().qsize msg = ('there are %d jobs pending to be done after the acquisition ' + '(before: %d)') % (jobs_after, jobs_before) self.assertEqual(jobs_before, jobs_after, msg) def stop_acquisition(self): """Method used to abort a running acquisition""" self.pmg.stop() def meas_cont_stop_acquisition(self, config, synchronization, moveable=None): """Executes measurement using the measurement group and tests that the acquisition can be stopped. """ self.prepare_meas(config) self.pmg.synchronization = synchronization self.pmg.set_moveable(moveable, to_fqdn=False) self.prepare_attribute_listener() self.pmg.start_acquisition() # retrieving the acquisition since it was cleaned when applying mg conf acq = self.pmg.acquisition # starting timer (0.05 s) which will stop the acquisiton threading.Timer(0.2, self.stop_acquisition).start() # waiting for acquisition and tggeneration to be stoped by thread while acq.is_running(): time.sleep(0.05) msg = "acquisition shall NOT be running after stopping it" self.assertEqual(acq.is_running(), False, msg) tp = get_thread_pool() numBW = tp.getNumOfBusyWorkers() msg = "The number of busy workers is not zero; numBW = %s" % (numBW) self.assertEqual(numBW, 0, msg) # print the acquisition records for i, record in enumerate(zip(*self.attr_listener.data.values())): print i, record def meas_contpos_acquisition(self, config, synchronization, moveable, second_config=None): # TODO: this code is ready only for one group configuration initial = synchronization[0][SynchParam.Initial][SynchDomain.Position] total = synchronization[0][SynchParam.Total][SynchDomain.Position] repeats = synchronization[0][SynchParam.Repeats] position = initial + total * repeats mot = self.mots[moveable] mot.set_base_rate(0) mot.set_acceleration(0.1) mot.set_velocity(0.5) channel_names = self.prepare_meas(config) self.pmg.synchronization = synchronization self.pmg.set_moveable(moveable, to_fqdn=False) repetitions = 0 for group in synchronization: repetitions += group[SynchParam.Repeats] self.prepare_attribute_listener() self.pmg.start_acquisition() mot.set_position(position) acq = self.pmg.acquisition # waiting for acquisition while acq.is_running(): time.sleep(0.1) # time.sleep(3) self.acq_asserts(channel_names, repetitions) def tearDown(self): self.attr_listener = None self.pmg = None