Exemplo n.º 1
0
    def load(self, path):
        """Return workspace configuration as dictionary.

        Args:
            path: configuration file used to generate workspace
                configuration dictionary.

        """
        scheme = {
            'workspace': {
                'description': str,
                'maintainer': str,
                'email': str,
                'startup_script': str
            },
            'folders': {
                'datasets': str,
                'networks': str,
                'systems': str,
                'models': str,
                'scripts': str
            }
        }

        config = ini.load(path, scheme=scheme)
        config['type'] = 'base.Workspace'

        return {'config': config}
Exemplo n.º 2
0
    def load(self, filepath: PathLike, pwd: OptBytes = None) -> None:
        """Load Workspace from file.

        Args:
            filepath: String or :term:`path-like object`, that points to a valid
                workspace file. If the filepath points to a valid workspace
                file, then the class instance is initialized with a memory copy
                of the file. If the given file, however, does not exist, isn't a
                valid ZipFile, or does not contain a workspace configuration,
                respectively one of the errors FileNotFoundError, BadZipFile or
                FileFormatError is raised.
            pwd: Bytes representing password of workspace file.

        """
        super().load(filepath, pwd=pwd)

        # Try to open and load workspace configuration from buffer
        scheme = {
            'dc': self._get_attr_types(group='dc'),
            'hooks': self._get_attr_types(category='hooks')
        }
        try:
            with self.open(self._config_file) as file:
                cfg = ini.load(file, scheme=scheme)
        except KeyError as err:
            if isinstance(self._path, Path):
                raise FileFormatError(self._path, 'rian workspace') from err
            raise

        # Link configuration
        self._set_attr_values(cfg.get('dc', {}), group='dc')  # type: ignore
Exemplo n.º 3
0
    def load(self, path):
        """Return network configuration as dictionary.

        Args:
            path: configuration file used to generate network
                configuration dictionary.

        """
        from hup.io import ini

        scheme = {'network': {'type': str}}
        network = ini.load(path, scheme=scheme)
        if not network \
            or not 'network' in network \
            or not 'type' in network['network']:
            raise ValueError("could not import network: "
                             "configuration file '%s' is not valid." % path)

        if network['network']['type'] in \
            ['layer.MultiLayer', 'layer.Shallow', 'layer.Factor']:
            return self._parse_layer_network(path)

        raise ValueError(
            "could not import network "
            "configuration file '%s' contains unsupported network "
            "type '%s'." % (path, network['network']['type']))
Exemplo n.º 4
0
    def load(self, path):
        """Return system configuration as dictionary.

        Args:
            path: configuration file used to generate system
                configuration dictionary.

        """
        from hup.base import env
        from hup.io import ini

        # import ini file to dictionary, using ini file structure
        # described with regular expressions
        system = ini.load(path,
                          scheme={
                              'system': {
                                  'name': str,
                                  'type': str
                              },
                              'schedule [.0-9a-zA-Z]*': {
                                  'system [.0-9a-zA-Z]*': dict
                              }
                          })

        if not system \
            or not 'system' in system \
            or not 'type' in system['system']:
            raise ValueError("""could not import system:
                configuration file '%s' is not valid.""" % (path))

        config = system['system'].copy()

        # update / set name
        if 'name' not in config: config['name'] = env.basename(path)

        # update / set optimization schedules
        schedules = {}
        for key in system:
            if key[:8].lower() != 'schedule': continue
            name = key[9:]
            schedules[name] = {'name': name}
            for syskey in system[key]:
                if syskey[:6].lower() != 'system': continue
                systype = syskey[7:]
                schedules[name][systype] = system[key][syskey].copy()

        config['schedules'] = schedules

        return {'config': config}
Exemplo n.º 5
0
    def __init__(self, site: bool = True, **kwds):
        """ """
        self._config = {**self._default, **kwds}

        # reset workspace to default values
        self._set_workspace_reset()

        # update basepaths from user configuration
        configfile = self._config['default']['path']['baseconf']
        configfile = self._get_path_expand(configfile)

        if os.path.exists(configfile):
            ini_dict = ini.load(configfile, scheme=self._struct)

            if 'folders' in ini_dict:
                for key, val in ini_dict['folders'].items():
                    path = self._get_path_expand(val)
                    if path:
                        self._config['default']['basepath'][key] = path

            if 'files' in ini_dict:
                for key, val in ini_dict['files'].items():
                    path = self._get_path_expand(val)
                    if path: self._config['current']['path'][key] = path
Exemplo n.º 6
0
 def _load_config(self) -> None:
     config = ini.load(
         self._config_file_path, scheme=self._config_file_struct)
     if 'session' in config and isinstance(config['session'], dict):
         for key, val in config['session'].items():
             self.config[key] = val
Exemplo n.º 7
0
 def test_load(self) -> None:
     obj = ini.load(self.filepath, scheme=self.scheme)  # type: ignore
     self.assertEqual(obj, self.obj)
     obj = ini.load(self.filepath, autocast=True)
     self.assertEqual(obj, self.obj)
Exemplo n.º 8
0
    def _parse_layer_network(self, path):
        from hup.base import env
        from hup.io import ini

        scheme = {
            'network': {
                'name': str,
                'description': str,
                'branch': str,
                'version': str,
                'about': str,
                'author': str,
                'email': str,
                'license': str,
                'type': str,
                'layers': list,
                'directed': bool,
                'labelformat': str
            },
            'layer [0-9a-zA-Z]*': {
                'name': str,
                'function': str,
                'distribution': str,
                'type': str,
                'visible': bool,
                'file': str,
                'nodes': list,
                'size': int
            },
            'binding [0-9a-zA-Z]*-[0-9a-zA-Z]*': {
                '[0-9a-zA-Z]*': list
            }
        }

        ini_dict = ini.load(path, scheme=scheme)
        config = ini_dict['network'].copy()

        # layers
        if 'layers' not in config:
            raise Warning("""file '%s' does not
                contain parameter 'layers'.""" % path)
        config['layer'] = config['layers']
        del config['layers']

        # name
        if 'name' not in config:
            config['name'] = env.basename(path)

        # directed
        if 'directed' not in config:
            config['directed'] = True

        # node labelformat
        if 'labelformat' not in config:
            config['labelformat'] = 'generic:string'

        # init network dictionary
        config['nodes'] = {}
        config['edges'] = {}
        config['layers'] = {}

        visible_layer_ids = [0, len(config['layer']) - 1]

        # [layer *]
        for layer_id, layer in enumerate(config['layer']):

            layer_section = 'layer ' + layer
            if layer_section not in ini_dict:
                raise Warning("""could not import layer
                    network: file '%s' does not contain information
                    about layer '%s'.""" % (path, layer))

            sec_data = ini_dict[layer_section]
            config['layers'][layer] = {}
            lay_data = config['layers'][layer]

            # get name of layer
            lay_data['name'] = sec_data.get('name')

            # get visibility (observable, latent) of nodes in layer
            if 'visible' in sec_data:
                lay_data['visible'] = sec_data['visible']
            else:
                lay_data['visible'] = layer_id in visible_layer_ids

            # get type of layer nodes
            if 'type' in sec_data:
                lay_data['type'] = sec_data.get('type')

            # get function of layer nodes
            if 'function' in sec_data:
                lay_data['function'] = sec_data.get('function')

            # get distribution of layer nodes
            if 'distribution' in sec_data:
                lay_data['distribution'] = sec_data.get('distribution')

            # get nodes of layer from given list file ('file')
            # or from list ('nodes') or from given layer size ('size')
            if 'file' in sec_data:
                file_str = sec_data['file']
                list_file = rian.workspace._expand_path(file_str)
                if not os.path.exists(list_file):
                    raise ValueError("""listfile '%s'
                        does not exists!""" % list_file)
                with open(list_file, 'r') as list_file:
                    fileLines = list_file.readlines()
                node_list = [node.strip() for node in fileLines]
            elif 'nodes' in sec_data:
                node_list = sec_data['nodes']
            elif 'size' in sec_data:
                node_list = ['n%s' % (n) \
                    for n in range(1, sec_data['size'] + 1)]
            else:
                raise Warning("""could not import layer
                    network: layer '%s' does not contain valid node
                    information!""" % (layer))

            config['nodes'][layer] = []
            for node in node_list:
                node = node.strip()
                if node == '':
                    continue
                if node not in config['nodes'][layer]:
                    config['nodes'][layer].append(node)

        # parse '[binding *]' sections and add edges to network dict
        for i in range(len(config['layer']) - 1):
            src_layer = config['layer'][i]
            tgt_layer = config['layer'][i + 1]

            edge_layer = (src_layer, tgt_layer)
            config['edges'][edge_layer] = []
            edge_section = 'binding %s-%s' % (src_layer, tgt_layer)

            # create full binding between two layers if not specified
            if edge_section not in ini_dict:
                for src_node in config['nodes'][src_layer]:
                    for tgt_node in config['nodes'][tgt_layer]:
                        edge = (src_node, tgt_node)
                        config['edges'][edge_layer].append(edge)
                continue

            # get edges from '[binding *]' section
            for src_node in ini_dict[edge_section]:
                src_node = src_node.strip()
                if src_node == '':
                    continue
                if src_node not in config['nodes'][src_layer]:
                    continue
                for tgt_node in ini_dict[edge_section][src_node]:
                    tgt_node = tgt_node.strip()
                    if tgt_node == '':
                        continue
                    if tgt_node not in config['nodes'][tgt_layer]:
                        continue
                    edge = (src_node, tgt_node)
                    if edge in config['edges'][edge_layer]:
                        continue
                    config['edges'][edge_layer].append(edge)

        # check network binding
        for i in range(len(config['layer']) - 1):
            src_layer = config['layer'][i]
            tgt_layer = config['layer'][i + 1]

            edge_layer = (src_layer, tgt_layer)
            if config['edges'][edge_layer] == []:
                raise Warning("""layer '%s' and
                    layer '%s' are not connected!""" % (src_layer, tgt_layer))

        return {'config': config}