Skip to content

Commit

Permalink
Merge pull request #669 from Verdant-Evolution/remove-numba-checks
Browse files Browse the repository at this point in the history
Remove numba checks
  • Loading branch information
psavery authored Jul 5, 2024
2 parents b32cf68 + 2323a5f commit b7a88d8
Show file tree
Hide file tree
Showing 22 changed files with 1,748 additions and 1,848 deletions.
15 changes: 1 addition & 14 deletions hexrd/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,24 +251,13 @@ def _readenv(name, ctor, default):
del warnings
return default


# 0 = do NOT use numba
# 1 = use numba (default)
USE_NUMBA = _readenv("HEXRD_USE_NUMBA", int, 1)
if USE_NUMBA:
try:
import numba
except ImportError:
print("*** Numba not available, processing may run slower ***")
USE_NUMBA = False

del _readenv


def set_numba_cache():
"""Set the numba cache only if the following are true:
1. We are using numba
1. We are using numba - assumed true now
2. We are on Windows
3. We don't have write access to this file
4. The NUMBA_CACHE_DIR environment variable is not defined
Expand All @@ -277,8 +266,6 @@ def set_numba_cache():
directory where it doesn't have permission, and cause the application to
freeze. Avoid that by setting the cache dir ourselves.
"""
if not USE_NUMBA:
return

if os.name != 'nt':
return
Expand Down
103 changes: 37 additions & 66 deletions hexrd/distortion/dexela_2923.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
@author: Joel V. Bernier
"""
import numpy as np
import numba

from hexrd import constants
from hexrd.constants import USE_NUMBA
if USE_NUMBA:
import numba

from .distortionabc import DistortionABC
from .registry import _RegisterDistortionClass
Expand Down Expand Up @@ -69,71 +67,44 @@ def _find_quadrant(xy_in):
return quad_label


if USE_NUMBA:
@numba.njit(nogil=True, cache=True)
def _dexela_2923_distortion(out_, in_, params):
for el in range(len(in_)):
xi, yi = in_[el, :]
if xi < 0.:
if yi < 0.:
# 3rd quadrant
out_[el, :] = in_[el, :] + params[4:6]
else:
# 2nd quadrant
out_[el, :] = in_[el, :] + params[2:4]
@numba.njit(nogil=True, cache=True)
def _dexela_2923_distortion(out_, in_, params):
for el in range(len(in_)):
xi, yi = in_[el, :]
if xi < 0.:
if yi < 0.:
# 3rd quadrant
out_[el, :] = in_[el, :] + params[4:6]
else:
if yi < 0.:
# 4th quadrant
out_[el, :] = in_[el, :] + params[6:8]
else:
# 1st quadrant
out_[el, :] = in_[el, :] + params[0:2]

@numba.njit(nogil=True, cache=True)
def _dexela_2923_inverse_distortion(out_, in_, params):
for el in range(len(in_)):
xi, yi = in_[el, :]
if xi < 0.:
if yi < 0.:
# 3rd quadrant
out_[el, :] = in_[el, :] - params[4:6]
else:
# 2nd quadrant
out_[el, :] = in_[el, :] - params[2:4]
# 2nd quadrant
out_[el, :] = in_[el, :] + params[2:4]
else:
if yi < 0.:
# 4th quadrant
out_[el, :] = in_[el, :] + params[6:8]
else:
if yi < 0.:
# 4th quadrant
out_[el, :] = in_[el, :] - params[6:8]
else:
# 1st quadrant
out_[el, :] = in_[el, :] - params[0:2]
else:
def _dexela_2923_distortion(out_, in_, params):
# find quadrant
ql = _find_quadrant(in_)
ql1 = ql == 1
ql2 = ql == 2
ql3 = ql == 3
ql4 = ql == 4
out_[ql1, :] = in_[ql1] + np.tile(params[0:2], (sum(ql1), 1))
out_[ql2, :] = in_[ql2] + np.tile(params[2:4], (sum(ql2), 1))
out_[ql3, :] = in_[ql3] + np.tile(params[4:6], (sum(ql3), 1))
out_[ql4, :] = in_[ql4] + np.tile(params[6:8], (sum(ql4), 1))
return

def _dexela_2923_inverse_distortion(out_, in_, params):
ql = _find_quadrant(in_)
ql1 = ql == 1
ql2 = ql == 2
ql3 = ql == 3
ql4 = ql == 4
out_[ql1, :] = in_[ql1] - np.tile(params[0:2], (sum(ql1), 1))
out_[ql2, :] = in_[ql2] - np.tile(params[2:4], (sum(ql2), 1))
out_[ql3, :] = in_[ql3] - np.tile(params[4:6], (sum(ql3), 1))
out_[ql4, :] = in_[ql4] - np.tile(params[6:8], (sum(ql4), 1))
return


# 1st quadrant
out_[el, :] = in_[el, :] + params[0:2]


@numba.njit(nogil=True, cache=True)
def _dexela_2923_inverse_distortion(out_, in_, params):
for el in range(len(in_)):
xi, yi = in_[el, :]
if xi < 0.:
if yi < 0.:
# 3rd quadrant
out_[el, :] = in_[el, :] - params[4:6]
else:
# 2nd quadrant
out_[el, :] = in_[el, :] - params[2:4]
else:
if yi < 0.:
# 4th quadrant
out_[el, :] = in_[el, :] - params[6:8]
else:
# 1st quadrant
out_[el, :] = in_[el, :] - params[0:2]

def test_disortion():
pts = np.random.randn(16, 2)
Expand Down
9 changes: 2 additions & 7 deletions hexrd/findorientations.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,6 @@ def quat_distance(x, y):
qbar[:, i] = rot.quatAverageCluster(
qfib_r[:, cl == i + 1], qsym
).flatten()
pass
pass

if algorithm in ('dbscan', 'ort-dbscan') and qbar.size/4 > 1:
logger.info("\tchecking for duplicate orientations...")
Expand All @@ -374,10 +372,7 @@ def quat_distance(x, y):
tmp[:, i] = rot.quatAverageCluster(
qbar[:, cl == i + 1].reshape(4, npts), qsym
).flatten()
pass
qbar = tmp
pass
pass

logger.info("clustering took %f seconds", timeit.default_timer() - start)
logger.info(
Expand Down Expand Up @@ -617,7 +612,7 @@ def _filter_eta_ome_maps(eta_ome, filter_stdev=False):
"""
gl_filter = ndimage.filters.gaussian_laplace
for i, pf in enumerate(eta_ome.dataStore):
for pf in eta_ome.dataStore:
# first compoute row-wise median over omega channel
ome_median = np.tile(np.nanmedian(pf, axis=0), (len(pf), 1))

Expand Down Expand Up @@ -894,7 +889,7 @@ def find_orientations(cfg,
logger.info("\tmean reflections per grain: %d", mean_rpg)
logger.info("\tneighborhood size: %d", min_samples)

qbar, cl = run_cluster(
qbar, _ = run_cluster(
completeness, qfib, plane_data.getQSym(), cfg,
min_samples=min_samples,
compl_thresh=compl_thresh,
Expand Down
22 changes: 11 additions & 11 deletions hexrd/fitting/peakfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
# ============================================================

import numpy as np
from numba import njit
import copy
from hexrd import constants
from hexrd.utils.decorators import numba_njit_if_available
from hexrd.constants import \
c_erf, cnum_exp1exp, cden_exp1exp, c_coeff_exp1exp

Expand Down Expand Up @@ -56,7 +56,7 @@
"""


@numba_njit_if_available(cache=True, nogil=True)
@njit(cache=True, nogil=True)
def erfc(x):
# save the sign of x
sign = np.sign(x)
Expand All @@ -80,7 +80,7 @@ def erfc(x):
"""


@numba_njit_if_available(cache=True, nogil=True)
@njit(cache=True, nogil=True)
def exp1exp_under1(x):
f = np.zeros(x.shape).astype(np.complex128)
for i in range(6):
Expand All @@ -99,7 +99,7 @@ def exp1exp_under1(x):
"""


@numba_njit_if_available(cache=True, nogil=True)
@njit(cache=True, nogil=True)
def exp1exp_over1(x):
num = np.zeros(x.shape).astype(np.complex128)
den = np.zeros(x.shape).astype(np.complex128)
Expand All @@ -117,7 +117,7 @@ def exp1exp_over1(x):
return (num/den)*(1./x)


@numba_njit_if_available(cache=True, nogil=True)
@njit(cache=True, nogil=True)
def exp1exp(x):
mask = np.sign(x.real)*np.abs(x) > 1.

Expand Down Expand Up @@ -457,19 +457,19 @@ def split_pvoigt1d(p, x):
"""


@numba_njit_if_available(cache=True, nogil=True)
@njit(cache=True, nogil=True)
def _calc_alpha(alpha, x0):
a0, a1 = alpha
return (a0 + a1*np.tan(np.radians(0.5*x0)))


@numba_njit_if_available(cache=True, nogil=True)
@njit(cache=True, nogil=True)
def _calc_beta(beta, x0):
b0, b1 = beta
return b0 + b1*np.tan(np.radians(0.5*x0))


@numba_njit_if_available(cache=True, nogil=True)
@njit(cache=True, nogil=True)
def _mixing_factor_pv(fwhm_g, fwhm_l):
"""
@AUTHOR: Saransh Singh, Lawrence Livermore National Lab,
Expand Down Expand Up @@ -499,7 +499,7 @@ def _mixing_factor_pv(fwhm_g, fwhm_l):
return eta, fwhm


@numba_njit_if_available(nogil=True)
@njit(nogil=True)
def _gaussian_pink_beam(p, x):
"""
@author Saransh Singh, Lawrence Livermore National Lab
Expand Down Expand Up @@ -544,7 +544,7 @@ def _gaussian_pink_beam(p, x):
return g


@numba_njit_if_available(nogil=True)
@njit(nogil=True)
def _lorentzian_pink_beam(p, x):
"""
@author Saransh Singh, Lawrence Livermore National Lab
Expand Down Expand Up @@ -579,7 +579,7 @@ def _lorentzian_pink_beam(p, x):
return y


@numba_njit_if_available(nogil=True)
@njit(nogil=True)
def _pink_beam_dcs_no_bg(p, x):
"""
@author Saransh Singh, Lawrence Livermore National Lab
Expand Down
20 changes: 10 additions & 10 deletions hexrd/fitting/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fnmatch

import numpy as np
from numba import njit

from hexrd.constants import (
c_erf, cnum_exp1exp, cden_exp1exp, c_coeff_exp1exp
)
from hexrd.matrixutil import uniqueVectors
from hexrd.utils.decorators import numba_njit_if_available


# =============================================================================
Expand Down Expand Up @@ -138,7 +138,7 @@ def _set_peak_center_bounds(params, window_range, min_sep=0.01):
"""


@numba_njit_if_available(cache=True, nogil=True)
@njit(cache=True, nogil=True)
def erfc(x):
# save the sign of x
sign = np.sign(x)
Expand All @@ -162,7 +162,7 @@ def erfc(x):
"""


@numba_njit_if_available(cache=True, nogil=True)
@njit(cache=True, nogil=True)
def exp1exp_under1(x):
f = np.zeros(x.shape).astype(np.complex128)
for i in range(6):
Expand All @@ -181,7 +181,7 @@ def exp1exp_under1(x):
"""


@numba_njit_if_available(cache=True, nogil=True)
@njit(cache=True, nogil=True)
def exp1exp_over1(x):
num = np.zeros(x.shape).astype(np.complex128)
den = np.zeros(x.shape).astype(np.complex128)
Expand All @@ -199,7 +199,7 @@ def exp1exp_over1(x):
return (num/den)*(1./x)


@numba_njit_if_available(cache=True, nogil=True)
@njit(cache=True, nogil=True)
def exp1exp(x):
mask = np.sign(x.real)*np.abs(x) > 1.

Expand All @@ -210,19 +210,19 @@ def exp1exp(x):
return f


@numba_njit_if_available(cache=True, nogil=True)
@njit(cache=True, nogil=True)
def _calc_alpha(alpha, x0):
a0, a1 = alpha
return (a0 + a1*np.tan(np.radians(0.5*x0)))


@numba_njit_if_available(cache=True, nogil=True)
@njit(cache=True, nogil=True)
def _calc_beta(beta, x0):
b0, b1 = beta
return b0 + b1*np.tan(np.radians(0.5*x0))


@numba_njit_if_available(cache=True, nogil=True)
@njit(cache=True, nogil=True)
def _mixing_factor_pv(fwhm_g, fwhm_l):
"""
@AUTHOR: Saransh Singh, Lawrence Livermore National Lab,
Expand Down Expand Up @@ -252,7 +252,7 @@ def _mixing_factor_pv(fwhm_g, fwhm_l):
return eta, fwhm


@numba_njit_if_available(nogil=True)
@njit(nogil=True)
def _gaussian_pink_beam(p, x):
"""
@author Saransh Singh, Lawrence Livermore National Lab
Expand Down Expand Up @@ -298,7 +298,7 @@ def _gaussian_pink_beam(p, x):
return g


@numba_njit_if_available(nogil=True)
@njit(nogil=True)
def _lorentzian_pink_beam(p, x):
"""
@author Saransh Singh, Lawrence Livermore National Lab
Expand Down
Loading

0 comments on commit b7a88d8

Please sign in to comment.