Skip to content

Commit 7019dce

Browse files
committed
Fixes to config file use, updates to logging, few bug fixes
1 parent 100d52b commit 7019dce

File tree

6 files changed

+160
-145
lines changed

6 files changed

+160
-145
lines changed

bin/halostack_cli.py

Lines changed: 42 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
'handlers': {
4040
'console': {
4141
'class': 'logging.StreamHandler',
42-
'level': 'DEBUG',
42+
'level': 'INFO',
4343
'formatter': 'normal'
4444
},
4545
'file': {
@@ -78,26 +78,6 @@
7878

7979
LOGGER = logging.getLogger("halostack_cli")
8080

81-
def logging_setup(args):
82-
'''Setup logging.
83-
'''
84-
85-
# global LOGGER
86-
87-
# Clear earlier handlers
88-
# LOGGER.handlers = []
89-
90-
if args is None:
91-
loglevel = logging.DEBUG #INFO
92-
else:
93-
loglevel = args.get("loglevel", "DEBUG").upper()
94-
try:
95-
loglevel = getattr(logging, loglevel)
96-
except AttributeError:
97-
loglevel = logging.INFO
98-
99-
LOGGER.setLevel(loglevel) # = logging.getLogger("halostack_cli")
100-
10181

10282
def halostack_cli(args):
10383
'''Commandline interface.'''
@@ -113,71 +93,69 @@ def halostack_cli(args):
11393
for stack in args['stacks']:
11494
stacks.append(Stack(stack, len(images), nprocs=args['nprocs']))
11595

116-
base_img = Image(fname=images[0], enhancements=args['enhance_images'],
117-
nprocs=args['nprocs'])
118-
LOGGER.info("Using %s as base image.", base_img.fname)
96+
base_img = Image(fname=images[0], nprocs=args['nprocs'])
97+
LOGGER.debug("Using %s as base image.", base_img.fname)
11998
images.remove(images[0])
12099

121100
if not args['no_alignment'] and len(images) > 1:
122101
view_img = base_img.luminance()
123102
if isinstance(args['view_gamma'], float):
124103
view_img.enhance({'gamma': args['view_gamma']})
125-
print "Click tight area (two opposite corners) for "\
126-
"reference location."
104+
print "\nClick tight area (two opposite corners) for "\
105+
"reference location.\n"
127106
args['focus_reference'] = get_two_points(view_img)
128-
LOGGER.info("Reference area: (%d, %d) with radius %d.",
129-
args['focus_reference'][0],
130-
args['focus_reference'][1],
131-
args['focus_reference'][2])
107+
LOGGER.debug("Reference area: (%d, %d) with radius %d.",
108+
args['focus_reference'][0],
109+
args['focus_reference'][1],
110+
args['focus_reference'][2])
132111
print "Click two corner points for the area where alignment "\
133-
"reference will be in every image."
112+
"reference will be in every image.\n"
134113
args['focus_area'] = get_two_points(view_img)
135114

136-
LOGGER.info("User-selected search area: (%d, %d) with radius %d.",
115+
LOGGER.debug("User-selected search area: (%d, %d) with radius %d.",
137116
args['focus_area'][0],
138117
args['focus_area'][1],
139118
args['focus_area'][2])
140119
del view_img
141120

142-
for stack in stacks:
143-
LOGGER.debug("Adding %s to %s stack", base_img.fname, stack.mode)
144-
stack.add_image(base_img)
145-
146121
if not args['no_alignment'] and len(images) > 1:
147122
LOGGER.debug("Initializing alignment.")
148-
aligner = Align(base_img, ref_loc=args['focus_reference'],
149-
srch_area=args['focus_area'],
123+
aligner = Align(base_img,
150124
cor_th=args['correlation_threshold'],
151125
nprocs=args['nprocs'])
152-
LOGGER.info("Alignment initialized.")
126+
aligner.set_reference(args['focus_reference'])
127+
aligner.set_search_area(args['focus_area'])
128+
LOGGER.debug("Alignment initialized.")
129+
130+
if len(args['enhance_images']) > 0:
131+
LOGGER.info("Preprocessing image.")
132+
base_img.enhance(args['enhance_images'])
133+
for stack in stacks:
134+
stack.add_image(base_img)
153135

154136
# memory management
155137
del base_img
156138

157139
for img in images:
158140
# Read image
159-
LOGGER.info("Reading %s.", img)
160-
img = Image(fname=img, enhancements=args['enhance_images'],
161-
nprocs=args['nprocs'])
141+
img = Image(fname=img, nprocs=args['nprocs'])
162142

163143
if not args['no_alignment'] and len(images) > 1:
164144
# align image
165-
LOGGER.info("Aligning image.")
166145
img = aligner.align(img)
167146

168147
if img is None:
169-
LOGGER.warning("Threshold was below threshold, skipping image.")
148+
LOGGER.warning("Skipping image.")
170149
continue
171150

151+
if len(args['enhance_images']) > 0:
152+
LOGGER.info("Preprocessing image.")
153+
img.enhance(args['enhance_images'])
172154
for stack in stacks:
173-
LOGGER.info("Adding image to %s stack.", stack.mode)
174155
stack.add_image(img)
175156

176157
for i in range(len(stacks)):
177-
LOGGER.info("Calculating %s stack", stacks[i].mode)
178158
img = stacks[i].calculate()
179-
LOGGER.info("Saving %s stack to %s.", stacks[i].mode,
180-
args['stack_fnames'][i])
181159
img.save(args['stack_fnames'][i],
182160
enhancements=args['enhance_stacks'])
183161

@@ -198,14 +176,14 @@ def main():
198176
help="Output filename of the median stack")
199177
parser.add_argument("-t", "--correlation-threshold",
200178
dest="correlation_threshold",
201-
default=0.7, metavar="NUM", type=float,
179+
default=None, metavar="NUM", type=float,
202180
help="Minimum required correlation [0.7]")
203181
parser.add_argument("-s", "--save-images", dest="save_postfix",
204182
default=None, metavar="STR",
205183
help="Save aligned images as PNG with the given " \
206184
"filename postfix")
207185
parser.add_argument("-n", "--no-alignment", dest="no_alignment",
208-
default=False, action="store_true",
186+
default=None, action="store_true",
209187
help="Stack without alignment")
210188
parser.add_argument("-e", "--enhance-images", dest="enhance_images",
211189
default=[], type=str, action="append",
@@ -221,10 +199,8 @@ def main():
221199
parser.add_argument("-c", "--config_item", dest="config_item",
222200
metavar="STR", default=None,
223201
help="Config item to select parameters")
224-
parser.add_argument("-l", "--loglevel", dest="loglevel", metavar="LOGLEVEL",
225-
type=str, default="INFO", help="Set level of shown messages")
226202
parser.add_argument("-p", "--nprocs", dest="nprocs", metavar="INT",
227-
type=int, default=1,
203+
type=int, default=None,
228204
help="Number of parallel processes")
229205
parser.add_argument('fname_in', metavar="FILE", type=str, nargs='*',
230206
help='List of files')
@@ -236,14 +212,24 @@ def main():
236212
if args["config_item"] is not None:
237213
args = read_config(args)
238214

239-
# Re-adjust logging
240-
logging_setup(args)
215+
# Check which adjustments are made for each image, and then for
216+
# the resulting stacks
217+
args['enhance_images'] = parse_enhancements(args['enhance_images'])
218+
args['enhance_stacks'] = parse_enhancements(args['enhance_stacks'])
241219

242220
# Workaround for windows for getting all the filenames with *.jpg syntax
243221
# that is expanded by the shell in linux
244222
args['fname_in'] = get_filenames(args['fname_in'])
245223
LOGGER.debug(args['fname_in'])
246224

225+
# Check validity
226+
if not isinstance(args['nprocs'], int):
227+
args['nprocs'] = 1
228+
if not isinstance(args['correlation_threshold'], float):
229+
args['correlation_threshold'] = 0.7
230+
if not isinstance(args['no_alignment'], bool):
231+
args['no_alignment'] = False
232+
247233
# Check which stacks will be made
248234
stacks = []
249235
stack_fnames = []
@@ -266,12 +252,8 @@ def main():
266252
args['stacks'] = stacks
267253
args['stack_fnames'] = stack_fnames
268254

269-
# Check which adjustments are made for each image, and then for
270-
# the resulting stacks
271-
args['enhance_images'] = parse_enhancements(args['enhance_images'])
272-
args['enhance_stacks'] = parse_enhancements(args['enhance_stacks'])
273-
274255
LOGGER.info("Starting stacking")
256+
275257
halostack_cli(args)
276258

277259

0 commit comments

Comments
 (0)