Skip to content

Commit f18192f

Browse files
Merge pull request #852 from donald-e-boyce/fix-undefined-panel
fixes undefined panel issue in fit-grains
2 parents 8871889 + e8ec186 commit f18192f

File tree

1 file changed

+109
-85
lines changed

1 file changed

+109
-85
lines changed

hexrd/hedm/fitgrains.py

Lines changed: 109 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -125,91 +125,9 @@ def fit_grain_FF_reduced(grain_id):
125125
interp='nearest',
126126
)
127127

128-
# ======= DETERMINE VALID REFLECTIONS =======
129-
130-
culled_results = dict.fromkeys(results)
131-
num_refl_tot = 0
132-
num_refl_valid = 0
133-
for det_key in culled_results:
134-
panel = instrument.detectors[det_key]
135-
136-
'''
137-
grab panel results:
138-
peak_id
139-
hkl_id
140-
hkl
141-
sum_int
142-
max_int,
143-
pred_angs,
144-
meas_angs,
145-
meas_xy
146-
'''
147-
presults = results[det_key]
148-
nrefl = len(presults)
149-
150-
# make data arrays
151-
refl_ids = np.empty(nrefl)
152-
max_int = np.empty(nrefl)
153-
for i, spot_data in enumerate(presults):
154-
refl_ids[i] = spot_data[0]
155-
max_int[i] = spot_data[4]
156-
157-
valid_refl_ids = refl_ids >= 0
158-
159-
# find unsaturated spots on this panel
160-
unsat_spots = np.ones(len(valid_refl_ids), dtype=bool)
161-
if panel.saturation_level is not None:
162-
unsat_spots[valid_refl_ids] = (
163-
max_int[valid_refl_ids] < panel.saturation_level
164-
)
165-
166-
idx = np.logical_and(valid_refl_ids, unsat_spots)
167-
168-
# if an overlap table has been written, load it and use it
169-
overlaps = np.zeros_like(idx, dtype=bool)
170-
try:
171-
ot = np.load(
172-
os.path.join(
173-
analysis_dirname,
174-
os.path.join(det_key, OVERLAP_TABLE_FILE),
175-
)
176-
)
177-
for key in ot.keys():
178-
for this_table in ot[key]:
179-
these_overlaps = np.where(
180-
this_table[:, 0] == grain_id
181-
)[0]
182-
if len(these_overlaps) > 0:
183-
mark_these = np.array(
184-
this_table[these_overlaps, 1], dtype=int
185-
)
186-
otidx = [
187-
np.where(refl_ids == mt)[0]
188-
for mt in mark_these
189-
]
190-
overlaps[otidx] = True
191-
idx = np.logical_and(idx, ~overlaps)
192-
# logger.info("found overlap table for '%s'", det_key)
193-
except (IOError, IndexError):
194-
# logger.info("no overlap table found for '%s'", det_key)
195-
pass
196-
197-
# attach to proper dict entry
198-
# FIXME: want to avoid looping again here
199-
culled_results[det_key] = [presults[i] for i in np.where(idx)[0]]
200-
num_refl_tot += len(valid_refl_ids)
201-
num_refl_valid += sum(valid_refl_ids)
202-
# now we have culled data
203-
204-
# CAVEAT: completeness from pullspots only; incl saturated and overlaps
205-
# <JVB 2015-12-15>
206-
try:
207-
completeness = num_refl_valid / float(num_refl_tot)
208-
except ZeroDivisionError:
209-
raise RuntimeError(
210-
"simulated number of relfections is 0; "
211-
+ "check instrument config or grain parameters"
212-
)
128+
culled_results, num_refl_valid, completeness = determine_valid_reflections(
129+
results, instrument, analysis_dirname
130+
)
213131

214132
# ======= DO LEASTSQ FIT =======
215133

@@ -263,6 +181,7 @@ def fit_grain_FF_reduced(grain_id):
263181
culled_results_r = dict.fromkeys(culled_results)
264182
num_refl_valid = 0
265183
for det_key in culled_results_r:
184+
panel = instrument.detectors[det_key]
266185
presults = culled_results[det_key]
267186

268187
if not presults:
@@ -331,6 +250,111 @@ def fit_grain_FF_reduced(grain_id):
331250
return grain_id, completeness, chisq, grain_params
332251

333252

253+
def determine_valid_reflections(results, instrument, analysis_dirname):
254+
"""Process results from `pull_spots()`
255+
256+
PARAMETERS
257+
----------
258+
results: dict
259+
output from `pull_spots()`
260+
instrument:
261+
the HEDM instrument
262+
analysis_dirname: str or Path
263+
the analysis output directory
264+
265+
RETURNS
266+
-------
267+
culled_results: dict
268+
spot information for valid reflections
269+
"""
270+
271+
culled_results = dict.fromkeys(results)
272+
num_refl_tot = 0
273+
num_refl_valid = 0
274+
for det_key in culled_results:
275+
panel = instrument.detectors[det_key]
276+
277+
'''
278+
grab panel results:
279+
peak_id
280+
hkl_id
281+
hkl
282+
sum_int
283+
max_int,
284+
pred_angs,
285+
meas_angs,
286+
meas_xy
287+
'''
288+
presults = results[det_key]
289+
nrefl = len(presults)
290+
291+
# make data arrays
292+
refl_ids = np.empty(nrefl)
293+
max_int = np.empty(nrefl)
294+
for i, spot_data in enumerate(presults):
295+
refl_ids[i] = spot_data[0]
296+
max_int[i] = spot_data[4]
297+
298+
valid_refl_ids = refl_ids >= 0
299+
300+
# find unsaturated spots on this panel
301+
unsat_spots = np.ones(len(valid_refl_ids), dtype=bool)
302+
if panel.saturation_level is not None:
303+
unsat_spots[valid_refl_ids] = (
304+
max_int[valid_refl_ids] < panel.saturation_level
305+
)
306+
307+
idx = np.logical_and(valid_refl_ids, unsat_spots)
308+
309+
# if an overlap table has been written, load it and use it
310+
overlaps = np.zeros_like(idx, dtype=bool)
311+
try:
312+
ot = np.load(
313+
os.path.join(
314+
analysis_dirname,
315+
os.path.join(det_key, OVERLAP_TABLE_FILE),
316+
)
317+
)
318+
for key in ot.keys():
319+
for this_table in ot[key]:
320+
these_overlaps = np.where(
321+
this_table[:, 0] == grain_id
322+
)[0]
323+
if len(these_overlaps) > 0:
324+
mark_these = np.array(
325+
this_table[these_overlaps, 1], dtype=int
326+
)
327+
otidx = [
328+
np.where(refl_ids == mt)[0]
329+
for mt in mark_these
330+
]
331+
overlaps[otidx] = True
332+
idx = np.logical_and(idx, ~overlaps)
333+
# logger.info("found overlap table for '%s'", det_key)
334+
except (IOError, IndexError):
335+
# logger.info("no overlap table found for '%s'", det_key)
336+
pass
337+
338+
# attach to proper dict entry
339+
# FIXME: want to avoid looping again here
340+
culled_results[det_key] = [presults[i] for i in np.where(idx)[0]]
341+
num_refl_tot += len(valid_refl_ids)
342+
num_refl_valid += sum(valid_refl_ids)
343+
# now we have culled data
344+
345+
# CAVEAT: completeness from pullspots only; incl saturated and overlaps
346+
# <JVB 2015-12-15>
347+
try:
348+
completeness = num_refl_valid / float(num_refl_tot)
349+
except ZeroDivisionError:
350+
raise RuntimeError(
351+
"simulated number of relfections is 0; "
352+
+ "check instrument config or grain parameters"
353+
)
354+
355+
return culled_results, num_refl_valid, completeness
356+
357+
334358
def fit_grains(
335359
cfg,
336360
grains_table,

0 commit comments

Comments
 (0)