def annotate_from_xml(cls, element, *args, **kwargs): annot_elem = expect_none_or_single( element.findall(NINEML + Annotations.element_name)) if annot_elem is not None: # Extract the annotations annotations = Annotations.from_xml(annot_elem) # Get a copy of the element with the annotations stripped element = copy(element) element.remove(element.find(NINEML + Annotations.element_name)) else: annotations = None nineml_object = from_xml(cls, element, *args, **kwargs) try: nineml_object.annotations = annotations except AttributeError: raise return nineml_object
def from_xml(cls, element, document): check_tag(element, cls) # Get Name name = element.get('name') # Get Source e = expect_single(element.findall(NINEML + 'Source')) e = expect_single(e.findall(NINEML + 'Reference')) source = nineml.user_layer.Reference.from_xml( e, document).user_layer_object # Get Destination e = expect_single(element.findall(NINEML + 'Destination')) e = expect_single(e.findall(NINEML + 'Reference')) destination = nineml.user_layer.Reference.from_xml( e, document).user_layer_object # Get Response e = element.find(NINEML + 'Response') component = e.find(NINEML + 'Component') if component is None: component = e.find(NINEML + 'Reference') response = Component.from_xml(component, document) # Get Plasticity e = expect_none_or_single(element.findall(NINEML + 'Plasticity')) if e is not None: component = e.find(NINEML + 'Component') if component is None: component = e.find(NINEML + 'Reference') plasticity = Component.from_xml(component, document) else: plasticity = None # Get Connectivity e = element.find(NINEML + 'Connectivity') component = e.find(NINEML + 'Component') if component is None: component = e.find(NINEML + 'Reference') connectivity = Component.from_xml(component, document) # Get Delay delay = Delay.from_xml( expect_single(element.findall(NINEML + 'Delay')), document) # Get port connections by Loop through 'source', 'destination', # 'response', 'plasticity' tags and extracting the "From*" elements port_connections = [] for receive_role in cls._component_roles: # Get element for component name e = element.find(NINEML + receive_role.capitalize()) if e is not None: # Plasticity is not required # Loop through all incoming port connections and add them to # list for sender_role in cls._component_roles: pc_elems = e.findall(NINEML + 'From' + sender_role.capitalize()) if sender_role == receive_role and pc_elems: msg = ("{} port connection receives from itself in " "Projection '{}'".format(name, name)) raise NineMLRuntimeError(msg) if (sender_role is 'plasticity' and plasticity is None and len(pc_elems)): msg = ("{} port connection receives from plasticity, " "which wasn't provided for Projection '{}'" .format(receive_role, name)) raise NineMLRuntimeError(msg) for pc in pc_elems: port_connections.append( PortConnection(sender_role, receive_role, pc.get('send_port'), pc.get('receive_port'))) return cls(name=element.attrib["name"], source=source, destination=destination, response=response, plasticity=plasticity, connectivity=connectivity, delay=delay, port_connections=port_connections)
def from_xml(cls, element, document): check_tag(element, cls) # Get Name name = element.get('name') # Get Source e = expect_single(element.findall(NINEML + 'Source')) e = expect_single(e.findall(NINEML + 'Reference')) source = nineml.user_layer.Reference.from_xml( e, document).user_layer_object # Get Destination e = expect_single(element.findall(NINEML + 'Destination')) e = expect_single(e.findall(NINEML + 'Reference')) destination = nineml.user_layer.Reference.from_xml( e, document).user_layer_object # Get Response e = element.find(NINEML + 'Response') component = e.find(NINEML + 'Component') if component is None: component = e.find(NINEML + 'Reference') response = Component.from_xml(component, document) # Get Plasticity e = expect_none_or_single(element.findall(NINEML + 'Plasticity')) if e is not None: component = e.find(NINEML + 'Component') if component is None: component = e.find(NINEML + 'Reference') plasticity = Component.from_xml(component, document) else: plasticity = None # Get Connectivity e = element.find(NINEML + 'Connectivity') component = e.find(NINEML + 'Component') if component is None: component = e.find(NINEML + 'Reference') connectivity = Component.from_xml(component, document) # Get Delay delay = Delay.from_xml( expect_single(element.findall(NINEML + 'Delay')), document) # Get port connections by Loop through 'source', 'destination', # 'response', 'plasticity' tags and extracting the "From*" elements port_connections = [] for receive_role in cls._component_roles: # Get element for component name e = element.find(NINEML + receive_role.capitalize()) if e is not None: # Plasticity is not required # Loop through all incoming port connections and add them to # list for sender_role in cls._component_roles: pc_elems = e.findall(NINEML + 'From' + sender_role.capitalize()) if sender_role == receive_role and pc_elems: msg = ("{} port connection receives from itself in " "Projection '{}'".format(name, name)) raise NineMLRuntimeError(msg) if (sender_role is 'plasticity' and plasticity is None and len(pc_elems)): msg = ( "{} port connection receives from plasticity, " "which wasn't provided for Projection '{}'".format( receive_role, name)) raise NineMLRuntimeError(msg) for pc in pc_elems: port_connections.append( PortConnection(sender_role, receive_role, pc.get('send_port'), pc.get('receive_port'))) return cls(name=element.attrib["name"], source=source, destination=destination, response=response, plasticity=plasticity, connectivity=connectivity, delay=delay, port_connections=port_connections)