hybrid_jp.arrays module#
Array operations for hybrid_jp.
- hybrid_jp.arrays.create_orthonormal_basis_from_vec(v: ndarray[Any, dtype[float64]]) ndarray[Any, dtype[float64]][source]#
create a basis of orthonormal vectors from an input vector v.
The original components of the vector (i,j,k) in the original basis are transformed to a new orthonormal basis (e1,e2,e3) where the component (e1,0,0) in the new basis points in the direction of v in the original. Therefore, e2 and e3 lie in the plane normal to v and are mutually orthogonal.
- Parameters:
v (hj.arrfloat) – 1D vector length 3
- Returns:
square array shape (3,3). e_n=arr[n-1,:] where n=1,2,3
- Return type:
hj.arrfloat
Examples
>>> v = np.array([1, 2, 3], dtype=np.float64) >>> basis = create_orthonormal_basis_from_vec(v) >>> print("e1", basis[:, 0]) >>> print("e2", basis[:, 1]) >>> print("e3", basis[:, 2]) >>> print("||e1|| ||e2|| ||e3|| -> ", np.linalg.norm(basis, axis=1)) >>> print("e1 . e2 = 0?", np.allclose(np.dot(orth[:, 0], orth[:, 1]), 0)) >>> print("e1 . e3 = 0?", np.allclose(np.dot(orth[:, 0], orth[:, 2]), 0)) >>> print("e2 . e3 = 0?", np.allclose(np.dot(orth[:, 1], orth[:, 2]), 0)
- hybrid_jp.arrays.df_to_rows_array(data: DataFrame, cols: list[str]) ndarray[source]#
Convert dataframe into 2d np array [x, columns].
- Parameters:
data (pd.DataFrame) – data
cols (list[str]) – Column names to transform into array
- Returns:
2d array of data
- Return type:
np.ndarray
- hybrid_jp.arrays.interpolate_to_midpoints(arr: ndarray, width: int) ndarray[source]#
Interpolate to midpoints.
returns an array of length len(arr) - width + 1.
- Parameters:
arr (np.ndarray) – array to interpolate.
width (int) – width of moving average.
- Returns:
interpolated array.
- Return type:
np.ndarray
- hybrid_jp.arrays.logspaced_edges(arr: numpy.ndarray[Any, numpy.dtype[numpy.float64]] | numpy.ndarray[Any, numpy.dtype[numpy.int64]]) ndarray[Any, dtype[float64]][source]#
Expand a (possibly uneven but approximately) logarithmically spaced arr to edges.
arr is shape (N,), therefore the returned array is shape (N+1,). The end points are given by n_0 - (n_1 - n_0)/2 and n_N + (n_N - n_{N-1})/2 for an array (n_0…n_N) = log10(arr). The spacing between values of the array is preserved, this is useful in the case of integer logspaced arrays where diff(log10(arr)) is not constant do to integer rounding. So, each value in the new array is half of the separation between the original values.
- Parameters:
arr (arrfloat | arrint) – Array of values.
- Returns:
Array of edges.
- Return type:
arrfloat
Example
>>> import matplotlib.pyplot as plt >>> import numpy as np >>> arr = np.unique(np.logspace(0, 2, 15, dtype=np.int32)) >>> brr = logspaced_edges(arr) >>> fig, axs = plt.subplots(2, 1, figsize=(8, 2)) >>> axlin, axlog = axs >>> orig = axlin.scatter(arr, np.zeros_like(arr), marker="x", color="k") >>> new = axlin.scatter(brr, np.zeros_like(brr), marker="+", color="r") >>> orig = axlog.scatter(arr, np.zeros_like(arr), marker="x", color="k") >>> new = axlog.scatter(brr, np.zeros_like(brr), marker="+", color="r") >>> axlog.set_xscale("log") >>> axlin.set_title("Linear scale") >>> axlog.set_title("Log scale") >>> axlin.set_yticks([]) >>> axlog.set_yticks([]) >>> axlog.set_xlabel("'x' = original, '+' = bin edges") >>> fig.tight_layout() >>> plt.show()
- hybrid_jp.arrays.mov_avg(data: ndarray, width: int) ndarray[source]#
Perform a moving average.
- Parameters:
data (np.ndarray) – data to perform moving average on.
width (int) – width of moving average.
- Returns:
moving average of data. length is len(data) - width + 1.
- Return type:
np.ndarray
- hybrid_jp.arrays.rotate_arr_to_new_basis(basis: ndarray[Any, dtype[float64]], arr: ndarray[Any, dtype[float64]]) ndarray[Any, dtype[float64]][source]#
Rotate an array (nx, ny, 3) in i,j,k basis to a new basis.
- Parameters:
basis (hj.arrfloat) – (3,3) basis of (e1, e2, e3) where basis[0,:]=e1 etc.
arr (hj.arrfloat) – (nx, ny, 3) array of vectors on a grid (nx, ny)
- Returns:
(nx, ny, 3) array of vectors in e1,e2,e3 basis
- Return type:
hj.arrfloat
Example
>>> import numpy as np >>> import matplotlib.pyplot as plt >>> scale = 100 >>> xx, yy = np.mgrid[0:scale, 0:scale] >>> grid = np.empty((*xx.shape, 3)) >>> grid[:, :, 0] = yy >>> grid[:, :, 1] = xx >>> # Add an oscillating part in k >>> grid[:, :, 2] = np.sin(2 * np.pi * xx / xx.max()) >>> # e1=k in new basis >>> vec = np.array([0, 0, 1], dtype=np.float64) >>> print(f"{vec = }") >>> basis = create_orthonormal_basis_from_vec(vec) >>> print(f"{basis = }") >>> rot = rotate_arr_to_new_basis(basis, grid) >>> fig, axs = plt.subplots(2, 1) >>> # Plot i and j streamlines coloured by k >>> axs[0].streamplot( >>> xx[:, 0], >>> yy[0, :], >>> grid[:, :, 0].T, >>> grid[:, :, 1].T, >>> density=2, >>> arrowstyle="-", >>> color=grid[:, :, 2].T, >>> ) >>> # Visualise e1, should be a horizontal sine wave >>> axs[1].pcolormesh(xx[:, 0], yy[0, :], rot[:, :, 0].T) >>> axs[0].grid(False) >>> axs[1].grid(False) >>> fig.tight_layout() >>> plt.show()
- hybrid_jp.arrays.trim_var(var: ndarray, slc: slice) ndarray[source]#
Trim a variable to a slice.
- Parameters:
var (np.ndarray) – Variable to trim.
slc (slice) – Slice to trim to.
- Returns:
Trimmed variable.
- Return type:
np.ndarray
- hybrid_jp.arrays.trim_vars(vars_list: list[numpy.ndarray], slc: slice) list[numpy.ndarray][source]#
Trim a list of variables to a slice.
- Parameters:
vars_list (list[np.ndarray]) – List of variables to trim.
slc (slice) – Slice to trim to.
- Returns:
Trimmed variables.
- Return type:
list[np.ndarray]
Example
>>> trim_vars([np.arange(10), np.arange(10)], slice(0, 5)) [array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4])]