utils.py
#
Miscellaneous utility functions.
get_array_module(a)
#
round_to_multiple(x, multiple=10, method='nearest')
#
Round value x
to next multiple
using method
.
Source code in pseudo_3D_interpolation\functions\utils.py
log_message(msg_lvl, verbosity, *msg_args)
#
xprint(*args, kind='info', verbosity=0, **kwargs)
#
Thin wrapper function for build-in print() to add informative prefix and color-coded print statements.
Source code in pseudo_3D_interpolation\functions\utils.py
clean_log_file(path_log, newline='\n')
#
Remove colored terminal output characters from ASCII log.
Source code in pseudo_3D_interpolation\functions\utils.py
timeit(func)
#
Decorate function to measure its runtime.
Source code in pseudo_3D_interpolation\functions\utils.py
profile(output_file=None, sort_by='cumulative', lines_to_print=None, strip_dirs=False)
#
Profile function.
Inspired by and modified the profile decorator of Giampaolo Rodola.
Copied code by Ehsan Khodabandeh from towardsdatascience.com
.
Parameters:
-
output_file
(str or None
, default:None
) –Path of the output file. If only name of the file is given, it's saved in the current directory. If it's None, the name of the decorated function is used (default:
None
). -
sort_by
(str or SortKey enum or tuple/list of str/SortKey enum
, default:'cumulative'
) –Sorting criteria for the Stats object. For a list of valid string and SortKey refer to: https://docs.python.org/3/library/profile.html#pstats.Stats.sort_stats
-
lines_to_print
(int or None
, default:None
) –Number of lines to print. Default (None) is for all the lines. This is useful in reducing the size of the printout, especially that sorting by 'cumulative', the time consuming operations are printed toward the top of the file.
-
strip_dirs
(bool
, default:False
) –Whether to remove the leading path info from file names. This is also useful in reducing the size of the printout
Returns:
-
func
–Profile of the decorated function
References#
-
Giampaolo Rodola, http://code.activestate.com/recipes/577817-profile-decorator/ ↩
-
Ehsan Khodabandeh, https://towardsdatascience.com/how-to-profile-your-code-in-python-e70c834fad89 ↩
Source code in pseudo_3D_interpolation\functions\utils.py
debug(func)
#
Decorate function to print debugging information.
Source code in pseudo_3D_interpolation\functions\utils.py
show_progressbar(progressbar, verbose=False)
#
pad_array(a, n, zeros=False)
#
Pad 1D input array with n
elements at start and end (mirror of array).
Parameters:
-
a
(ndarray
) –1D input array.
-
n
(int
) –Number of elements to add at start and end.
-
zeros
(bool
, default:False
) –Add zeros instead of mirrored values (default:
False
).
Returns:
-
ndarray
–Padded input array.
Source code in pseudo_3D_interpolation\functions\utils.py
pad_along_axis(array, n, mode='constant', kwargs=None, axis=-1)
#
Pad 2D array along given axis (default: -1
).
Parameters:
-
array
(ndarray
) –Input data.
-
n
(int
) –Number of values padded to the edges of specified axis.
-
mode
(str
, default:'constant'
) –How to pad array edges (see
np.pad
, default:constant
). -
kwargs
(dict
, default:None
) –OPtional keyword arguments for
np.pad
(default:None
). -
axis
(int
, default:-1
) –Axis to pad (default:
-1
).
Returns:
-
ndarray
–Padded input array.
Source code in pseudo_3D_interpolation\functions\utils.py
slice_valid_data(data, nso)
#
Account for zero padded input data and return only valid data samples (!= 0).
Parameters:
-
data
(ndarray
) –Seismic section (samples x traces).
-
nso
(int
) –Original number samples per trace (from binary header).
Returns:
-
ndarray
–"Unpadded" seismic section with only valid (non-zero) samples (2D).
-
idx_start_slice
(ndarray
) –Array of starting indices for valid data slice per trace.
Source code in pseudo_3D_interpolation\functions\utils.py
depth2twt(depth, v=1500)
#
twt2depth(twt, v=1500, units='s')
#
Convert two-way travel time (TWT in sec) to depth (m).
Source code in pseudo_3D_interpolation\functions\utils.py
twt2samples(twt, dt, units='s')
#
Convert TWT (sec) to samples (#) based on sampling interval dt
(sec).
Source code in pseudo_3D_interpolation\functions\utils.py
samples2twt(samples, dt)
#
depth2samples(depth, dt, v=1500, units='s')
#
Convert depth (m) to samples (#) given a sampling interval dt
and acoustic velocity.
Source code in pseudo_3D_interpolation\functions\utils.py
samples2depth(samples, dt, v=1500, units='s')
#
Convert samples (#) to depth (m) given a sampling interval dt
and acoustic velocity.
Source code in pseudo_3D_interpolation\functions\utils.py
convert_twt(twt, unit_in, unit_out)
#
Convert TWT unit.
Parameters:
-
twt
((int, float, ndarray)
) –Input TWT value(s).
-
unit_in
(str
) –Input time unit, one of ['s', 'ms', 'us', 'ns'].
-
unit_out
(str
) –Output time unit, one of ['s', 'ms', 'us', 'ns'].
Returns:
-
(int, float, ndarray)
–Converted TWT value(s).
Source code in pseudo_3D_interpolation\functions\utils.py
euclidean_distance(coords)
#
Calculate euclidean distance between consecutive points in array (row-wise).
rescale(a, vmin=0, vmax=1)
#
Rescale array to given range (default: [0, 1]).
Parameters:
-
a
(ndarray
) –Input array to rescale/normalize.
-
vmin
(float
, default:0
) –New minimum value (default:
0
). -
vmax
(float
, default:1
) –New maximum value (default:
1
).
Returns:
-
ndarray
–Rescaled input array.
Source code in pseudo_3D_interpolation\functions\utils.py
rescale_dask(a, vmin=0, vmax=1, amin=None, amax=None)
#
Rescale array to given range (default: [0, 1]).
Parameters:
-
a
(ndarray
) –Input array to rescale/normalize.
-
vmin
(float
, default:0
) –New minimum value (default:
0
). -
vmax
(float
, default:1
) –New maximum value (default:
1
). -
amin
(float
, default:None
) –Minimum value (default:
None
). -
amax
(float
, default:None
) –New maximum value (default:
None
).
Returns:
-
ndarray
–Rescaled input array.