import inspect
import itertools
[docs]
def printCones(*args):
"""
Prints a message including the name of the function from which it was called.
"""
# Get the current frame in the execution stack.
# The '1' indicates that we want the frame of the caller,
# not the frame of printCones itself.
caller_frame = inspect.currentframe().f_back
# Get the name of the function from the caller's frame
caller_name = caller_frame.f_code.co_name
if args:
args_str = " ".join(str(arg) for arg in args)
message = f"{caller_name} : \n\t {args_str}"
else:
message = f"{caller_name} : \n\t"
# Print the message with the calling function's name
print(message)
[docs]
def global2Local(globalID, globalCellList):
for irank, cellList in enumerate(globalCellList):
if (any(globalID == e for e in cellList)):
return irank, cellList.index(globalID)
[docs]
def local2Global(localID, globalCellList, rank):
return globalCellList[rank][localID]
[docs]
def mergeClips(clipList, globalCellList):
"""
Given a list of conesClip, finds clippings that shares cells and merge them
:param clipList: the list of clippings
:type clipList: list
:param globalCellList: the list of global cell ids
:type globalCellList: list
:returns: the list of global cells id of merged clips
:rtype: list
"""
""" First we flat the global cell lists """
globalCells = []
for clip in clipList:
flatList = list(itertools.chain(*clip.globalCellList))
flatList.sort()
globalCells.append(flatList)
#printCones("Flatten lists", globalCells)
""" Then we merge lists if they share elements"""
element_to_list_index = {}
for i, sublist in enumerate(globalCells):
for element in sublist:
if element not in element_to_list_index:
element_to_list_index[element] = []
num_list = len(globalCells)
parent = list(range(num_list))
def find(i):
if parent[i] == i:
return i
parent[i] = find(parent[i])
return parent[i]
def union(i, j):
root_i = find(i)
root_j = find(j)
if root_i !=root_j:
parent[root_i] = root_j
return True
return False
for element, indices in element_to_list_index.items():
if len(indices) > 1:
#if a cell appears in multiple clips, merge those clips
first_index = indices[0]
for i in range(1, len(indices)):
union(first_index, indices[i])
# group lists by their representative parent
merged_groups = {}
for i in range(num_list):
root = find(i)
if root not in merged_groups:
merged_groups[root] = set()
merged_groups[root].update(globalCells[i])
result = [list(group) for group in merged_groups.values()]
for res in result:
res.sort()
#printCones("Original clips", len(clipList))
#printCones("Merged clips", len(result))
return result