topologicpy.Helper module

class topologicpy.Helper.Helper

Bases: object

Methods

BinAndAverage(listA[, mantissa, tolerance, ...])

Groups numbers within a specified tolerance into bins, calculates the average of each bin, and returns a sorted list of these averages.

CheckVersion([version, silent])

Compare an input version with the latest version of a Python library on PyPI.

ClosestMatch(item, listA)

Returns the index of the closest match in the input list to the input item.

ClusterByKeys(elements, dictionaries, *keys)

Clusters the input list of elements and dictionaries based on the input key or keys.

Flatten(listA)

Flattens the input nested list.

Grow(seed_idx, group_size, adjacency, ...)

Attempts to grow a spatially connected group of a specified size starting from a given seed index.

Iterate(listA)

Iterates the input nested list so that each sublist has the same number of members.

MakeUnique(listA)

Forces the strings in the input list to be unique if they have duplicates.

MaximumIndices(listA[, silent])

Returns a list of indices of the maximum value in the input list.

MergeByThreshold(listA[, threshold])

Merges the numbers in the input list so that numbers within the input threshold are averaged into one number.

MinimumIndices(listA[, silent])

Returns a list of indices of the minimum value in the input list.

Normalize(listA[, mantissa])

Normalizes the input list so that it is in the range 0 to 1

Position(item, listA)

Returns the position of the item in the list or the position it would have been inserts.

RemoveEven(listA)

Removes the even indexed members of the input list.

RemoveOdd(listA)

Removes the odd indexed members of the input list.

Repeat(listA)

Repeats the input nested list so that each sublist has the same number of members.

Sort(listA, *otherLists[, reverseFlags, silent])

Sorts the first input list according to the values in the subsequent input lists in order.

Transpose(listA)

Transposes the input list (swaps rows and columns).

Trim(listA)

Trims the input nested list so that each sublist has the same number of members.

Version([check, silent])

Returns the current version of the software.

static BinAndAverage(listA, mantissa: int = 6, tolerance: float = 0.0001, silent: bool = False)

Groups numbers within a specified tolerance into bins, calculates the average of each bin, and returns a sorted list of these averages.

Parameters
listAlist

The input list.

mantissaint , optional

The desired length of the mantissa. Default is 6

tolerancefloat , optional

The desired tolerance. Default is 0.0001.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns
list

The sorted list of bin averages.

CheckVersion(version: str = None, silent: bool = False)

Compare an input version with the latest version of a Python library on PyPI.

Parameters
librarystr

The input software library name. Default is None.

versionstr

The input software version number to compare. Default is None.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns:
str: A message indicating whether the input version is less than,

equal to, or greater than the latest version on PyPI.

static ClosestMatch(item, listA)

Returns the index of the closest match in the input list to the input item. This works for lists made out of numeric or string values.

Parameters
itemint, float, or str

The input item.

listAlist

The input list.

Returns
int

The index of the best match in listA for the input item.

static ClusterByKeys(elements, dictionaries, *keys, silent=False)

Clusters the input list of elements and dictionaries based on the input key or keys.

Parameters
elementslist

The input list of elements to be clustered.

dictionarieslist[Topology.Dictionary]

The input list of dictionaries to be consulted for clustering. This is assumed to be in the same order as the list of elements.

keysstr or list or comma-separated str input parameters

The key or keys in the topology’s dictionary to use for clustering.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns
dict

A dictionary containing the elements and the dictionaries, but clustered. The dictionary has two keys: “elements”: list

A nested list of elements where each item is a list of elements with the same key values.

“dictionaries”: list

A nested list of dictionaries where each item is a list of dictionaries with the same key values.

static Flatten(listA)

Flattens the input nested list.

Parameters
listAlist

The input nested list.

Returns
list

The flattened list.

static Grow(seed_idx, group_size, adjacency, visited_global)

Attempts to grow a spatially connected group of a specified size starting from a given seed index.

This method uses a breadth-first search strategy to explore neighboring indices from the seed index, guided by the provided adjacency dictionary. It avoids reusing indices that are globally visited. The growth continues until the desired group size is reached or no further expansion is possible.

Parameters
seed_idxint

The index from which to start growing the group.

group_sizeint

The target size of the group to be grown.

adjacencydict

A dictionary mapping each index to a list of adjacent indices. This defines the connectivity.

visited_globalset

A set of indices that have already been used in previously grown groups and should be avoided.

Returns
list[int] or None

A list of indices representing a connected group of the specified size if successful, otherwise None.

Notes

This method is intended for internal use in functions that generate connected subgroups of spatial elements (e.g., cells) based on adjacency. The result may vary between runs due to random shuffling of neighbor order to diversify outputs.

static Iterate(listA)

Iterates the input nested list so that each sublist has the same number of members. To fill extra members, the shorter lists are iterated from their first member. For example Iterate([[1,2,3],[‘m’,’n’,’o’,’p’],[‘a’,’b’,’c’,’d’,’e’]]) yields [[1, 2, 3, 1, 2], [‘m’, ‘n’, ‘o’, ‘p’, ‘m’], [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]]

Parameters
listAlist

The input nested list.

Returns
list

The iterated list.

static MakeUnique(listA)

Forces the strings in the input list to be unique if they have duplicates.

Parameters
listAlist

The input list of strings.

Returns
list

The input list, but with each item ensured to be unique if they have duplicates.

static MaximumIndices(listA, silent: bool = False)

Returns a list of indices of the maximum value in the input list. For example, if the input list is [7,3,4,7,5,7] then the returned list is [0,3,5] to indicate the indices of the maximum value (7).

Parameters
listAlist

The input list.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns
list

The resulting list.

static MergeByThreshold(listA, threshold=0.0001)

Merges the numbers in the input list so that numbers within the input threshold are averaged into one number.

Parameters
listAlist

The input nested list.

thresholdfloat , optional

The desired merge threshold value. Default is 0.0001.

Returns
list

The merged list. The list is sorted in ascending numeric order.

static MinimumIndices(listA, silent: bool = False)

Returns a list of indices of the minimum value in the input list. For example, if the input list is [1,3,4,1,5,1] then the returned list is [0,3,5] to indicate the indices of the minimum value (1).

Parameters
listAlist

The input list.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns
list

The resulting list.

static Normalize(listA, mantissa: int = 6)

Normalizes the input list so that it is in the range 0 to 1

Parameters
listAlist

The input nested list.

mantissaint , optional

The number of decimal places to round the result to. Default is 6.

Returns
list

The normalized list.

static Position(item, listA)

Returns the position of the item in the list or the position it would have been inserts. item is assumed to be numeric. listA is assumed to contain only numeric values and sorted from lowest to highest value.

Parameters
itemint or float

The input number to be positioned.

listAlist

The input sorted list.

Returns
int

The position of the item within the list.

static RemoveEven(listA)

Removes the even indexed members of the input list.

Parameters
listAlist

The input list.

Returns
list

The resulting list.

static RemoveOdd(listA)

Removes the odd indexed members of the input list.

Parameters
listAlist

The input list.

Returns
list

The resulting list.

static Repeat(listA)

Repeats the input nested list so that each sublist has the same number of members. To fill extra members, the last item in the shorter lists are repeated and appended. For example Iterate([[1,2,3],[‘m’,’n’,’o’,’p’],[‘a’,’b’,’c’,’d’,’e’]]) yields [[1, 2, 3, 3, 3], [‘m’, ‘n’, ‘o’, ‘p’, ‘p’], [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]]

Parameters
listAlist

The input nested list.

Returns
list

The repeated list.

static Sort(listA, *otherLists, reverseFlags=None, silent: bool = False)

Sorts the first input list according to the values in the subsequent input lists in order. For example, your first list can be a list of topologies and the next set of lists can be their volume, surface area, and z level. The list of topologies will then be sorted first by volume, then by surface, and lastly by z level. You can choose to reverse the order of sorting by including a list of TRUE/FALSE values in the reverseFlags input parameter. For example, if you wish to sort the volume in reverse order (from large to small), but sort the other parameters normally, you would include the following list for reverseFlag: [True, False, False].

Parameters
listAlist

The first input list to be sorts

*otherListsany number of lists to use for sorting listA, optional.

Any number of lists that are used to sort the listA input parameter. The order of these input parameters determines the order of sorting (from left to right). If no lists are included, the input list will be sorted as is.

reverseFlagslist, optional.

The list of booleans (TRUE/FALSE) to indicated if sorting based on a particular list should be conducted in reverse order. The length of the reverseFlags list should match the number of the lists in the input otherLists parameter. If set to None, a default list of FALSE values is created to match the number of the lists in the input otherLists parameter. The default is None.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns
list

The sorted list.

static Transpose(listA)

Transposes the input list (swaps rows and columns).

Parameters
listAlist

The input list.

Returns
list

The transposed list.

static Trim(listA)

Trims the input nested list so that each sublist has the same number of members. All lists are trimmed to match the length of the shortest list. For example Trim([[1,2,3],[‘m’,’n’,’o’,’p’],[‘a’,’b’,’c’,’d’,’e’]]) yields [[1, 2, 3], [‘m’, ‘n’, ‘o’], [‘a’, ‘b’, ‘c’]]

Parameters
listAlist

The input nested list.

Returns
list

The repeated list.

static Version(check: bool = True, silent: bool = False)

Returns the current version of the software.

Parameters
checkbool , optional

if set to True, the version number is checked with the latest version on PyPi. Default is True.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns
str

The current version of the software. Optionally, includes a check with PyPi.