pyloggrid.Libs.datasci

Libraries for data science

Module Contents

Functions

filter_range(→ numpy.ndarray)

Trims a 1D array from its first and last portions.

mean(→ tuple[numpy.ndarray, numpy.ndarray | None])

Mean of data. If ts is provided, the mean is weighted by the time intervals. If log, the log mean is returned.

clamp_zero(→ numpy.ndarray)

Replace all zero values with fill (defaults to min(data)/10)

fit(→ tuple[float, Ellipsis])

Fit (x,y) by the function f with an optional mask

powerlaw_fit(→ tuple[float, float])

Power law fit Y = b * X ^ a

randcomplex_like(→ numpy.ndarray)

Random array of complex numbers, see randcomplex()

randcomplex(→ numpy.ndarray)

Random array of complex numbers, normally distributed in modulus and uniformly in angle

rand_seeded_by_array(→ numpy.ndarray)

Returns a pseudorandom array of same shape as source where items with the same source will have the same value for a given seed.

randcomplex_seeded_by_array(→ numpy.ndarray)

Like rand_seeded_by_array(), but returns a complex array (uniform in a square)

ragged_array_to_array(→ numpy.ndarray)

Transform an 1D array of 1D array of different sizes into an array of array of same sizes, i.e a classic 2D array.

logmean(→ numpy.ndarray)

Logarithmic mean along an axis, ignoring zero and nan values.

filter_range(data: ndarray, start: float = 0, end: float = 1) ndarray

Trims a 1D array from its first and last portions.

This is a shortcut to data[int(size * start) : int(size * end)]

Parameters:
  • data – the data to filter

  • start – where to start in the values (ex: 0.2 -> we ommit the 20% first values)

  • end – where to end in the values (ex: 0.8 -> we ommit the 20% last values)

Returns:

the new array (if filtered, the shape is altered)

mean(data: ndarray, ts: ndarray | None = None, log: bool = False, start: float = 0, end: float = 1) tuple[ndarray, ndarray | None]

Mean of data. If ts is provided, the mean is weighted by the time intervals. If log, the log mean is returned.

Parameters:
  • data – data to average

  • ts – times associated with the data. Must be increasing.

  • log – if True, only the mean avg is returned exp(mean(log(abs(data_weighted))), and the variance returns None

  • start – where to start in the values (ex: 0.2 -> we ommit the 20% first values)

  • end – where to end in the values (ex: 0.2 -> we ommit the 20% last values)

Returns:

the mean and std value

clamp_zero(data: ndarray, fill: Any = None) ndarray

Replace all zero values with fill (defaults to min(data)/10)

Parameters:
  • data – array to clamp

  • fill – if specified, all zero values are filled with this

Returns:

The clamped array

fit(f: Callable, x: ndarray, y: ndarray, mask: ndarray | tuple[float, float] = None) tuple[float, Ellipsis]

Fit (x,y) by the function f with an optional mask

Parameters:
  • f – fit function

  • x

  • y

  • mask – an array mask, or a tuple (start, end)

powerlaw_fit(x: ndarray, y: ndarray, mask: ndarray | float = None, a: float = None) tuple[float, float]

Power law fit Y = b * X ^ a

Parameters:
  • x – X array

  • y – Y array

  • mask – mask to specify the relevant values of X, Y to use (same as fit())

  • a – optional fixed value for a. If specified, only b is fitted

Returns:

the fit parameters b, a

randcomplex_like(data: ndarray) ndarray

Random array of complex numbers, see randcomplex()

Parameters:

data – array to copy the shape from

Returns:

random array

randcomplex(shape: Iterable) ndarray

Random array of complex numbers, normally distributed in modulus and uniformly in angle

Parameters:

shape – return array shape

Returns:

random array

rand_seeded_by_array(source: ndarray, seed: int) ndarray

Returns a pseudorandom array of same shape as source where items with the same source will have the same value for a given seed.

This is useful when creating pseudorandom arrays based on kx,ky,kz, of different sizes, e.g. when changing the grid size but keeping the same pseudorandom forcing

Warning

This is not optimised for speed, and should probably not be called every step

randcomplex_seeded_by_array(source: ndarray, seed: int) ndarray

Like rand_seeded_by_array(), but returns a complex array (uniform in a square)

ragged_array_to_array(data: ndarray | list, fill=np.nan) ndarray

Transform an 1D array of 1D array of different sizes into an array of array of same sizes, i.e a classic 2D array.

Smaller arrays are padded with fill.

If a classic 2D array is given, it does nothing.

Parameters:
  • data – array of array of different sizes

  • fill – fill value

Returns:

padded 2D-array

logmean(data: ndarray, axis=None) ndarray

Logarithmic mean along an axis, ignoring zero and nan values.