示例#1
0
    def _get_networks_tag(self):
        bs = BeautifulSoup()
        networks_tag = bs.new_tag('networks')
        for key in self.networks:
            network_tag = bs.new_tag('network')
            network_tag['sourceType'] = self.networks[key][1]['sourceType']
            network_tag['source'] = self.networks[key][1]['source']
            network_tag['targetType'] = self.networks[key][1]['targetType']
            network_tag['target'] = self.networks[key][1]['target']
            network_tag['id'] = key
            network_tag['isDirected'] = dmlpu.unformat_prop(self.networks[key]['isDirected'])
            network_tag['allowSelfLoops'] = dmlpu.unformat_prop(self.networks[key]['allowSelfLoops'])
            network_tag['isBinary'] = dmlpu.unformat_prop(self.networks[key]['isBinary'])

            e_l = self.networks[key].edge_list()
            if self.networks[key]['isBinary']:
                for i in range(len(e_l)):
                    network_tag.append(bs.new_tag('link', source=e_l[i][0], target=e_l[i][1]))
            else:
                for i in range(len(e_l)):
                    network_tag.append(bs.new_tag('link', source=e_l[i][0], target=e_l[i][1],
                                                  value=self.networks[key].es[i]['weight']))

            networks_tag.append(network_tag)

        return networks_tag
    def _get_networks_tag(self):
        bs = BeautifulSoup()
        networks_tag = bs.new_tag('networks')
        for key in self.networks:
            network_tag = bs.new_tag('network')
            network_tag['sourceType'] = self.networks[key].graph['sourceType']
            network_tag['source'] = self.networks[key].graph['source']
            network_tag['targetType'] = self.networks[key].graph['targetType']
            network_tag['target'] = self.networks[key].graph['target']
            network_tag['id'] = key
            network_tag['isDirected'] = dmlpu.unformat_prop(
                self.networks[key].graph['isDirected'])
            network_tag['allowSelfLoops'] = dmlpu.unformat_prop(
                self.networks[key].graph['allowSelfLoops'])
            network_tag['isBinary'] = dmlpu.unformat_prop(
                self.networks[key].graph['isBinary'])

            if self.networks[key].graph['isBinary']:
                for edge in self.networks[key].edges_iter(data=True):
                    network_tag.append(
                        bs.new_tag('link',
                                   source=edge[0],
                                   target=edge[1],
                                   value=edge[2]['weight']))
            else:
                for edge in self.networks[key].edges_iter():
                    network_tag.append(
                        bs.new_tag('link', source=edge[0], target=edge[1]))

            networks_tag.append(network_tag)

        return networks_tag
示例#3
0
    def convert_to_dynetml(self, is_entire_file=False):
        """
        Converts the graph to dynetml and returns a BeautifulSoup tag
        :param is_entire_file: if True, wraps value as a soup. If False, returns the top tag
        :type is_entire_file: bool
        :return: bs4.element.Tag
        :raise TypeError: if is_entire_file isn't a bool
        """
        dmlpu.check_type(is_entire_file, 'is_entire_file', bool)

        bs = BeautifulSoup(features='xml')
        bs.append(bs.new_tag('MetaNetwork'))

        for attr in self.attributes:
            bs.MetaNetwork[attr] = dmlpu.unformat_prop(self.attributes[attr])

        bs.MetaNetwork.append(dmlpu.get_property_identities_tag(self.propertyIdentities))

        bs.MetaNetwork.append(bs.new_tag('properties'))
        for key in self.properties:
            prop_tag = bs.new_tag('property')
            prop_tag['id'] = key
            prop_tag['value'] = dmlpu.unformat_prop(self.properties[key])
            bs.MetaNetwork.properties.append(prop_tag)

        bs.MetaNetwork.append(bs.new_tag('nodes'))
        for class_type in self.__node_tree:
            for class_id in self.__node_tree[class_type]:
                nodeclass_tag = bs.new_tag('nodeclass', type=class_type, id=class_id)
                nodeclass_tag.append(dmlpu.get_property_identities_tag(self.__node_tree[class_type][class_id][0]))

                for key in self.__node_tree[class_type][class_id][1]:
                    node_tag = bs.new_tag('node', id=key)
                    for attr in self.__node_tree[class_type][class_id][1][key][0]:
                        node_tag[attr] = dmlpu.unformat_prop(self.__node_tree[class_type][class_id][1][key][0][attr])
                    node_tag.append(dmlpu.get_properties_tag(self.__node_tree[class_type][class_id][1][key][1]))
                    nodeclass_tag.append(node_tag)

                bs.MetaNetwork.nodes.append(nodeclass_tag)

        networks_tag = self._get_networks_tag()
        bs.MetaNetwork.networks.append(networks_tag)

        if not is_entire_file:
            bs = bs.MetaNetwork

        return bs
    def _get_networks_tag(self):
        # bs = BeautifulSoup()
        # networks_tag = bs.new_tag('networks')
        # for key in self.networks:
        #     network_tag = bs.new_tag('network')
        #     network_tag['sourceType'] = self.networks[key][1]['sourceType']
        #     network_tag['source'] = self.networks[key][1]['source']
        #     network_tag['targetType'] = self.networks[key][1]['targetType']
        #     network_tag['target'] = self.networks[key][1]['target']
        #     network_tag['id'] = key
        #     network_tag['isDirected'] = dmlpu.unformat_prop(self.networks[key]['isDirected'])
        #     network_tag['allowSelfLoops'] = dmlpu.unformat_prop(self.networks[key]['allowSelfLoops'])
        #     network_tag['isBinary'] = dmlpu.unformat_prop(self.networks[key]['isBinary'])
        #
        #     e_l = self.networks[key].edge_list()
        #     if self.networks[key]['isBinary']:
        #         for i in range(len(e_l)):
        #             network_tag.append(bs.new_tag('link', source=e_l[i][0], target=e_l[i][1]))
        #     else:
        #         for i in range(len(e_l)):
        #             network_tag.append(bs.new_tag('link', source=e_l[i][0], target=e_l[i][1],
        #                                           value=self.networks[key].es[i]['weight']))
        #
        #     networks_tag.append(network_tag)
        #
        # return networks_tag
        networks_tag = etree.Element('networks')
        for key in self.networks:
            network_tag = etree.SubElement(networks_tag, 'network', attrib={
                'sourceType': self.networks[key][1]['sourceType'], 'source': self.networks[key][1]['source'],
                'targetType': self.networks[key][1]['targetType'], 'target': self.networks[key][1]['target'],
                'id': key, 'isDirected': dmlpu.unformat_prop(self.networks[key]['isDirected']),
                'allowSelfLoops': dmlpu.unformat_prop(self.networks[key]['allowSelfLoops']),
                'isBinary': dmlpu.unformat_prop(self.networks[key]['isBinary'])})

            e_l = self.networks[key].edge_list()
            if self.networks[key]['isBinary']:
                for i in range(len(e_l)):
                    etree.SubElement(network_tag, 'link', attrib={'source': e_l[i][0], 'target': e_l[i][1]})
            else:
                for i in range(len(e_l)):
                    etree.SubElement(network_tag, 'link', attrib={'source': e_l[i][0], 'target': e_l[i][1],
                                                                  'value': self.networks[key].es[i]['weight']})

        return networks_tag
示例#5
0
    def _get_networks_tag(self):
        """Generates an :class:`lxml._Element` from the networks"""
        # bs = BeautifulSoup()
        # networks_tag = bs.new_tag('networks')
        # for key in self.networks:
        #     network_tag = bs.new_tag('network')
        #     network_tag['sourceType'] = self.networks[key][0]['sourceType']
        #     network_tag['source'] = self.networks[key][0]['source']
        #     network_tag['targetType'] = self.networks[key][0]['targetType']
        #     network_tag['target'] = self.networks[key][0]['target']
        #     network_tag['id'] = key
        #     network_tag['isDirected'] = dmlpu.unformat_prop(self.networks[key][0]['isDirected'])
        #     network_tag['allowSelfLoops'] = dmlpu.unformat_prop(self.networks[key][0]['allowSelfLoops'])
        #     network_tag['isBinary'] = dmlpu.unformat_prop(self.networks[key][0]['isBinary'])
        #
        #     if self.networks[key][0]['isBinary']:
        #         for edge in self.networks[key].edges_iter():
        #             network_tag.append(bs.new_tag('link', source=edge[0], target=edge[1]))
        #     else:
        #         for edge in self.networks[key].edges_iter(data=True):
        #             network_tag.append(bs.new_tag('link', source=edge[0], target=edge[1], value=edge[2]['weight']))
        #
        #     networks_tag.append(network_tag)
        #
        # return networks_tag
        networks_tag = etree.Element('networks')
        for key in self.networks:
            network_tag = etree.SubElement(networks_tag, 'network', attrib={
                'sourceType': self.networks[key][0]['sourceType'], 'source': self.networks[key][0]['source'],
                'targetType': self.networks[key][0]['targetType'], 'target': self.networks[key][0]['target'],
                'id': key, 'isDirected': dmlpu.unformat_prop(self.networks[key][0]['isDirected']),
                'allowSelfLoops': dmlpu.unformat_prop(self.networks[key][0]['allowSelfLoops']),
                'isBinary': dmlpu.unformat_prop(self.networks[key][0]['isBinary'])})

            if self.networks[key][0]['isBinary']:
                for edge in self.networks[key].edges_iter():
                    etree.SubElement(network_tag, 'link', attrib={'source': edge[0], 'target': edge[1]})
            else:
                for edge in self.networks[key].edges_iter(data=True):
                    etree.SubElement(network_tag, 'link', attrib={'source': edge[0], 'target': edge[1],
                                                                  'value': edge[2]['weight']})

        return networks_tag
    def _get_networks_tag(self):
        bs = BeautifulSoup()
        networks_tag = bs.new_tag('networks')
        for key in self.networks:
            network_tag = bs.new_tag('network')
            network_tag['sourceType'] = self.networks[key][0]['sourceType']
            network_tag['source'] = self.networks[key][0]['source']
            network_tag['targetType'] = self.networks[key][0]['targetType']
            network_tag['target'] = self.networks[key][0]['target']
            network_tag['id'] = key
            network_tag['isDirected'] = dmlpu.unformat_prop(self.networks[key][0]['isDirected'])
            network_tag['allowSelfLoops'] = dmlpu.unformat_prop(self.networks[key][0]['allowSelfLoops'])
            network_tag['isBinary'] = dmlpu.unformat_prop(self.networks[key][0]['isBinary'])

            if self.networks[key][0]['isBinary']:
                for edge in self.networks[key].edges_iter():
                    network_tag.append(bs.new_tag('link', source=edge[0], target=edge[1]))
            else:
                for edge in self.networks[key].edges_iter(data=True):
                    network_tag.append(bs.new_tag('link', source=edge[0], target=edge[1], value=edge[2]['weight']))

            networks_tag.append(network_tag)

        return networks_tag
    def _get_networks_tag(self):
        bs = BeautifulSoup()
        networks_tag = bs.new_tag("networks")
        for key in self.networks:
            network_tag = bs.new_tag("network")
            network_tag["sourceType"] = self.networks[key].graph["sourceType"]
            network_tag["source"] = self.networks[key].graph["source"]
            network_tag["targetType"] = self.networks[key].graph["targetType"]
            network_tag["target"] = self.networks[key].graph["target"]
            network_tag["id"] = key
            network_tag["isDirected"] = dmlpu.unformat_prop(self.networks[key].graph["isDirected"])
            network_tag["allowSelfLoops"] = dmlpu.unformat_prop(self.networks[key].graph["allowSelfLoops"])
            network_tag["isBinary"] = dmlpu.unformat_prop(self.networks[key].graph["isBinary"])

            if self.networks[key].graph["isBinary"]:
                for edge in self.networks[key].edges_iter(data=True):
                    network_tag.append(bs.new_tag("link", source=edge[0], target=edge[1], value=edge[2]["weight"]))
            else:
                for edge in self.networks[key].edges_iter():
                    network_tag.append(bs.new_tag("link", source=edge[0], target=edge[1]))

            networks_tag.append(network_tag)

        return networks_tag
    def convert_to_dynetml(self):
        """Return the dynamic meta-network as an :class:`lxml._Element`"""
        # bs = BeautifulSoup(features='xml')
        # bs.append(bs.new_tag('DynamicMetaNetwork'))
        # for attr in self.attributes:
        #     bs.DynamicMetaNetwork[attr] = dmlpu.unformat_prop(self.attributes[attr])
        #
        # for mn in self.metanetworks:
        #     bs.DynamicMetaNetwork.append(mn.convert_to_dynetml())
        #
        # if not is_entire_file:
        #     bs = bs.DynamicMetaNetwork
        #
        # return bs
        dmn = etree.Element('DynamicMetaNetwork')
        for attr in self.attributes:
            dmn.attrib[attr] = dmlpu.unformat_prop(self.attributes[attr])

        for mn in self.metanetworks:
            etree.SubElement(dmn, mn.convert_to_dynetml())

        return dmn
    def convert_to_dynetml(self, is_entire_file=False):
        """
        Converts the dynamic meta network to a BeautifulSoup Tag and returns it
        :param is_entire_file: If True, Tag will include XML wrapper code
        :type is_entire_file: bool
        :return: a BeautifulSoup Tag
        :raise TypeError: if is_entire_file is not a bool
        """
        if not isinstance(is_entire_file, bool):
            raise TypeError('is_entire_file must be True or False')

        bs = BeautifulSoup(features='xml')
        bs.append(bs.new_tag('DynamicMetaNetwork'))
        for attr in self.attributes:
            bs.DynamicMetaNetwork[attr] = dmlpu.unformat_prop(self.attributes[attr])

        for mn in self.metanetworks:
            bs.DynamicMetaNetwork.append(mn.convert_to_dynetml())

        if not is_entire_file:
            bs = bs.DynamicMetaNetwork

        return bs
示例#10
0
    def convert_to_dynetml(self):
        """Converts the graph to DyNetML and returns an :class:`lxml._Element`"""

        # bs = BeautifulSoup(features='xml')
        # bs.append(bs.new_tag('MetaNetwork'))
        #
        # for attr in self.attributes:
        #     bs.MetaNetwork[attr] = dmlpu.unformat_prop(self.attributes[attr])
        #
        # bs.MetaNetwork.append(dmlpu.get_property_identities_tag(self.propertyIdentities))
        #
        # bs.MetaNetwork.append(bs.new_tag('properties'))
        # for key in self.properties:
        #     prop_tag = bs.new_tag('property')
        #     prop_tag['id'] = key
        #     prop_tag['value'] = dmlpu.unformat_prop(self.properties[key])
        #     bs.MetaNetwork.properties.append(prop_tag)
        #
        # bs.MetaNetwork.append(bs.new_tag('nodes'))
        # for class_type in self.__node_tree:
        #     for class_id in self.__node_tree[class_type]:
        #         nodeclass_tag = bs.new_tag('nodeclass', type=class_type, id=class_id)
        #         nodeclass_tag.append(dmlpu.get_property_identities_tag(self.__node_tree[class_type][class_id][0]))
        #
        #         for key in self.__node_tree[class_type][class_id][1]:
        #             node_tag = bs.new_tag('node', id=key)
        #             for attr in self.__node_tree[class_type][class_id][1][key][0]:
        #                 node_tag[attr] = dmlpu.unformat_prop(self.__node_tree[class_type][class_id][1][key][0][attr])
        #             node_tag.append(dmlpu.get_properties_tag(self.__node_tree[class_type][class_id][1][key][1]))
        #             nodeclass_tag.append(node_tag)
        #
        #         bs.MetaNetwork.nodes.append(nodeclass_tag)
        #
        # networks_tag = self._get_networks_tag()
        # bs.MetaNetwork.networks.append(networks_tag)
        #
        # if not is_entire_file:
        #     bs = bs.MetaNetwork
        #
        # return bs
        mn = etree.Element('MetaNetwork')

        for attr in self.attributes:
            mn.attrib[attr] = dmlpu.unformat_prop(self.attributes[attr])

        etree.SubElement(mn, dmlpu.get_property_identities_tag(self.propertyIdentities))

        properties_tag = etree.SubElement(mn, 'properties')
        for key in self.properties:
            prop_tag = etree.SubElement(properties_tag, 'property')
            prop_tag.attrib['id'] = key
            prop_tag['value'] = dmlpu.unformat_prop(self.properties[key])

        nodes_tag = etree.SubElement(mn, 'nodes')
        for class_type in self.__node_tree:
            for class_id in self.__node_tree[class_type]:
                nodeclass_tag = etree.SubElement(nodes_tag, 'nodeclass', attrib={'type': class_type, 'id': class_id})
                etree.SubElement(nodeclass_tag,
                                 dmlpu.get_property_identities_tag(self.__node_tree[class_type][class_id][0]))

        etree.SubElement(mn, self._get_networks_tag())

        return mn