[Python] Best practices for naming and managing NumPy arrays

Discussão em 'Python' iniciado por Stack, Fevereiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    I work in a small scientific team, and we are facing issues with naming conventions for NumPy arrays.

    For example, consider the array name rawdata_freq_dist_time. This array contains raw data (rawdata), and its dimensions are freq, dist, and time. Therefore, it is a 3D array where the first dimension represents frequencies, and so on.

    The problem with this naming convention is that it has limitations, and we are considering a cleaner approach.

    I proposed using a data structure:

    from typing import NamedTuple
    import numpy as np

    class RawData(NamedTuple):
    data: np.ndarray
    freq_size: int
    dist_size: int
    time_size: int


    However, if I instantiate the class like this:

    rawData = RawData(data=np.zeros((128, 256, 12)),
    freq_size=128,
    dist_size=256,
    time_size=12)


    and then work with this structure:

    rawData.data = ...


    I lose information about the dimensions of my array. I cannot easily tell if it is 2D, 3D, etc., unlike with the name rawdata_freq_dist_time.

    I must admit, I am a bit stuck. How do your teams handle NumPy arrays to work efficiently and keep track of dimensions without getting lost?

    Thank you for your insights.

    Continue reading...

Compartilhe esta Página