-
Notifications
You must be signed in to change notification settings - Fork 256
/
load_nrrd.py
94 lines (70 loc) · 2.02 KB
/
load_nrrd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'''
PyMOL NRRD map file loading
(c) Thomas Holder, Schrodinger, Inc.
License: BSD-2
'''
from pymol import cmd, CmdException
@cmd.extend
def load_nrrd(filename, prefix='channel', channel='', _self=cmd):
'''
DESCRIPTION
Load maps in NRRD file format.
Spec:
http://teem.sourceforge.net/nrrd/format.html
Example files:
http://www.cs.utah.edu/~jmk/simian/download.htm
'''
import numpy
import shlex
from chempy.brick import Brick
handle = open(filename, 'rb')
magic = handle.readline()
assert magic.startswith('NRRD')
header = {}
while True:
line = handle.readline()
if not line.strip():
break
if line.startswith('#'):
continue
key, value = line.split(':')
header[key] = value.strip()
assert header['encoding'] == 'raw'
endian = '>' if header.get('endian') == 'big' else '<'
dtype = {
'signed char': 'i1',
'int8': 'i1',
'int8_t': 'i1',
'uchar': 'u1',
'unsigned char': 'u1',
'uint8': 'u1',
'uint8_t': 'u1',
'float': 'f4',
'double': 'f8',
}[header['type']]
data = numpy.fromfile(handle, endian + dtype)
handle.close()
sizes = [int(i) for i in header['sizes'].split()]
grid = [float(i) for i in header.get('spacings', '0 1 1 1').split()[1:]]
channels = shlex.split(header.get('labels', 'abcdefg'))[0]
assert len(sizes) == 4
sizes = tuple(reversed(sizes))
grid = tuple(reversed(grid))
data = data.reshape(sizes)
for i in range(sizes[-1]):
ch = channels[i]
name = prefix
if not channel:
name += '_' + ch
elif channel != ch:
continue
stack = data[..., i].astype('f4')
# normalize
stack -= stack.mean()
stack /= stack.std()
brick = Brick.from_numpy(stack, grid)
_self.load_brick(brick, name)
if __name__ == '__main__':
# pymol -r load_nrrd.py -- file.nrrd
import sys
load_nrrd(sys.argv[1])