Skip to content

Commit

Permalink
Fixed pole assignment issue
Browse files Browse the repository at this point in the history
Signed-off by: David Rowenhorst <[email protected]>
  • Loading branch information
drowenhorst-nrl committed Nov 4, 2024
1 parent 6d5168c commit 98b80e8
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions pyebsdindex/tripletvote.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,11 +552,11 @@ def bandindex(self, band_norms, band_intensity = None, band_widths=None, verbose
n_band_early = np.int64(self.nband_earlyexit)

# this will check the vote, and return the exact band matching to specific poles of the best fitting solution.
fit, polematch, nMatch, whGood, ij, R, fitb = \
fit, polematch, polevalid, nMatch, whGood, ij, R, fitb = \
self._assign_bands_nb(libPolesCart, libAngTable, libFamIndx, nFam, angTol, n_band_early, bandnorms, bandRank_arg, bandFam)

# check how often the indexed band matched the top voting band family.
acc_correct = np.sum(np.array((polematch >= 0) & (self.completelib['familyid'][polematch] == bandFam), dtype=int),axis=1).astype(np.int32)
acc_correct = np.sum(np.array((polevalid > 0) & (self.completelib['familyid'][polematch] == bandFam), dtype=int),axis=1).astype(np.int32)


# accumulator = accumulator[0, ...]
Expand Down Expand Up @@ -596,13 +596,15 @@ def bandindex(self, band_norms, band_intensity = None, band_widths=None, verbose

if self.high_fidelity == True:

weights = self._calc_quest_weights(libFamID, accumulator, accumulator_nw, polematch, band_intensity, nfit=6)
avequat, fit = self._refine_orientation_quest(libPolesCart, bandnorms, polematch, weights = weights)
weights = self._calc_quest_weights(libFamID, accumulator, accumulator_nw,
polematch, polevalid, band_intensity, nfit=6)
avequat, fit = self._refine_orientation_quest(libPolesCart, bandnorms,
polematch, polevalid, weights = weights)
fit = np.arccos(np.clip(fit, -1.0, 1.0))*RADEG
else:
avequat = rotlib.om2qu(R)

cm2 = self._calc_cm(accumulator, polematch, libFamID)
cm2 = self._calc_cm(accumulator, polematch, polevalid, libFamID)

if verbose > 2:
print('refinement: ', timer() - tic)
Expand Down Expand Up @@ -838,15 +840,17 @@ def _refine_orientation(self, bandnorms, whGood, polematch):

@staticmethod
@numba.jit(nopython=True, cache=True, fastmath=True, parallel=False)
def _calc_quest_weights( libComFamID, accumulator, accumulator_nw, polematch, band_intensity, nfit=6):
def _calc_quest_weights( libComFamID, accumulator, accumulator_nw,
polematch, polevalid, band_intensity, nfit=6):
npats = accumulator.shape[0]
nbands = polematch.shape[-1]
weights = np.zeros((npats, nbands), dtype=np.float32)
#print(band_intensity)
for p in range(npats):
score = np.full((nbands), -1.0, np.float32)
pmatch = np.ravel(polematch[p, :]).astype(np.int64)
whGood = (np.nonzero(pmatch >= 0)[0]).astype(np.int64)
pvalid = np.ravel(polevalid[p, :])
whGood = (np.nonzero(pvalid > 0)[0]).astype(np.int64)

if whGood.size < 2:
continue
Expand All @@ -865,21 +869,27 @@ def _calc_quest_weights( libComFamID, accumulator, accumulator_nw, polematch, ba
#print(srt6)
for s in srt6:
weights[p, s] = band_intensity[p, s]
weights[p, :] /= weights[p,:].max()


#weights[p, :] *= 2.0/weights[p,:].max()
#weights[p, :] = 0.5*(1+np.tanh(8.0 * (weights[p, :] - 1.0)))
weights[p, :] *= 1.0 / weights[p, :].max()
weights[p, :] = np.exp(2*weights[p, :])-1.0

weights[p, :] /= weights[p, :].max()
#print(weights[p,:]/weights[p,:].max())
return weights

def _refine_orientation_quest(self, libpolecart, bandnorms, polesmatch, weights = None):
def _refine_orientation_quest(self, libpolecart, bandnorms,
polesmatch, polesvalid, weights = None):
tic = timer()
npats = bandnorms.shape[0]
nbands = bandnorms.shape[-1]


if weights is None:
weights = np.ones((npats, nbands), dtype=np.float64)
weights *= (polesmatch > 0).astype(np.float32)
weights *= (polesvalid > 0).astype(np.float32)

weightsn = np.asarray(weights, dtype=np.float64)
weightsn /= np.maximum(np.sum(weightsn, axis=1), 1e-12).reshape(-1, 1)
Expand Down Expand Up @@ -1220,7 +1230,8 @@ def _assign_bands_nb(libPolesCart, libAngTable, libFamIndx, nFam, angTol, n_band
whGood_out = np.zeros((npats, nBnds), dtype=np.int64)-1
Rout = np.zeros((npats,3,3), dtype=np.float32)
Rout[:,0,0] = 1.0 ; Rout[:,1,1] = 1.0 ; Rout[:,2,2] = 1.0 ;
polematch_out = np.full((npats, nBnds),-1, dtype=np.int64) - 1
polematch_out = np.full((npats, nBnds),-1000, dtype=np.int64)
polevalid_out = np.full((npats, nBnds),0, dtype=np.uint8)

fitout = np.full(npats, 360.0, dtype=np.float32)
fitbout = np.full((npats, nBnds),360.0, dtype=np.float32)
Expand All @@ -1237,7 +1248,8 @@ def _assign_bands_nb(libPolesCart, libAngTable, libFamIndx, nFam, angTol, n_band
for ii in range(nBnds-1):
for jj in range(ii+1,nBnds):
#print(ii,jj)
polematch = np.zeros((nBnds),dtype=np.int64) - 1
polematch = np.full((nBnds),-1, dtype=np.int64)
polevalid = np.zeros((nBnds), dtype=np.uint8)

bnd1 = bandRank_arg[p, -1 - ii]
bnd2 = bandRank_arg[p, -1 - jj]
Expand Down Expand Up @@ -1325,7 +1337,8 @@ def _assign_bands_nb(libPolesCart, libAngTable, libFamIndx, nFam, angTol, n_band
score = scoreTry
angFit = angfitTry
for j in range(nBnds):
polematch[j] = np.argmax(test[:,j]) * ( 2*np.int32(angfitTry[j] < angTol)-1)
polematch[j] = np.argmax(test[:,j])
polevalid[j] = np.uint8(angfitTry[j] < angTol)
R[0, :,:] = Rtry[i,:,:]


Expand Down Expand Up @@ -1355,6 +1368,7 @@ def _assign_bands_nb(libPolesCart, libAngTable, libFamIndx, nFam, angTol, n_band
nMatch[p] = nGood
whGood_out[p,0:nGood] = whGood[:]
polematch_out[p,...] = polematch[:]
polevalid_out[p, ...] = polevalid[:]
Rout[p,:,:] = R[0,:,:]
ij[p,:] = np.asarray((ii,jj,bnd1,bnd2), dtype=np.int64)
break
Expand All @@ -1368,6 +1382,7 @@ def _assign_bands_nb(libPolesCart, libAngTable, libFamIndx, nFam, angTol, n_band
nMatch[p] = nGood
whGood_out[p, 0:nGood] = whGood[:]
polematch_out[p, ...] = polematch[:]
polevalid_out[p, ...] = polevalid[:]
Rout[p, :, :] = R[0, :, :]
ij[p, :] = np.asarray((ii, jj, bnd1, bnd2), dtype=np.int64)

Expand All @@ -1380,6 +1395,7 @@ def _assign_bands_nb(libPolesCart, libAngTable, libFamIndx, nFam, angTol, n_band
nMatch[p] = nGood
whGood_out[p, 0:nGood] = whGood[:]
polematch_out[p, ...] = polematch[:]
polevalid_out[p, ...] = polevalid[:]
Rout[p, :, :] = R[0, :, :]
ij[p, :] = np.asarray((ii, jj, bnd1, bnd2), dtype=np.int64)

Expand All @@ -1394,7 +1410,7 @@ def _assign_bands_nb(libPolesCart, libAngTable, libFamIndx, nFam, angTol, n_band
#print(testout.T)
#print(pflt[polematch_out,:])
#print(dave)
return fitout, polematch_out,nMatch, whGood_out, ij, Rout, fitbout
return fitout, polematch_out,polevalid_out, nMatch, whGood_out, ij, Rout, fitbout

@staticmethod
@numba.jit(nopython=True, cache=True, fastmath=True,parallel=False)
Expand Down Expand Up @@ -1755,14 +1771,14 @@ def _fitcheck(self, q, bandnorms, cartxstalpoles):

@staticmethod
@numba.jit(nopython=True, cache=True, fastmath=True, parallel=False)
def _calc_cm(accumulator, polematch, libFamIndx):
def _calc_cm(accumulator, polematch, polevalid, libFamIndx):

npats = accumulator.shape[0]
cm2 = -1 * np.ones(npats, dtype=np.float32)

for p in range(npats):

whmatch = (np.nonzero(polematch[p, :] >= 0)[0]).astype(np.int64)
whmatch = (np.nonzero(polevalid[p, :] > 0)[0]).astype(np.int64)
if whmatch.size < 2:
continue
# cm = np.mean(band_cm[whmatch])
Expand Down

0 comments on commit 98b80e8

Please sign in to comment.