You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG: model_builder:: get_indices_block, get_indices_sphere return value inconsistency #1606
According to their docstrings, both get_indices_block and get_indices_sphere should return a tuple object. On the other hand, they are returning a tuple and a numpy ndarray respectively. To make it consistent with the documentation, get_indices_sphere is the one that must be corrected.
Reproducible code example:
fromdiscretizeimportTensorMeshfromsimpeg.utilsimportmodel_builderimportnumpyasnpncx=10# number of core mesh cells in xncy=15# number of core mesh cells in ydx=15# base cell width xdy=10# base cell width yhx=dx*np.ones(ncx)
hy=dy*np.ones(ncy)
x0=0y0=-150mesh=TensorMesh([hx, hy], x0=[x0, y0])
idx1=model_builder.get_indices_block(
np.r_[60, 60], np.r_[10, 10], mesh.gridCC
)
idx2=model_builder.get_indices_sphere(
np.r_[60, 60], 10, mesh.gridCC
)
print(type(idx1), type(idx2))
Error message:
<class 'tuple'><class 'numpy.ndarray'>
Runtime information:
Date: Wed Feb 05 16:42:46 2025 Hora oficial do Brasil
Thanks @ghwilliams for opening this issue. I can reproduce your findings, and yes, this seems to be a bug.
I think there are other important differences between the two functions that we should also take care of. For example, I tweaked your example by changing the coordinates of the block, to see the outputs of each one of these two functions:
importnumpyasnpfromdiscretizeimportTensorMeshfromsimpeg.utilsimportmodel_builderncx=10# number of core mesh cells in xncy=15# number of core mesh cells in ydx=15# base cell width xdy=10# base cell width yhx=dx*np.ones(ncx)
hy=dy*np.ones(ncy)
x0=0y0=-150mesh=TensorMesh([hx, hy], x0=[x0, y0])
idx1=model_builder.get_indices_block(
np.r_[60, -60], np.r_[10, -10], mesh.gridCC
)
idx2=model_builder.get_indices_sphere(
np.r_[60, -60], 20, mesh.gridCC
)
print(type(idx1), type(idx2))
print(f"{idx1=}")
print(f"{idx2=}")
get_indices_block returns a tuple of arrays (not a tuple of ints) with the indices of the cells that belong to the given block.
get_indices_sphere returns an array of bools that we can use to index the cells that belong to the sphere.
I think there are a couple of things we can improve:
I think it's better to return arrays instead of tuples of ints. The outputs of these functions are ultimately going to be used to index other arrays, so returning tuples will force us to cast them as arrays at some point.
From what I understand, the outputs of these functions are always going to be related to a single block or sphere, so they can just return a single array (no need to return a tuple of arrays).
I think we should standardize the type of the outputs: should it be an array of integers for the cells, or array of bools?
[I think we should standardize the type of the outputs: should it be an array of integers for the cells, or array of bools?]
I'd vote for "an array of integers".
Cool. The good thing about an array of integers is that we could probably reduce the memory footprint of those arrays: if a block is composed by a dozen of cells, then the array will be much smaller compared with an array of bools that have possibly millions of elements (one for each cell in the mesh).
Nonetheless, the drawback of array of integers is that they could be used to index "the wrong array" as long as the maximum index is not greater than the size of the "wrong array". For example, let's say I have two arrays a and b of different size:
Activity
santisoler commentedon Feb 12, 2025
Thanks @ghwilliams for opening this issue. I can reproduce your findings, and yes, this seems to be a bug.
I think there are other important differences between the two functions that we should also take care of. For example, I tweaked your example by changing the coordinates of the block, to see the outputs of each one of these two functions:
And I get the following:
get_indices_blockreturns a tuple of arrays (not a tuple of ints) with the indices of the cells that belong to the given block.get_indices_spherereturns an array of bools that we can use to index the cells that belong to the sphere.I think there are a couple of things we can improve:
ghwilliams commentedon Feb 13, 2025
[I think we should standardize the type of the outputs: should it be an array of integers for the cells, or array of bools?]
I'd vote for "an array of integers".
santisoler commentedon Feb 13, 2025
Cool. The good thing about an array of integers is that we could probably reduce the memory footprint of those arrays: if a block is composed by a dozen of cells, then the array will be much smaller compared with an array of bools that have possibly millions of elements (one for each cell in the mesh).
Nonetheless, the drawback of array of integers is that they could be used to index "the wrong array" as long as the maximum index is not greater than the size of the "wrong array". For example, let's say I have two arrays
aandbof different size:And then I create an array of integers for the indices of elements of
athat are even:I can use it to index
a:But I also can use it to index
bwithout erroring out as long as the maximum number inindicesis lower than thelen(b):If I use a mask of bools to index a subset of
a, then I can never use it forbbecause I'll get an error due to inconsistent size.That's why I was considering moving to an array of bools instead. But I'm open to hear opinions on that.
Fix return of `get_indices_block`