예제 #1
0
    def load_from_tag(self, mn_tag, properties_to_include=None, properties_to_ignore=None, nodeclasses_to_include=None,
                      nodeclasses_to_ignore=None, networks_to_include=None, networks_to_ignore=None):
        """
        Parses the content of an lxml _Element containing a Meta-Network and loads the contents
        :param mn_tag: An lxml _Element containing a Dynamic Meta-Network
        :param properties_to_include: a list of nodeclass properties that should be included
        :param properties_to_ignore: a list of nodeclass properties that should be ignored
        :param nodeclasses_to_include: a list of nodeclasses that should be included
        :param nodeclasses_to_ignore: a list of nodeclasses that should be ignored
        :param networks_to_include: a list of networks that should be included
        :param networks_to_ignore: a list of networks that should be ignored
        :type mn_tag: An lxml _Element containing a DynamicMetaNetwork
        :type properties_to_include: list
        :type properties_to_ignore: list
        :type nodeclasses_to_include: list
        :type nodeclasses_to_ignore: list
        :type networks_to_include: list
        :type networks_to_ignore: list
        :raise TypeError: if dnn isn't a BeautifulSoup Tag
        """
        prop_inclusion_test = dmlpu.validate_and_get_inclusion_test(
            (properties_to_include, 'properties_to_include'),
            (properties_to_ignore, 'properties_to_ignore'))
        nodeclass_inclusion_test = dmlpu.validate_and_get_inclusion_test(
            (nodeclasses_to_include, 'nodeclasses_to_include'),
            (nodeclasses_to_ignore, 'nodeclasses_to_ignore'))
        network_inclusion_test = dmlpu.validate_and_get_inclusion_test(
            (networks_to_include, 'networks_to_include'),
            (networks_to_ignore, 'networks_to_ignore'))

        for attrib_key in mn_tag.attrib:
            self.attributes[attrib_key] = dmlpu.format_prop(mn_tag.attrib[attrib_key])

        properties_tag = mn_tag.find('properties')
        if properties_tag is not None:
            for prop in properties_tag.iterfind('property'):
                self.properties[prop.attrib['id']] = dmlpu.format_prop(prop.attrib['value'])

        ##TODO: deal with sources tag in Automap output

        self.propertyIdentities = \
            dmlpu.get_property_identities_dict(mn_tag.find('propertyIdentities'), prop_inclusion_test)

        self.__node_tree = dmlpu.get_nodeclass_dict(mn_tag.find('nodes'), prop_inclusion_test, nodeclass_inclusion_test)

        for nk_tag in mn_tag.find('networks').iterfind('network'):
            if not network_inclusion_test(nk_tag.attrib['id']):
                continue

            self._parse_and_add_graph_tag(nk_tag)
예제 #2
0
    def load_from_dynetml(self, mn_text, properties_to_include=None, properties_to_ignore=None,
                          nodeclasses_to_include=None, nodeclasses_to_ignore=None, networks_to_include=None,
                          networks_to_ignore=None):
        """
        Parses XML containing a Meta-Network and loads the contents
        :param mn_text: XML containing a Meta-Network
        :param properties_to_include: a list of nodeclass properties that should be included
        :param properties_to_ignore: a list of nodeclass properties that should be ignored
        :param nodeclasses_to_include: a list of nodeclasses that should be included
        :param nodeclasses_to_ignore: a list of nodeclasses that should be ignored
        :param networks_to_include: a list of networks that should be included
        :param networks_to_ignore: a list of networks that should be ignored
        :param mn_text: str or unicode
        :param properties_to_include: lists
        :param properties_to_ignore: lists
        :param nodeclasses_to_include: lists
        :param nodeclasses_to_ignore: lists
        :param networks_to_include: lists
        :param networks_to_ignore: lists
        :raise TypeError: if mn_text isn't a str or unicode
        """
        dmlpu.check_type(mn_text, 'mn_text', (unicode, str))
        mn_tag = etree.XML(mn_text)
        if mn_tag.tag != 'MetaNetwork':
            return
        for attrib_key in mn_tag.attrib:
            self.attributes[attrib_key] = dmlpu.format_prop(mn_tag.attrib[attrib_key])

        self.load_from_tag(mn_tag, properties_to_include, properties_to_ignore, nodeclasses_to_include,
                           nodeclasses_to_ignore, networks_to_include, networks_to_ignore)
예제 #3
0
    def load_from_tag(self, mn_tag, properties_to_include=None, properties_to_ignore=None, nodeclasses_to_include=None,
                      nodeclasses_to_ignore=None, networks_to_include=None, networks_to_ignore=None):
        """
        Parses the content of an :class:`lxml._Element` containing a meta-network and loads the contents

        :param lxml._Element mn_tag: A tag containing a dynamic meta-network
        :param list properties_to_include: a list of nodeclass properties that should be included
        :param list properties_to_ignore: a list of nodeclass properties that should be ignored
        :param list nodeclasses_to_include: a list of nodeclasses that should be included
        :param list nodeclasses_to_ignore: a list of nodeclasses that should be ignored
        :param list networks_to_include: a list of networks that should be included
        :param list networks_to_ignore: a list of networks that should be ignored
        """
        prop_inclusion_test = dmlpu.validate_and_get_inclusion_test(
            (properties_to_include, 'properties_to_include'),
            (properties_to_ignore, 'properties_to_ignore'))
        nodeclass_inclusion_test = dmlpu.validate_and_get_inclusion_test(
            (nodeclasses_to_include, 'nodeclasses_to_include'),
            (nodeclasses_to_ignore, 'nodeclasses_to_ignore'))
        network_inclusion_test = dmlpu.validate_and_get_inclusion_test(
            (networks_to_include, 'networks_to_include'),
            (networks_to_ignore, 'networks_to_ignore'))

        for attrib_key in mn_tag.attrib:
            self.attributes[attrib_key] = dmlpu.format_prop(mn_tag.attrib[attrib_key])

        properties_tag = mn_tag.find('properties')
        if properties_tag is not None:
            for prop in properties_tag.iterfind('property'):
                self.properties[prop.attrib['id']] = dmlpu.format_prop(prop.attrib['value'])

        # TODO: Deal with source tag in AutoMap output.

        self.propertyIdentities = \
            dmlpu.get_property_identities_dict(mn_tag.find('propertyIdentities'), prop_inclusion_test)

        self.__node_tree = dmlpu.get_nodeclass_dict(mn_tag.find('nodes'), prop_inclusion_test, nodeclass_inclusion_test)

        for nk_tag in mn_tag.find('networks').iterfind('network'):
            if not network_inclusion_test(nk_tag.attrib['id']):
                continue

            self._parse_and_add_graph_tag(nk_tag)
    def load_from_tag(self, dmn_tag, properties_to_include=None, properties_to_ignore=None, nodeclasses_to_include=None,
                      nodeclasses_to_ignore=None, networks_to_include=None, networks_to_ignore=None, start_date=None,
                      end_date=None):
        """
        Parses the content of an lxml _Element containing a Dynamic Meta-Network and loads the contents
        :param dmn_tag: An lxml _Element containing a Dynamic Meta-Network
        :param properties_to_include: a list of nodeclass properties that should be included
        :param properties_to_ignore: a list of nodeclass properties that should be ignored
        :param nodeclasses_to_include: a list of nodeclasses that should be included
        :param nodeclasses_to_ignore: a list of nodeclasses that should be ignored
        :param networks_to_include: a list of networks that should be included
        :param networks_to_ignore: a list of networks that should be ignored
        :param start_date: MetaNetworks from before this datetime should not be imported
        :param end_date: MetaNetworks from after this datetime should not be imported
        :type dmn_tag: lxml._Element
        :type properties_to_include: list
        :type properties_to_ignore: list
        :type nodeclasses_to_include: list
        :type nodeclasses_to_ignore: list
        :type networks_to_include: list
        :type networks_to_ignore: list
        :type start_date: datetime
        :type end_date: datetime
        :raise TypeError: if dnn isn't an lxml._Element
        """
        #if not isinstance(dmn_tag, (unicode, str)):
        #    raise TypeError('load_from_dynetml needs text containing XML; got {0}'.format(type(dnn_text)))
        for attrib_key in dmn_tag.attrib:
            self.attributes[attrib_key] = dmlpu.format_prop(dmn_tag.attrib[attrib_key])

        if self.__network_format == 'dict':
            from MetaNetworkDict import MetaNetworkDict as MetaNetwork
        elif self.__network_format == 'igraph':
            from MetaNetworkIGraph import MetaNetworkIG as MetaNetwork
        else:
            from MetaNetworkNetworkX import MetaNetworkNX as MetaNetwork

        for mn_tag in dmn_tag.iterfind('MetaNetwork'):

            if start_date is not None:
                if start_date > datetime.strptime(mn_tag.attrib['id'], '%Y%m%dT%H:%M:%S'):
                    continue

            if end_date is not None:
                if end_date < datetime.strptime(mn_tag.attrib['id'], '%Y%m%dT%H:%M:%S'):
                    continue

            self.metanetworks.append(MetaNetwork())
            self.metanetworks[-1].load_from_tag(mn_tag, properties_to_include, properties_to_ignore,
                                                nodeclasses_to_include, nodeclasses_to_ignore, networks_to_include,
                                                networks_to_ignore)
예제 #5
0
    def set_node_property(self, nodeclass_name, nodeset_name, node_name, property_name, value):
        """
        Set the value of a node property

        :param str|unicode nodeclass_name: the name of the parent of nodeset_name
        :param str|unicode nodeset_name: the name of the parent of node_name
        :param str|unicode node_name: the name of the node whose property is being set
        :param str|unicode property_name: the name of the property to be set
        :param str|unicode|bool|float|datetime.datetime value: the value of the parameter
        """
        self.__validate_tree_branch(nodeclass_name, nodeset_name, node_name)
        dmlpu.check_type(property_name, 'property_name', (str, unicode))
        dmlpu.check_key(property_name, 'property_name', self.__node_tree[nodeclass_name][nodeset_name][0], nodeset_name)

        self.__node_tree[nodeclass_name][nodeset_name][1][node_name][property_name] = \
            dmlpu.format_prop(value, self.__node_tree[nodeclass_name][nodeset_name][0][property_name][0])
예제 #6
0
    def set_node_property(self, nodeclass_name, nodeset_name, node_name, property_name, value):
        """
        Set the value of a node property
        :param nodeclass_name: the name of the parent of nodeset_name
        :param nodeset_name: the name of the parent of node_name
        :param node_name: the name of the node whose property is being set
        :param property_name: the name of the property to be set
        :param value: the value of the parameter
        :type nodeclass_name: str or unicode
        :type nodeset_name: str or unicode
        :type node_name: str or unicode
        :type property_name: str or unicode
        :type value: str, unicode, or the appropriate type specified in dmlpu.format_prop
        :raise TypeError: if property_name isn't a str or unicode
        """
        self.__validate_tree_branch(nodeclass_name, nodeset_name, node_name)
        dmlpu.check_type(property_name, 'property_name', (str, unicode))
        dmlpu.check_key(property_name, 'property_name', self.__node_tree[nodeclass_name][nodeset_name][0], nodeset_name)

        self.__node_tree[nodeclass_name][nodeset_name][1][node_name][property_name] = \
            dmlpu.format_prop(value, self.__node_tree[nodeclass_name][nodeset_name][0][property_name][0])