forked from asarnow/pyem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emcalc.py
executable file
·66 lines (58 loc) · 2.46 KB
/
emcalc.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
#!/usr/bin/env python
# Copyright (C) 2020 Daniel Asarnow
# University of California, San Francisco
#
# Command line calculator for EM data using numexpr.
# See help text and README file for more information.
#
# 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/>.
import logging
import numexpr as ne
import numpy as np
import sys
from pyem.mrc import read
from pyem.mrc import write
from pyem import vop
from string import ascii_lowercase
def main(args):
log = logging.getLogger(__name__)
hdlr = logging.StreamHandler(sys.stdout)
log.addHandler(hdlr)
log.setLevel(logging.getLevelName(args.loglevel.upper()))
data = {}
hdr = {}
for i,inp in enumerate(args.input[1:]):
d, h = read(inp, inc_header=True)
if args.normalize:
d = vop.normalize(d)
data[ascii_lowercase[i]] = d
hdr[ascii_lowercase[i]] = h
if args.eval:
final = eval(args.input[0], globals(), data)
else:
final = ne.evaluate(args.input[0], local_dict=data)
if args.apix is None:
args.apix = hdr[ascii_lowercase[0]]['xlen'] / hdr[ascii_lowercase[0]]['nx']
write(args.output, final.astype(np.single), psz=args.apix)
return 0
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Use equals sign when passing arguments with negative numbers.")
parser.add_argument("input", help="Input volume (MRC file)", nargs="*")
parser.add_argument("output", help="Output volume (MRC file)")
parser.add_argument("--apix", help="Output pixel size")
parser.add_argument("--normalize", "-n", help="Normalize all input maps", action="store_true")
parser.add_argument("--eval", "-e", help="Use eval builtin instead of numexpr.evaluate", action="store_true")
parser.add_argument("--loglevel", "-l", type=str, default="WARNING", help="Logging level and debug output")
sys.exit(main(parser.parse_args()))