Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d812b5d

Browse files
committed
MAINT: clean up and documentation of tecmag module
1 parent 11dc98a commit d812b5d

4 files changed

Lines changed: 76 additions & 50 deletions

File tree

doc/source/reference/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fileio modules
1919
simpson
2020
sparky
2121
table
22+
tecmag
2223
varian
2324

2425
process modules

doc/source/reference/tecmag.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
nmrglue.tecmag
2+
==============
3+
4+
.. automodule:: nmrglue.fileio.tecmag
5+
6+
This modules is imported as nmrglue.tecmag and can be called as such.
7+
8+
User Information
9+
----------------
10+
11+
User Functions
12+
^^^^^^^^^^^^^^
13+
14+
.. autosummary::
15+
:toctree: generated/
16+
17+
read
18+
guess_udic
19+
20+
21+
Developer Infomation
22+
--------------------
23+
24+
.. include:: ../../../nmrglue/fileio/tecmag.py
25+
:start-line: 4
26+
:end-line: 13

nmrglue/fileio/tecmag.py

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
"""
2-
Functions for reading and Tecmag .tnt data files
2+
Functions for reading Tecmag .tnt data files.
33
"""
4-
__developer_info__ = """
4+
__developer_doc__ = """
55
Tecmag .tnt file format information
6-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7-
6+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
87
The Tecmag .tnt file format is documented with C pseudo-code in the file
98
"A1 - TNMR File Format.doc" distributed with the TNMR software.
109
11-
This file is based on the pytnt module available at
12-
https://www.github.com/chatcannon/pytnt
13-
Please inform upstream if you find a bug or want a new feature.
10+
This file is based on the
11+
`pytnt module <https://www.github.com/chatcannon/pytnt>`_.
12+
Please inform upstream if you find a bug or to request additional features.
1413
1514
"""
1615

1716
import io
1817
import re
19-
from collections import OrderedDict
2018

2119
import numpy as np
2220

2321
from . import fileiobase
2422

2523

26-
tntMagic_re = re.compile(b"^TNT1\.\d\d\d$")
24+
TNTMAGIC_RE = re.compile(b"^TNT1\.\d\d\d$")
2725

28-
tntMagic = np.dtype('a8')
29-
tntTLV = np.dtype([('tag', 'a4'), ('bool', '<u4'), ('length', '<u4')])
26+
TNTMAGIC = np.dtype('a8')
27+
TNTTLV = np.dtype([('tag', 'a4'), ('bool', '<u4'), ('length', '<u4')])
3028

31-
tntTMAG = np.dtype([
29+
TNTTMAG = np.dtype([
3230
('npts', '<i4', 4),
3331
('actual_npts', '<i4', 4),
3432
('acq_points', '<i4'),
@@ -109,10 +107,10 @@
109107
('sequence', 'a32'),
110108
('lock_solvent', 'a16'),
111109
('lock_nucleus', 'a16')
112-
])
110+
])
113111

114112

115-
tntGridAndAxis = np.dtype([
113+
TNTGRIDANDAXIS = np.dtype([
116114
('majorTickInc', '<f8', 12),
117115
('minorIntNum', '<i2', 12),
118116
('labelPrecision', '<i2', 12),
@@ -128,7 +126,7 @@
128126
])
129127

130128

131-
tntTMG2 = np.dtype([
129+
TNTTMG2 = np.dtype([
132130
('real_flag', '<u4'),
133131
('imag_flag', '<u4'),
134132
('magn_flag', '<u4'),
@@ -155,7 +153,7 @@
155153
('ampCtl', '<f8'),
156154
('offset', '<i4'),
157155

158-
('axis_set', tntGridAndAxis),
156+
('axis_set', TNTGRIDANDAXIS),
159157

160158
('display_units', '<i2', 4),
161159
('ref_point', '<i4', 4),
@@ -235,7 +233,7 @@
235233

236234
def read(filename):
237235
"""
238-
Read a .tnt data file
236+
Read a Tecmag .tnt data file.
239237
240238
Parameters
241239
----------
@@ -250,60 +248,62 @@ def read(filename):
250248
Array of NMR data.
251249
252250
"""
253-
tnt_sections = OrderedDict()
251+
tnt_sections = dict()
254252

255253
with open(filename, 'rb') as tntfile:
256254

257-
tntmagic = np.fromstring(tntfile.read(tntMagic.itemsize),
258-
tntMagic, count=1)[0]
255+
tntmagic = np.fromstring(tntfile.read(TNTMAGIC.itemsize),
256+
TNTMAGIC, count=1)[0]
259257

260-
if not tntMagic_re.match(tntmagic):
261-
raise ValueError("Invalid magic number (is '%s' really a TNMR file?): %s" % (filename, tntmagic))
258+
if not TNTMAGIC_RE.match(tntmagic):
259+
err = ("Invalid magic number (is '%s' really TNMR file?): %s" %
260+
(filename, tntmagic))
261+
raise ValueError(err)
262262

263263
##Read in the section headers
264-
tnthdrbytes = tntfile.read(tntTLV.itemsize)
265-
while(tntTLV.itemsize == len(tnthdrbytes)):
266-
TLV = np.fromstring(tnthdrbytes, tntTLV)[0]
267-
data_length = TLV['length']
264+
tnthdrbytes = tntfile.read(TNTTLV.itemsize)
265+
while(TNTTLV.itemsize == len(tnthdrbytes)):
266+
tlv = np.fromstring(tnthdrbytes, TNTTLV)[0]
267+
data_length = tlv['length']
268268
hdrdict = {'offset': tntfile.tell(),
269269
'length': data_length,
270-
'bool': bool(TLV['bool'])}
270+
'bool': bool(tlv['bool'])}
271271
if data_length <= 4096:
272272
hdrdict['data'] = tntfile.read(data_length)
273273
assert(len(hdrdict['data']) == data_length)
274274
else:
275275
tntfile.seek(data_length, io.SEEK_CUR)
276-
tnt_sections[TLV['tag']] = hdrdict
277-
tnthdrbytes = tntfile.read(tntTLV.itemsize)
276+
tnt_sections[tlv['tag']] = hdrdict
277+
tnthdrbytes = tntfile.read(TNTTLV.itemsize)
278278

279-
assert(tnt_sections['TMAG']['length'] == tntTMAG.itemsize)
280-
TMAG = np.fromstring(tnt_sections['TMAG']['data'], tntTMAG, count=1)[0]
279+
assert(tnt_sections['TMAG']['length'] == TNTTMAG.itemsize)
280+
tmag = np.fromstring(tnt_sections['TMAG']['data'], TNTTMAG, count=1)[0]
281281

282282
assert(tnt_sections['DATA']['length'] ==
283-
TMAG['actual_npts'].prod() * 8)
283+
tmag['actual_npts'].prod() * 8)
284284
## For some reason we can't set offset and shape together
285285
#DATA = np.memmap(tntfilename,np.dtype('<c8'), mode='r',
286286
# offset=self.tnt_sections['DATA']['offset'],
287287
# shape=self.TMAG['actual_npts'].tolist(),order='F')
288-
DATA = np.memmap(filename, np.dtype('<c8'), mode='c',
288+
data = np.memmap(filename, np.dtype('<c8'), mode='c',
289289
offset=tnt_sections['DATA']['offset'],
290-
shape=TMAG['actual_npts'].prod())
291-
DATA = np.reshape(DATA, TMAG['actual_npts'], order='F')
290+
shape=tmag['actual_npts'].prod())
291+
data = np.reshape(data, tmag['actual_npts'], order='F')
292292

293-
assert(tnt_sections['TMG2']['length'] == tntTMG2.itemsize)
294-
TMG2 = np.fromstring(tnt_sections['TMG2']['data'], tntTMG2, count=1)[0]
293+
assert(tnt_sections['TMG2']['length'] == TNTTMG2.itemsize)
294+
tmg2 = np.fromstring(tnt_sections['TMG2']['data'], TNTTMG2, count=1)[0]
295295

296296
dic = dict()
297-
for name in tntTMAG.names:
297+
for name in TNTTMAG.names:
298298
if not name.startswith('space'):
299-
dic[name] = TMAG[name]
300-
for name in tntTMG2.names:
299+
dic[name] = tmag[name]
300+
for name in TNTTMG2.names:
301301
if name not in ['Boolean_space', 'unused', 'space', 'axis_set']:
302-
dic[name] = TMG2[name]
303-
for name in tntGridAndAxis.names:
304-
dic[name] = TMG2['axis_set'][name]
302+
dic[name] = tmg2[name]
303+
for name in TNTGRIDANDAXIS.names:
304+
dic[name] = tmg2['axis_set'][name]
305305

306-
return dic, DATA
306+
return dic, data
307307

308308

309309
def guess_udic(dic, data):

tests/test_tecmag.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22

33
import numpy as np
4-
from numpy.testing import assert_allclose
54
from numpy.testing import assert_array_almost_equal
65

76
import nmrglue as ng
@@ -10,13 +9,13 @@
109

1110

1211
def test_tecmag_load_time_domain():
13-
ref1, data = ng.tecmag.read(os.path.join(DATA_DIR, 'tecmag', 'LiCl_ref1.tnt'))
12+
ref1, data = ng.tecmag.read(os.path.join(
13+
DATA_DIR, 'tecmag', 'LiCl_ref1.tnt'))
1414

15-
real, imag, usec = np.loadtxt(os.path.join(DATA_DIR, 'tecmag', 'LiCl_ref1.txt'),
16-
skiprows=3, unpack=True)
15+
real, imag, usec = np.loadtxt(os.path.join(
16+
DATA_DIR, 'tecmag', 'LiCl_ref1.txt'), skiprows=3, unpack=True)
1717

1818
assert_array_almost_equal(data.real.squeeze(), real, decimal=3)
1919
assert_array_almost_equal(data.imag.squeeze(), imag, decimal=3)
2020
assert_array_almost_equal(np.arange(ref1['npts'][0]) *
21-
ref1['dwell'][0] * 1e6,
22-
usec, decimal=3)
21+
ref1['dwell'][0] * 1e6, usec, decimal=3)

0 commit comments

Comments
 (0)