Skip to content

Commit 3d4801c

Browse files
committed
fit-grains CLI implementation
1 parent 06ae56e commit 3d4801c

File tree

6 files changed

+466
-500
lines changed

6 files changed

+466
-500
lines changed

hexrd/cli/findorientations.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import print_function, division, absolute_import
22

33

4-
descr = 'Process diffraction data to find grain orientations'
4+
descr = 'Process rotation image series to find grain orientations'
55
example = """
66
examples:
77
hexrd find-orientations configuration.yml
@@ -79,29 +79,24 @@ def execute(args, parser):
7979
# load the configuration settings
8080
cfg = config.open(args.yml)[0]
8181

82-
# ...make this an attribute in cfg?
83-
analysis_id = '%s_%s' %(
84-
cfg.analysis_name.strip().replace(' ', '-'),
85-
cfg.material.active.strip().replace(' ', '-'),
86-
)
87-
8882
# prepare the analysis directory
8983
quats_f = os.path.join(
9084
cfg.working_dir,
91-
'accepted_orientations_%s.dat' %analysis_id
85+
'accepted_orientations_%s.dat' % cfg.analysis_id
9286
)
9387
if os.path.exists(quats_f) and not (args.force or args.clean):
9488
logger.error(
95-
'%s already exists. Change yml file or specify "force" or "clean"', quats_f
96-
)
89+
'%s already exists. Change yml file or specify "force" or "clean"',
90+
quats_f
91+
)
9792
sys.exit()
9893
if not os.path.exists(cfg.working_dir):
9994
os.makedirs(cfg.working_dir)
10095

10196
# configure logging to file
10297
logfile = os.path.join(
10398
cfg.working_dir,
104-
'find-orientations_%s.log' %analysis_id
99+
'find-orientations_%s.log' % cfg.analysis_id
105100
)
106101
fh = logging.FileHandler(logfile, mode='w')
107102
fh.setLevel(log_level)
@@ -120,7 +115,12 @@ def execute(args, parser):
120115
pr.enable()
121116

122117
# process the data
123-
find_orientations(cfg, hkls=args.hkls, clean=args.clean, profile=args.profile)
118+
find_orientations(
119+
cfg,
120+
hkls=args.hkls,
121+
clean=args.clean,
122+
profile=args.profile
123+
)
124124

125125
if args.profile:
126126
pr.disable()

hexrd/cli/fitgrains.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ def configure_parser(sub_parsers):
2424
)
2525
p.add_argument(
2626
'-c', '--clean', action='store_true',
27-
help='overwrites existing analysis, including frame cache'
27+
help='overwrites existing analysis, uses initial orientations'
2828
)
2929
p.add_argument(
3030
'-f', '--force', action='store_true',
31-
help='overwrites existing analysis, exlcuding frame cache'
31+
help='overwrites existing analysis'
3232
)
3333
p.add_argument(
3434
'-p', '--profile', action='store_true',
@@ -62,17 +62,11 @@ def execute(args, parser):
6262
cf = logging.Formatter('%(asctime)s - %(message)s', '%y-%m-%d %H:%M:%S')
6363
ch.setFormatter(cf)
6464
logger.addHandler(ch)
65-
66-
# ...make this an attribute in cfg?
67-
analysis_id = '%s_%s' %(
68-
cfgs[0].analysis_name.strip().replace(' ', '-'),
69-
cfgs[0].material.active.strip().replace(' ', '-'),
70-
)
7165

7266
# if find-orientations has not already been run, do so:
7367
quats_f = os.path.join(
7468
cfgs[0].working_dir,
75-
'accepted_orientations_%s.dat' %analysis_id
69+
'accepted_orientations_%s.dat' % cfgs[0].analysis_id
7670
)
7771
if not os.path.exists(quats_f):
7872
logger.info("Missing %s, running find-orientations", quats_f)

hexrd/config/fitgrains.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def fit_only(self):
116116
def tth_max(self):
117117
key = 'fit_grains:tth_max'
118118
temp = self._cfg.get(key, True)
119-
if temp in (True, False):
119+
if isinstance(temp, bool):
120120
return temp
121121
if isinstance(temp, (int, float)):
122122
if temp > 0:

hexrd/findorientations.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import numpy as np
99
# np.seterr(over='ignore', invalid='ignore')
1010

11+
import tqdm
12+
1113
import scipy.cluster as cluster
1214
from scipy import ndimage
1315
from skimage.feature import blob_dog, blob_log
@@ -62,7 +64,7 @@ def generate_orientation_fibers(cfg, eta_ome):
6264
# default values for each case? They must be specified as of now.
6365
method = next(method_dict.iterkeys())
6466
method_kwargs = method_dict[method]
65-
logger.info('using "%s" method for fiber generation'
67+
logger.info('\tusing "%s" method for fiber generation'
6668
% method)
6769

6870
# seed_hkl_ids must be consistent with this...
@@ -163,24 +165,44 @@ def generate_orientation_fibers(cfg, eta_ome):
163165
if ncpus > 1:
164166
# multiple process version
165167
# ???: Need a chunksize in map?
168+
chunksize = max(1, len(input_p)//(10*ncpus))
166169
pool = mp.Pool(ncpus, discretefiber_init, (params, ))
167-
qfib = pool.map(discretefiber_reduced, input_p)
170+
qfib = pool.map(
171+
discretefiber_reduced, input_p,
172+
chunksize=chunksize
173+
)
174+
'''
175+
# This is an experiment...
176+
ntotal= 10*ncpus + np.remainder(len(input_p), 10*ncpus) > 0
177+
for _ in tqdm.tqdm(
178+
pool.imap_unordered(
179+
discretefiber_reduced, input_p, chunksize=chunksize
180+
), total=ntotal
181+
):
182+
pass
183+
print(_.shape)
184+
'''
168185
pool.close()
186+
pool.join()
169187
else:
170188
# single process version.
171-
global paramMP
172189
discretefiber_init(params) # sets paramMP
173190
qfib = map(discretefiber_reduced, input_p)
174-
paramMP = None # clear paramMP
191+
discretefiber_cleanup()
175192
elapsed = (timeit.default_timer() - start)
176-
logger.info("fiber generation took %.3f seconds", elapsed)
193+
logger.info("\tfiber generation took %.3f seconds", elapsed)
177194
return np.hstack(qfib)
178195

179196

180197
def discretefiber_init(params):
181198
global paramMP
182199
paramMP = params
183200

201+
202+
def discretefiber_cleanup():
203+
global paramMP
204+
del paramMP
205+
184206

185207
def discretefiber_reduced(params_in):
186208
"""
@@ -587,6 +609,7 @@ def find_orientations(cfg,
587609
# handle search space
588610
if cfg.find_orientations.use_quaternion_grid is None:
589611
# doing seeded search
612+
logger.info("Will perform seeded search")
590613
logger.info(
591614
"\tgenerating search quaternion list using %d processes",
592615
ncpus
@@ -653,7 +676,7 @@ def find_orientations(cfg,
653676
# do map-based indexing
654677
start = timeit.default_timer()
655678

656-
logger.info(" will test %d quaternions using %d processes",
679+
logger.info("will test %d quaternions using %d processes",
657680
qfib.shape[1], ncpus)
658681

659682
completeness = indexer.paintGrid(
@@ -740,8 +763,6 @@ def find_orientations(cfg,
740763

741764
logger.info("\tmean reflections per grain: %d", mean_rpg)
742765
logger.info("\tneighborhood size: %d", min_samples)
743-
logger.info("\tFeeding %d orientations above %.1f%% to clustering",
744-
sum(completeness > compl_thresh), compl_thresh)
745766

746767
qbar, cl = run_cluster(
747768
completeness, qfib, plane_data.getQSym(), cfg,

0 commit comments

Comments
 (0)