Skip to content

BUG: model_builder:: get_indices_block, get_indices_sphere return value inconsistency #1606

Closed

Description

@ghwilliams

Describe the issue:

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:

from discretize import TensorMesh
from simpeg.utils import model_builder
import numpy as np

ncx = 10  # number of core mesh cells in x
ncy = 15  # number of core mesh cells in y
dx = 15  # base cell width x
dy = 10  # base cell width y
hx = dx * np.ones(ncx)
hy = dy * np.ones(ncy)

x0 = 0
y0 = -150

mesh = 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

            OS : Windows (10 10.0.26100 SP0 Multiprocessor Free)
        CPU(s) : 22
       Machine : AMD64
  Architecture : 64bit
           RAM : 31.5 GiB
   Environment : Jupyter

Python 3.11.11 | packaged by conda-forge | (main, Dec 5 2024, 14:06:23)
[MSC v.1942 64 bit (AMD64)]

        simpeg : 0.23.1.dev10+gf697d2455
    discretize : 0.11.1
   pymatsolver : 0.3.1
         numpy : 2.0.2
         scipy : 1.14.1
    matplotlib : 3.10.0
        geoana : 0.7.2
        libdlf : 0.3.0
        pydiso : 0.1.2
         numba : 0.60.0
          dask : 2024.12.1
       sklearn : 1.6.0
        pandas : 2.2.3
         sympy : 1.13.3
       IPython : 8.30.0
    ipywidgets : 8.1.5
        plotly : 5.24.1
           vtk : 9.3.1

memory_profiler : 0.61.0
choclo : 0.3.1

Context for the issue:

No response

Activity

  1. santisoler commented on Feb 12, 2025

    @santisoler
    Member

    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:

    import numpy as np
    from discretize import TensorMesh
    from simpeg.utils import model_builder
    
    ncx = 10  # number of core mesh cells in x
    ncy = 15  # number of core mesh cells in y
    dx = 15  # base cell width x
    dy = 10  # base cell width y
    hx = dx * np.ones(ncx)
    hy = dy * np.ones(ncy)
    
    x0 = 0
    y0 = -150
    
    mesh = 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=}")

    And I get the following:

    <class 'tuple'> <class 'numpy.ndarray'>
    idx1=(array([ 91,  92,  93, 101, 102, 103, 111, 112, 113, 121, 122, 123, 131,
           132, 133]),)
    idx2=array([False, False, False, False, False, False, False, False, False,
           False, False, False, False, False, False, False, False, False,
           False, False, False, False, False, False, False, False, False,
           False, False, False, False, False, False, False, False, False,
           False, False, False, False, False, False, False, False, False,
           False, False, False, False, False, False, False, False, False,
           False, False, False, False, False, False, False, False, False,
           False, False, False, False, False, False, False, False, False,
           False,  True,  True, False, False, False, False, False, False,
           False, False,  True,  True, False, False, False, False, False,
           False, False, False,  True,  True, False, False, False, False,
           False, False, False, False,  True,  True, False, False, False,
           False, False, False, False, False, False, False, False, False,
           False, False, False, False, False, False, False, False, False,
           False, False, False, False, False, False, False, False, False,
           False, False, False, False, False, False, False, False, False,
           False, False, False, False, False, False])
    
    • 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?
  2. ghwilliams commented on Feb 13, 2025

    @ghwilliams
    ContributorAuthor

    [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".

  3. santisoler commented on Feb 13, 2025

    @santisoler
    Member

    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:

    a = np.arange(20, dtype=np.float64)
    b = np.random.default_rng(seed=42).uniform(low=0, high=10, size=30)
    print(f"{a=}")
    print(f"{b=}")
    a=array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12.,
           13., 14., 15., 16., 17., 18., 19.])
    b=array([7.73956049, 4.3887844 , 8.5859792 , 6.97368029, 0.94177348,
           9.75622352, 7.61139702, 7.86064305, 1.28113633, 4.50385938,
           3.70798024, 9.26764989, 6.4386512 , 8.22761613, 4.43414199,
           2.27238722, 5.54584787, 0.63817256, 8.27631172, 6.31664399,
           7.5808774 , 3.54525968, 9.70698024, 8.93121121, 7.78383497,
           1.94638708, 4.66721004, 0.43803766, 1.54289492, 6.83048953])
    

    And then I create an array of integers for the indices of elements of a that are even:

    (indices,) = np.where(a % 2 == 0)
    indices
    indices=array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
    

    I can use it to index a:

    a[indices]

    But I also can use it to index b without erroring out as long as the maximum number in indices is lower than the len(b):

    b[indices]

    If I use a mask of bools to index a subset of a, then I can never use it for b because 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.

  4. added this to the 0.24.0 milestone on Mar 10, 2025
  5. modified the milestones: 0.24.0, 0.25.0 on Apr 24, 2025
  6. added a commit that references this issue on Oct 14, 2025
    66e45a0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions