Skip to content

Commit

Permalink
Merge pull request #328 from HEXRD/angular-pixel-size-update
Browse files Browse the repository at this point in the history
swith to using pixel angular gradient arrays
  • Loading branch information
joelvbernier authored Sep 30, 2021
2 parents 29b9ec9 + 2357759 commit 1155462
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions hexrd/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,13 @@ def _solid_angle_of_triangle(vtx_list):

return 2.*np.arctan2(scalar_triple_product, denominator)


def _fix_branch_cut_in_gradients(pgarray):
return np.min(
np.abs(np.stack([pgarray - np.pi, pgarray, pgarray + np.pi])),
axis=0
)

# =============================================================================
# CLASSES
# =============================================================================
Expand Down Expand Up @@ -2402,30 +2409,17 @@ def pixel_tth_gradient(self, origin=ct.zeros_3):
return np.linalg.norm(np.stack(np.gradient(ptth)), axis=0)

def pixel_eta_gradient(self, origin=ct.zeros_3):
period = np.r_[0., 2*np.pi]
assert len(origin) == 3, "origin must have 3 elemnts"
_, peta = self.pixel_angles(origin=origin)

# !!! handle cyclic nature of eta
rowmap = np.empty_like(peta)
for i in range(rowmap.shape[0]):
rowmap[i, :] = mapAngle(
peta[i, :], peta[i, 0] + period
)

colmap = np.empty_like(peta)
for i in range(colmap.shape[1]):
colmap[:, i] = mapAngle(
peta[:, i], peta[0, i] + period
)
peta_grad_row = np.gradient(peta, axis=0)
peta_grad_col = np.gradient(peta, axis=1)

peta_grad_row = np.gradient(rowmap)
peta_grad_col = np.gradient(colmap)
# !!!: fix branch cut
peta_grad_row = _fix_branch_cut_in_gradients(peta_grad_row)
peta_grad_col = _fix_branch_cut_in_gradients(peta_grad_col)

return np.linalg.norm(
np.stack([peta_grad_col[0], peta_grad_row[1]]),
axis=0
)
return np.linalg.norm(np.stack([peta_grad_col, peta_grad_row]), axis=0)

def cartToPixel(self, xy_det, pixels=False):
"""
Expand Down Expand Up @@ -2467,7 +2461,9 @@ def pixelToCart(self, ij_det):

def angularPixelSize(self, xy, rMat_s=None, tVec_s=None, tVec_c=None):
"""
Wraps xrdutil.angularPixelSize
Notes
-----
!!! assumes xy are in raw (distorted) frame, if applicable
"""
# munge kwargs
if rMat_s is None:
Expand All @@ -2476,7 +2472,19 @@ def angularPixelSize(self, xy, rMat_s=None, tVec_s=None, tVec_c=None):
tVec_s = ct.zeros_3x1
if tVec_c is None:
tVec_c = ct.zeros_3x1
origin = np.dot(rMat_s, tVec_c).flatten() + tVec_s.flatten()

# FIXME: perhaps not necessary, but safe...
xy = np.atleast_2d(xy)

# get pixel indices
i_crds = cellIndices(self.row_edge_vec, xy[:, 1])
j_crds = cellIndices(self.col_edge_vec, xy[:, 0])

ptth_grad = self.pixel_tth_gradient(origin=origin)[i_crds, j_crds]
peta_grad = self.pixel_eta_gradient(origin=origin)[i_crds, j_crds]

'''
# call function
ang_ps = xrdutil.angularPixelSize(
xy, (self.pixel_size_row, self.pixel_size_col),
Expand All @@ -2485,6 +2493,8 @@ def angularPixelSize(self, xy, rMat_s=None, tVec_s=None, tVec_c=None):
distortion=self.distortion,
beamVec=self.bvec, etaVec=self.evec)
return ang_ps
'''
return np.vstack([ptth_grad, peta_grad]).T

def clip_to_panel(self, xy, buffer_edges=True):
"""
Expand Down Expand Up @@ -3817,7 +3827,7 @@ def _lorentz_polarization_factor(tth, eta, f_hor, f_vert, unpolarized):
L = 1./(4.0*cth*sth2)
if unpolarized:
P = (1. + ctth2)/2.
else:
else:
P = f_hor*(seta2 + ceta2*ctth2) + f_vert*(ceta2 + seta2*ctth2)

return L*P
Expand Down

0 comments on commit 1155462

Please sign in to comment.