Skip to content

Commit 02fb3ba

Browse files
committed
return list
1 parent 26c0cf7 commit 02fb3ba

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

pyxtal/XRD_indexer.py

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ def get_seeds(spg, hkls, two_thetas):
381381

382382

383383
def get_cell_from_multi_hkls(spg, hkls, two_thetas, long_thetas=None, wave_length=1.54184,
384-
tolerance=0.05, use_seed=True, min_matched_peaks=2):
384+
tolerance=0.05, use_seed=True, min_score=0.999):
385385
"""
386386
Estimate the cell parameters from multiple (hkl, two_theta) inputs.
387387
The idea is to use the Bragg's law and the lattice spacing formula to estimate the lattice parameters.
@@ -395,10 +395,10 @@ def get_cell_from_multi_hkls(spg, hkls, two_thetas, long_thetas=None, wave_lengt
395395
wave_length: X-ray wavelength, default is Cu K-alpha
396396
tolerance: tolerance for matching 2theta values, default is 0.05 degrees
397397
use_seed: whether to use seed hkls for initial cell estimation
398-
min_matched_peaks: minimum number of matched peaks to consider a valid solution
398+
min_score: threshold score for consideration
399399
400400
Returns:
401-
cells: estimated lattice parameter
401+
cells: list of solutions
402402
"""
403403
if long_thetas is None: long_thetas = two_thetas
404404
#test_hkls_array = np.array(generate_possible_hkls(max_h=max_h))
@@ -411,16 +411,14 @@ def get_cell_from_multi_hkls(spg, hkls, two_thetas, long_thetas=None, wave_lengt
411411
else:
412412
level = 0 # triclinic
413413

414-
best_solution = None
415-
best_score = 0
416-
417414
if use_seed:
418415
seed_hkls, seed_thetas = get_seeds(spg, hkls, two_thetas)#; print(seed_hkls, seed_thetas)
419416
cells = get_cell_params(spg, seed_hkls, seed_thetas, wave_length)#; print(cells)
420417
else:
421418
cells = get_cell_params(spg, hkls, two_thetas, wave_length)#; print(cells)
419+
422420
cells = np.array(cells)
423-
if len(cells) == 0: return None
421+
if len(cells) == 0: return []
424422
cells = np.unique(cells, axis=0)#; print(cells) # remove duplicates
425423

426424
# get the maximum h from assuming the cell[-1] is (h00)
@@ -434,6 +432,7 @@ def get_cell_from_multi_hkls(spg, hkls, two_thetas, long_thetas=None, wave_lengt
434432
k_maxs = np.array(long_thetas[-1] / theta_010s, dtype=int)
435433
l_maxs = np.array(long_thetas[-1] / theta_001s, dtype=int)
436434

435+
solutions = []
437436
for i, cell in enumerate(cells):
438437
test_hkls = np.array(generate_possible_hkls(h_max=h_maxs[i],
439438
k_max=k_maxs[i],
@@ -469,23 +468,20 @@ def get_cell_from_multi_hkls(spg, hkls, two_thetas, long_thetas=None, wave_lengt
469468

470469
# Score this solution
471470
n_matched = len(matched_peaks)
472-
if n_matched >= min_matched_peaks:
473-
coverage = n_matched / len(long_thetas)
474-
avg_error = np.mean([match[2] for match in matched_peaks])
475-
consistency_score = 1.0 / (1.0 + avg_error) # lower error = higher score
476-
total_score = coverage * consistency_score
471+
coverage = n_matched / len(long_thetas)
472+
avg_error = np.mean([match[-1] for match in matched_peaks])
473+
consistency_score = 1.0 / (1.0 + avg_error) # lower error = higher score
474+
score = coverage * consistency_score
477475

478-
if total_score > best_score:
479-
best_score = total_score
480-
best_solution = {
481-
'cell': cell,
482-
'n_matched': n_matched,
483-
'n_total': len(long_thetas),
484-
'score': total_score,
485-
#'avg_error': avg_error,
486-
}
476+
if score > min_score:
477+
solutions.append({
478+
'cell': cell,
479+
'n_matched': n_matched,
480+
'n_total': len(long_thetas),
481+
'score': score,
482+
})
487483

488-
return best_solution
484+
return solutions
489485

490486

491487
if __name__ == "__main__":
@@ -522,17 +518,16 @@ def get_cell_from_multi_hkls(spg, hkls, two_thetas, long_thetas=None, wave_lengt
522518
thetas.extend(available_peaks[list(peak_combo)])
523519
hkls_t = np.tile(guess, (int(len(thetas)/len(guess)), 1))
524520

525-
result = get_cell_from_multi_hkls(spg, hkls_t, thetas, long_thetas, use_seed=False)
526-
if result is not None and result['score'] > 0.98:
521+
solutions = get_cell_from_multi_hkls(spg, hkls_t, thetas, long_thetas, use_seed=False)
522+
for sol in solutions:
527523
d2 = np.sum(guess**2)
528-
print("Guess:", guess.flatten(), d2, "->", result['cell'], thetas[:len(guess)], "Score:", result['score'])
529-
if result['score'] > 0.992:
530-
cell1 = np.sort(np.array(result['cell']))
531-
diff = np.sum((cell1 - cell_ref)**2)
532-
if diff < 0.1:
533-
print("High score, exiting early.")
534-
found = True
535-
break
524+
print("Guess:", guess.flatten(), d2, "->", sol['cell'], thetas[:len(guess)], "Score:", sol['score'])
525+
cell1 = np.sort(np.array(sol['cell']))
526+
diff = np.sum((cell1 - cell_ref)**2)
527+
if diff < 0.1:
528+
print("High score, exiting early.")
529+
found = True
530+
break
536531
if found:
537532
break
538533

0 commit comments

Comments
 (0)