Skip to content

Commit

Permalink
Merge pull request #61 from jjhelmus/nmrml
Browse files Browse the repository at this point in the history
Initial support for reading nmrML files
  • Loading branch information
jjhelmus committed Mar 7, 2017
2 parents ca8dc08 + e84e48b commit 35afa66
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fileio modules
bruker
convert
fileiobase
nmrml
pipe
rnmrtk
simpson
Expand Down
32 changes: 32 additions & 0 deletions doc/source/reference/nmrml.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.. _nmrml_module:

nmrglue.nmrml
=============

.. automodule:: nmrglue.fileio.nmrml

This modules is imported as nmrglue.nmrml and can be called as such.

User Information
----------------

User Functions
^^^^^^^^^^^^^^

.. autosummary::
:toctree: generated/

read

Developer Infomation
--------------------

Developer Functions
^^^^^^^^^^^^^^^^^^^

These functions are typically not used directly by users.

.. autosummary::
:toctree: generated/

_get_nmrml_data
2 changes: 1 addition & 1 deletion nmrglue/fileio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from . import bruker
from . import convert
# from . import glue
from . import nmrml
from . import pipe
from . import rnmrtk
from . import simpson
Expand Down
76 changes: 76 additions & 0 deletions nmrglue/fileio/nmrml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
Functions for reading `nmrML <http://nmrml.org/>`_ files.
"""

import base64
import zlib
from warnings import warn

import numpy as np


def read(filename, data_dtype=None):
"""
Read a nmrML file.
Parameters
----------
filename : str
Name of nmrML file to read.
data_dtype : str, optional
NumPy data type of the data. None, the default, will determine this
data type from the infomation in the file. Occasionally this
information is incorrect and this argument can be used to explicitly
supply this information.
Returns
-------
dic : dict
Dictionary of spectra parameters.
data : ndarray, 1D
Array of spectral data.
"""
import xmltodict # delay import so that xmltodict is optional
with open(filename, 'rb') as nmrml_file:
doc = xmltodict.parse(nmrml_file.read())

fid_dict = doc['nmrML']['acquisition']['acquisition1D']['fidData']
data = _get_nmrml_data(fid_dict, data_dtype)

dic = doc['nmrML']
return dic, data


BYTE_FORMAT_TO_DTYPE = {
'float32': '<f4',
'float64': '<f8',
'complex64': '<c8',
'complex128': '<c16',
}


def _get_nmrml_data(fid_dict, data_dtype):
""" Return a NumPy array for spectral data in a nmrML data file. """
# read required parameters from dictionary
byte_format = fid_dict['@byteFormat'].lower()
compressed = fid_dict['@compressed'].lower() == 'true'
encoded_length = int(fid_dict['@encodedLength'])
base64_text = fid_dict['#text']
if len(base64_text) != encoded_length:
warn("Size of encoded text (%i) does not match stated length (%i)"
% (len(base64_text), encoded_length))

# decode and uncompress
compressed_bytes = base64.b64decode(base64_text)
if compressed:
uncompressed_bytes = zlib.decompress(compressed_bytes)
else:
uncompressed_bytes = compressed_bytes

# convert to ndarray
if data_dtype is None:
data_dtype = BYTE_FORMAT_TO_DTYPE[byte_format]
data = np.fromstring(uncompressed_bytes, dtype=data_dtype)

return data

0 comments on commit 35afa66

Please sign in to comment.