sgpykit.util.misc

Functions

append(some_list, item)

Append an item to a list and return as a numpy array.

apply_lev2knots(i, lev2knots[, N])

Apply level-to-knots mapping to an index array.

contextmanager(func)

@contextmanager decorator.

generate_multi_indices(shape[, min_idx])

Generate all multi-indices for a given shape.

get_shape_of_cells(objects)

Get the shape of the first Cell object in the list of objects.

is_int_nan(values)

Check if values are the integer NaN workaround.

is_numeric_scalar(item)

Check if an item is a numeric scalar.

matlab_to_python_index(var)

Convert MATLAB-style index to Python-style index.

merge_all_args_to_kwargs(args, kwargs[, ...])

Merge positional and keyword arguments into a single dictionary.

print_plain_int(x)

Print integer values in a plain format.

python_to_matlab_index(var)

Convert Python-style index to MATLAB-style index.

reshape_nested_lists_to_nrows(obj[, nrows])

Reshape nested lists to a specified number of rows.

safe_vstack(*arrays)

Safely stack arrays vertically, filtering out empty arrays.

time_block([description])

Measure and print the execution time of a code block.

to_array(item, n)

Create an array of length n with the given item.

unique_rows_stable(A, B)

Find unique rows in the combined array of A and B, maintaining order.

Classes

Cell(shape[, dtype])

A 1D or 2D container class that extends Python's built-in list.

sgpykit.util.misc.append(some_list, item)[source]

Append an item to a list and return as a numpy array.

Parameters:
some_listlist

List to append to.

itemany

Item to append.

Returns:
numpy.ndarray

Array with the item appended.

sgpykit.util.misc.apply_lev2knots(i, lev2knots, N=None)[source]

Apply level-to-knots mapping to an index array.

Parameters:
inumpy.ndarray

Array of 0-based indices.

lev2knotslist or Cell or callable

Level-to-knots mapping function or list of functions.

Nint, optional

Number of dimensions (default is None).

Returns:
numpy.ndarray

Mapped indices.

sgpykit.util.misc.generate_multi_indices(shape, min_idx=0)[source]

Generate all multi-indices for a given shape.

Parameters:
shapetuple

Shape of the multi-indices to generate.

min_idxint, optional

Minimum index value (default is 0).

Returns:
numpy.ndarray

Array of multi-indices.

sgpykit.util.misc.get_shape_of_cells(objects)[source]

Get the shape of the first Cell object in the list of objects.

Parameters:
objectslist

List of objects to check for Cell type and shape.

Returns:
numpy.ndarray or None

Shape of the first Cell object found, or None if no Cell object is found.

sgpykit.util.misc.is_int_nan(values)[source]

Check if values are the integer NaN workaround.

Parameters:
valuesnumpy.ndarray

Values to check.

Returns:
numpy.ndarray

Boolean array indicating which values are the integer NaN workaround.

sgpykit.util.misc.matlab_to_python_index(var)[source]

Convert MATLAB-style index to Python-style index.

Parameters:
varint or numpy.ndarray

MATLAB-style index or array of indices.

Returns:
int or numpy.ndarray

Python-style index or array of indices.

sgpykit.util.misc.merge_all_args_to_kwargs(args, kwargs, to_lowercase=False)[source]

Merge positional and keyword arguments into a single dictionary.

Parameters:
argslist

Positional arguments.

kwargsdict

Keyword arguments.

to_lowercasebool, optional

Convert keys to lowercase (default is False).

Returns:
tuple

Tuple of merged keyword arguments and format string.

sgpykit.util.misc.print_plain_int(x)[source]

Print integer values in a plain format.

Parameters:
xint or list or tuple

Integer or list/tuple of integers to print.

sgpykit.util.misc.python_to_matlab_index(var)[source]

Convert Python-style index to MATLAB-style index.

Parameters:
varint or array-like

Python-style index or array of indices.

Returns:
int or numpy.ndarray

MATLAB-style index or array of indices.

sgpykit.util.misc.reshape_nested_lists_to_nrows(obj, nrows=2)[source]

Reshape nested lists to a specified number of rows.

Parameters:
objlist

Nested list to reshape.

nrowsint, optional

Number of rows (default is 2).

Returns:
list

Reshaped list.

sgpykit.util.misc.safe_vstack(*arrays)[source]

Safely stack arrays vertically, filtering out empty arrays.

Parameters:
*arraysnumpy.ndarray

Arrays to stack.

Returns:
numpy.ndarray

Stacked array.

sgpykit.util.misc.time_block(description='')[source]

Measure and print the execution time of a code block.

Args:

description (str): Optional description of the timed block.

sgpykit.util.misc.to_array(item, n)[source]

Create an array of length n with the given item.

Parameters:
itemany

Item to repeat.

nint

Length of the array.

Returns:
list

Array of length n with the given item.

sgpykit.util.misc.unique_rows_stable(A, B)[source]

Find unique rows in the combined array of A and B, maintaining order.

Parameters:
Anumpy.ndarray

First array.

Bnumpy.ndarray

Second array.

Returns:
numpy.ndarray

Array of unique rows.