def test_get_schema_subtype(): r"""Test get_schema_subtype for allow_instance=True.""" component = 'serializer' subtype = 'direct' doc = {'seritype': subtype} valid = components.create_component(component, seritype=subtype) invalid = components.create_component(component, seritype='json') s = schema.get_schema() kwargs = {'subtype': subtype, 'allow_instance': True} s.validate_component(component, doc, **kwargs) s.validate_component(component, valid, **kwargs) assert_raises(ValidationError, s.validate_component, component, invalid, **kwargs) s.validate_component(component, doc, subtype=subtype) assert_raises(ValidationError, s.validate_component, component, valid, subtype=subtype) # Test for base s.validate_component(component, valid, subtype='base', allow_instance=True) s.validate_component(component, invalid, subtype='base', allow_instance=True)
def test_create_component(): r"""Test dynamic creation of component instance.""" x = components.create_component('serializer', seritype='direct') assert (components.isinstance_component(x, ['serializer'])) assert (components.isinstance_component(x, ['comm', 'serializer'])) assert (not components.isinstance_component(x, ['comm'])) x = components.create_component('serializer') assert (components.isinstance_component(x, ['serializer']))
def check_received_data(cls, transform, x_recv): r"""Check that the received message is equivalent to the test data for the specified type. Args: transform (str): Name of transform being tested. x_recv (object): Received object. Raises: AssertionError: If the received message is not equivalent to the received message. """ try: t = create_component('transform', subtype=transform) except ValueError: def t(x): return x x_sent = t(cls.get_test_data()) print('RECEIVED:') tools.pprint_encoded(x_recv) print('EXPECTED:') tools.pprint_encoded(x_sent) assert_equal(x_recv, x_sent)
def check_received_data(transform, x_recv): r"""Check that the received message is equivalent to the test data for the specified type. Args: transform (str): Name of transform being tested. x_recv (object): Received object. Raises: AssertionError: If the received message is not equivalent to the received message. """ try: t = create_component('transform', subtype=transform) except ComponentError: def t(x): return x x_sent = t(get_test_data(transform)) print('RECEIVED:') tools.pprint_encoded(x_recv) print('EXPECTED:') tools.pprint_encoded(x_sent) if isinstance(x_sent, np.ndarray): np.testing.assert_array_equal(x_recv, x_sent) elif isinstance(x_sent, pd.DataFrame): pd.testing.assert_frame_equal(x_recv, x_sent) else: assert(x_recv == x_sent)
def read_file_w(fname): x = create_component('file', 'table', name='test', address=fname, direction='recv', as_array=True, recv_converter='pandas') msg = x.recv_array()[1] if msg is not None: msg = msg.sort_values(by=['InputMass']).reset_index( drop=True) x.close() return msg
def __init__(self, *args, **kwargs): super(FilterTransform, self).__init__(*args, **kwargs) if isinstance(self.filter, dict): from yggdrasil.schema import get_schema from yggdrasil.components import create_component filter_schema = get_schema().get('filter') filter_kws = dict(self.filter, subtype=filter_schema.identify_subtype( self.filter)) self.filter = create_component('filter', **filter_kws)
def __init__(self, name, translator=None, single_use=False, onexit=None, **kwargs): # kwargs['method'] = 'process' super(ConnectionDriver, self).__init__(name, **kwargs) # Shared attributes (set once or synced using events) self.single_use = single_use self.shared = self.context.Dict() self.shared.update(nrecv=0, nproc=0, nsent=0, state='started', close_state='', _comm_closed=multitasking.DummyEvent(), _skip_after_loop=multitasking.DummyEvent()) # Attributes used by process self._eof_sent = False self._first_send_done = False self._used = False self.onexit = None self.task_thread = None if self.as_process: self.task_thread = RemoteTaskLoop(self, name=('%s.TaskThread' % self.name)) # Translator if translator is None: translator = [] elif not isinstance(translator, list): translator = [translator] self.translator = [] for t in translator: if isinstance(t, dict): t = create_component('transform', **t) if not hasattr(t, '__call__'): raise ValueError("Translator %s not callable." % t) self.translator.append(t) if (onexit is not None) and (not hasattr(self, onexit)): raise ValueError("onexit '%s' is not a class method." % onexit) self.onexit = onexit # Add comms and print debug info self._init_comms(name, **kwargs) self.models = { 'input': list(self.icomm.model_env.keys()), 'output': list(self.ocomm.model_env.keys()) } # self.debug(' env: %s', str(self.env)) self.debug(('\n' + 80 * '=' + '\n' + 'class = %s\n' + ' input: name = %s, address = %s\n' + ' output: name = %s, address = %s\n' + (80 * '=')), self.__class__, self.icomm.name, self.icomm.address, self.ocomm.name, self.ocomm.address)
def __init__(self, name, translator=None, single_use=False, onexit=None, **kwargs): super(ConnectionDriver, self).__init__(name, **kwargs) # Translator if translator is None: translator = [] elif not isinstance(translator, list): translator = [translator] self.translator = [] for t in translator: if isinstance(t, dict): t = create_component('transform', **t) if not hasattr(t, '__call__'): raise ValueError("Translator %s not callable." % t) self.translator.append(t) if (onexit is not None) and (not hasattr(self, onexit)): raise ValueError("onexit '%s' is not a class method." % onexit) self.onexit = onexit # Attributes self._eof_sent = False self.single_use = single_use self._first_send_done = False self._comm_opened = threading.Event() self._comm_closed = False self._used = False self._skip_after_loop = False self._model_exited = False self.nrecv = 0 self.nproc = 0 self.nsent = 0 self.nskip = 0 self.state = 'started' self.close_state = '' # Add comms and print debug info self._init_comms(name, **kwargs) # self.debug(' env: %s', str(self.env)) self.debug(('\n' + 80 * '=' + '\n' + 'class = %s\n' + ' input: name = %s, address = %s\n' + ' output: name = %s, address = %s\n' + (80 * '=')), self.__class__, self.icomm.name, self.icomm.address, self.ocomm.name, self.ocomm.address)
def read_file(self, fname): r"""Read in contents from a file. Args: fname (str): Full path to the file that should be read. Returns: object: File contents. """ x = create_component('file', 'table', name='test', address=fname, direction='recv', as_array=True, recv_converter='pandas') msg = x.recv_array()[1] if msg is not None: msg = msg.sort_values(by=['InputMass']).reset_index(drop=True) x.close() return msg
def test_create_component(): r"""Test dynamic creation of component instance.""" components.create_component('serializer', seritype='direct')