scsr package
Submodules
scsr.maths module
- scsr.maths.calc_velocity(theta_array: ndarray, phi_array: ndarray, vf: float) ndarray
Get velocity in x, y and z.
- Returns:
x, y and z velocity.
- Return type:
xp.ndarray[theta*phi*3]
- scsr.maths.cartesian_product(*arrays: ndarray, make_2d: bool = True)
- scsr.maths.chunk_indices(length: int, chunk_size: int)
- scsr.maths.compute_functions(functions, p, cache, result_only=False)
Computes the relevant H,G,A1,A2 matrices with the inclusion of the Fuchs specularity parameter.
- Parameters:
functions (list[str]) – Functions to compute. Can include (“H”, “A1”, “A2”, “G”).
(dict[str (C) – object]): parameters used for calculations.
(dict[str – xp.ndarray]): Arrays used for calculations.
- Returns:
dict[str:xp.ndarray]]: Arrays of the functions in the format:
{ FUNCTION_NAME: { "array": xp.ndarray[theta*phi], "integral": xp.ndarray[theta*phi], }, ... }
- Return type:
dict[str
- scsr.maths.ensure_numpy_array(obj: ndarray) ndarray
If obj is a cupy array, return a matching numpy array. Otherwise return obj.
- scsr.maths.get_epsilon_at_index(results, index: int) Tuple[ndarray, ...]
Calculate stuff.
TODO: Add proper description here.
- Parameters:
results (results.ResultsBase) – Results object.
index – Index at which to fetch the parameters.
- Returns:
Values (in same order as EPSILON_FUNCTIONS)
- Return type:
Tuple[ndarray, …]
- scsr.maths.get_epsilon_function_shapes(results)
- scsr.maths.mn_mul(arr_a: ndarray, arr_b: ndarray) ndarray
Given an (1 or m, 1 or n)-shaped array and an (m, n, theta, phi)-shaped array, return an array where each (theta, phi) slice is multipled by the value at (m,n).
Example
>>> mn_mul(np.array([[1,2],[3,4]]), np.ones((2,2,1,1))) array([[[[1.]], [[2.]]], [[[3.]], [[4.]]]]) >>> mn_mul(np.array([[1,2]]), np.ones((2,2,1,1))) array([[[[1.]], [[2.]]], [[[1.]], [[2.]]]]) >>> mn_mul(np.array([[1],[2]]), np.ones((2,2,1,1))) array([[[[1.]], [[1.]]], [[[2.]], [[2.]]]])
- scsr.maths.mul_axes(arr_a: ndarray, arr_b: ndarray, axes: List[int]) ndarray
Alias for op_across_axes(arr_a, arr_b, axes, operator.mul)
- scsr.maths.op_across_axes(arr_a: ndarray, arr_b: ndarray, axes: List[int], op: Callable[[ndarray, ndarray], ndarray]) ndarray
Perform operation on two arrays along the given axes.
Example
>>> arr_a, arr_b = np.arange(9).reshape(3,3), np.array([[[-1,1,0]]]) >>> op_across_axes(arr_a, arr_b, [0,2,1], operator.mul) array([[[ 0, 3, 0], [-1, 4, 0], [-2, 5, 0]]]) >>> op_across_axes(arr_a, arr_b, [0,1,2], operator.mul) array([[[ 0, 1, 0], [-3, 4, 0], [-6, 7, 0]]])
- scsr.maths.sadd(arr_a: ndarray, arr_b: ndarray) ndarray
Alias for stack_op(arr_a, arr_b, operator.add)
- scsr.maths.set_gpu_mode(enabled: bool)
Set whether to use GPU or not.
This sets the xp global within the maths module to the cupy module, rather than numpy as it is by default.
This should probably be set before calling
update_arrays(), to ensure that all the cached arrays are CuPy arrays and loaded into GPU memory.- Parameters:
enabled (bool) – True to use GPU, False to use CPU.
- scsr.maths.smul(arr_a: ndarray, arr_b: ndarray) ndarray
Alias for stack_op(arr_a, arr_b, operator.mul)
- scsr.maths.stack_broadcast(arr_a: ndarray, arr_b: ndarray) ndarray
Given arr_a (with n dims) and arr_b (with m dims), return a new array with n+m dims, with arr_b broadcast across on every new leading axis.
Example
>>> stack_broadcast(np.empty((2,2)), np.array([5,10])) array([[[ 5, 10], [ 5, 10]], [[ 5, 10], [ 5, 10]]])
- scsr.maths.stack_op(arr_a: ndarray, arr_b: ndarray, op: Callable[[ndarray, ndarray], ndarray]) ndarray
Given arr_a (with x dims) and arr_b (with y dims), return a new array with x+y dims, where each axis is a combination of an arr_a value and an arr_b value.
Example
>>> stack_op(np.array([[1,2],[3,4]]), np.array([-1,10]), operator.mul) array([[[-1, 10], [-2, 20]], [[-3, 30], [-4, 40]]])
- scsr.maths.tile_2d_arr(width: int, height: int, max_width: int, max_height: int) Generator[Tuple[int, int, int, int], None, None]
Yields tile dimensions of tiles of an array.
- Parameters:
width – Width of array
height – Height of array
max_width – Max width of tile
max_height – Max height of tile
Example
>>> for tile in tile_2d_arr(5,5,3,2): print(tile) (0, 3, 0, 2) (3, 5, 0, 2) (0, 3, 2, 4) (3, 5, 2, 4) (0, 3, 4, 5) (3, 5, 4, 5) >>> for tile in tile_2d_arr(7,3,4,2): print(tile) (0, 4, 0, 2) (4, 7, 0, 2) (0, 4, 2, 3) (4, 7, 2, 3)
- Yields:
tuple with the 4 values for each tile –
horizontal start index
horizontal end index + 1
vertical start index
vertical end index + 1
- scsr.maths.update_arrays(p: Dict[str, ndarray], cache: Dict[str, ndarray]) None
Update cache with intermediary arrays to use for calculations.
This is a key method used to optimise calculations, with the primary goal of limiting any cases where we might otherwise recalculate the same array twice. All of the quantities cached here are used to calculate the functions in compute_functions.
- Parameters:
p – The parameters to use to calculate the quantities. Only values that depend on parameters which differ between p and cache[‘last_p’] will be calculated.
cache – Dictionary to update with the quantities.
- scsr.maths.values_differ(d1: dict, d2: dict, keys: Iterable[Hashable])
Check if any of the values for the given keys differ between d1 & d2.
d1 (dict): Dictionary.
keys (iterable[str]): Keys to check.
- Returns:
True if values differ, False otherwise.
- Return type:
bool
scsr.plots module
plot the results from the thing bla
- scsr.plots.generate_plots(args)
- scsr.plots.write_plot(axes, axis_labels, iteration_params, array_2d, func_name, title, variable_params, figs_dir='figs')
- scsr.plots.write_plots_from_plots_pickle(plots_pickle, variable_params, figs_dir)
scsr.results module
Module defining objects used for managing matrices of results.
- class scsr.results.ChunkedEpsilonResults(results)
Bases:
ChunkedResults,EpsilonResults- get_epsilon_at_index(index)
- class scsr.results.ChunkedResults(results)
Bases:
ResultsCombine the results of a set of runs.
- Parameters:
results (list[Results]) – list of results.
- get_m_n_array_from_index(function, index)
- reconstruct_readable_param_args(all_chunks=None)
- class scsr.results.EpsilonResults(functions, parameters, variable_params)
Bases:
Results- classmethod from_dict(dictionary)
- get_epsilon_at_index(index)
- get_pickle_type()
- class scsr.results.EpsilonResultsProcessor(functions, parameters, variable_params, dtype=<class 'numpy.complex128'>)
Bases:
ProcessorBase,EpsilonResults- add_m_n_arrays(i, arrays)
- numpyify()
- reserve_memory()
reserve memory for the arrays.
- Returns:
filesize estimate in bytes.
- Return type:
int
- size_estimate()
- class scsr.results.PickleType(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)
Bases:
Enum- EPSILON_PLOTS = 2
- EPSILON_VALUES = 1
- M_N_ARRAYS = 0
- class scsr.results.PlotIterator(results, axes)
Bases:
object
- class scsr.results.ProcessorBase(functions, parameters, variable_params, dtype=<class 'numpy.complex128'>)
Bases:
Results- get_tasks()
- class scsr.results.Results(functions, parameters, variable_params)
Bases:
ResultsBase- classmethod from_dict(dictionary)
- get_dtype()
- get_epsilon_at_index(index)
- get_m_n_array_from_index(function, index)
- get_m_n_array_from_values(function, iteration_params)
- get_pickle_type()
- iter_plots(axes=('w', 'Kx'))
- class scsr.results.ResultsBase(parameters, variable_params)
Bases:
object- as_dict(**kwargs)
- get_param_args(all_chunks=False)
- get_param_at_index(param_name, index)
- get_pickle_type()
- param_combination_count()
- parameter_combinations()
- reconstruct_readable_param_args(all_chunks=False)
- update_metadata(result_dict)
- class scsr.results.ResultsProcessor(functions, parameters, variable_params, dtype=<class 'numpy.complex128'>)
Bases:
ProcessorBase- add_m_n_array(function, i, array)
- add_m_n_arrays(i, arrays)
- numpyify()
- reserve_memory()
reserve memory for the arrays.
- Returns:
filesize estimate in bytes.
- Return type:
int
- size_estimate()
- scsr.results.calculate_m_n_sizes(results)
- scsr.results.load_results(pickle_files)
- scsr.results.readable_filesize(num, suffix='B')