Author: bugman Date: Mon Dec 8 16:04:49 2014 New Revision: 27016 URL: http://svn.gna.org/viewcvs/relax?rev=27016&view=rev Log: Created an initial data store object for handling the conversion of nmrglue data to and from XML. This is the data_store.nmrglue.Nmrglue data container which inherits from data_store.data_classes.Element. Custom to_xml() and from_xml() methods have been added to specifically convert the numpy data element to and from Base64 format for fast and efficient storage. Added: branches/nmrglue/data_store/nmrglue.py Added: branches/nmrglue/data_store/nmrglue.py URL: http://svn.gna.org/viewcvs/relax/branches/nmrglue/data_store/nmrglue.py?rev=27016&view=auto ============================================================================== --- branches/nmrglue/data_store/nmrglue.py (added) +++ branches/nmrglue/data_store/nmrglue.py Mon Dec 8 16:04:49 2014 @@ -0,0 +1,102 @@ +############################################################################### +# # +# Copyright (C) 2014 Edward d'Auvergne # +# # +# This file is part of the program relax (http://www.nmr-relax.com). # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <http://www.gnu.org/licenses/>. # +# # +############################################################################### + +# Module docstring. +"""Special relax data storage for nmrglue data.""" + +# Python module imports. +from base64 import b64encode, decodestring +from numpy import float32, frombuffer + +# relax module imports. +from data_store.data_classes import Element +from lib.xml import object_to_xml, xml_to_object + + +class Nmrglue(Element): + """Container for the global GUI data structures.""" + + def __init__(self): + """Initialise the container info.""" + + # Execute the base class __init__() method. + super(Gui, self).__init__() + + # Initialise the data. + self.dic = None + self.udic = None + self.data = None + + + def from_xml(self, nmrglue_node, file_version=1): + """Recreate the nmrglue data structure from the XML gui node. + + @param gui_node: The gui XML node. + @type gui_node: xml.dom.minicompat.Element instance + @keyword file_version: The relax XML version of the XML file. + @type file_version: int + """ + + # Get the data node. + data_nodes = nmrglue_node.getElementsByTagName('data') + + # Loop over the info nodes of the Python object. + for sub_node in node.childNodes: + # Get the value. + if sub_node.localName == 'value': + # Convert from Base64 to numpy.float32. + buffer = decodestring(sub_node.childNodes[0]) + self.data = frombuffer(buffer, dtype=np.float32) + + # The shape attribute. + shape = eval(node.getAttribute('shape')) + + # Reshape the data. + self.data.reshape(shape) + + # Recreate all the other data structures. + xml_to_object(gui_node, self, file_version=file_version, blacklist=['data']) + + + def to_xml(self, doc, element): + """Create an XML element for the container. + + @param doc: The XML document object. + @type doc: xml.dom.minidom.Document instance + @param element: The element to add the data container XML element to. + @type element: XML element object + """ + + # Call the parent class method for all but the data variable. + self.blacklist.append('data') + super(Nmrglue, self).to_xml(doc, element) + + # Convert the data into a Base64 string. + string = b64encode(self.data) + + # Store the value as the string. + val_elem = doc.createElement('value') + element.appendChild(val_elem) + val_elem.appendChild(doc.createTextNode(string)) + + # Set the type and shape as attributes. + element.setAttribute('type', 'base64, numpy.float32') + element.setAttribute('shape', repr(self.data.shape))