【问题标题】:IndexError: index 4 is out of bounds for axis 0 with size 4IndexError:索引 4 超出轴 0 的范围,大小为 4
【发布时间】:2021-02-18 20:53:03
【问题描述】:

嘿,我在尝试合成事件时遇到此索引错误,但我的索引从 0 而不是 1 开始,并且尝试执行许多操作,例如尝试 .append[i+1] 我无法修复我遇到的这个错误。

这行代码显然有问题:dset_IDX[offset:offset_next] = event_id[file_indices]

虽然 .py 文件有超过 1000 行代码,所以我无法显示所有代码,但我能够显示给我错误的函数部分

def count_events(files):                                                           
    # Because we want to remove events with 0 hits,                                
    # we need to count the events beforehand (to create the h5 file).              
    # This function counts and indexes the events with more than 0 hits.           
    # Files need to be iterated in the same order to use the indexes.              
    """ This is where we manually specify the file"""                              
    num_events = 0                                                                 
    nonzero_file_events = []                                                       
    for file_index, f in enumerate(files):                                         
        data = np.load(f, allow_pickle=True)                                       
        nonzero_file_events.append([])                                             
        hits = data['digi_hit_pmt']                                                
        for i in range(len(hits)):                                                 
            if len(hits[i]) != 0:                                                  
                nonzero_file_events[file_index].append(i)                          
                num_events += 1                                                    
    return num_events, nonzero_file_events                                         
                                                                                   
                                                                                   
def GenMapping(csv_file):                                                          
    mPMT_to_index = {}                                                             
    with open(csv_file) as f:                                                      
        rows = f.readline().split(",")[1:]                                         
        rows = [int(r.strip()) for r in rows]                                      
                                                                                   
        for line in f:                                                             
            line_split = line.split(",")                                           
            col = int(line_split[0].strip())                                       
            for row, value in zip(rows, line_split[1:]):                           
                value = value.strip()                                              
                if value:  # If the value is not empty                             
                    mPMT_to_index[int(value)] = [col, row]                         
    npmap = np.zeros((max(mPMT_to_index) + 1, 2), dtype=np.int)                    
    for k, v in mPMT_to_index.items():                                             
        npmap[k] = v                                                               
    return npmap                                                                   
                                                                                   
def GenerateMultiMuonSample_h5(avg_mu_per_ev=2.5, sigma_time_offset=21.2):
    """                                                                                                       
    Inputs:                                                                                                   
     avg_mu_per_ev == Poisson distribution mean for number of muons in each spill                             
     sigma_time_offset == Width of spill (Gaussian) in nanoseconds                                            
    """
    files = ['event998.npz']

    # Remove whitespace                                                                                       
    files = [x.strip() for x in files]

    # Check that files were provided                                                                          
    if len(files) == 0:
        raise ValueError("No files provided!!")
    print("Merging " + str(len(files)) + " files")

    # Start merging                                                                                           

    num_nonzero_events, nonzero_event_indexes = count_events(files)
    print(num_nonzero_events)

    # np.random.poisson( avg_mu_per_ev, number_of_throws )                                                    
    num_muons = np.random.poisson(avg_mu_per_ev, num_nonzero_events - 2954)

    # creates h5 file to generate the h5 file                                                                  

    dtype_events = np.dtype(np.float32)
    dtype_labels = np.dtype(np.int32)
    dtype_energies = np.dtype(np.float32)
    dtype_positions = np.dtype(np.float32)
    dtype_IDX = np.dtype(np.int32)
    dtype_PATHS = h5py.special_dtype(vlen=str)
    dtype_angles = np.dtype(np.float32)
    # sets h5 file to be written                                                                               
    h5_file = h5py.File('multimuonfile(2).h5', 'w')
    dset_event_data = h5_file.create_dataset("event_data",
                                             shape=(num_nonzero_events,) + IMAGE_SHAPE,
                                             dtype=dtype_events)
    dset_labels = h5_file.create_dataset("labels",
                                         shape=(num_nonzero_events,),
                                         dtype=dtype_labels)
    dset_energies = h5_file.create_dataset("energies",
                                           shape=(num_nonzero_events, 1),
                                           dtype=dtype_energies)
    dset_positions = h5_file.create_dataset("positions",
                                            shape=(num_nonzero_events, 1, 3),
                                            dtype=dtype_positions)
    dset_IDX = h5_file.create_dataset("event_ids",
                                      shape=(num_nonzero_events,),
                                      dtype=dtype_IDX)
    dset_PATHS = h5_file.create_dataset("root_files",
                                        shape=(num_nonzero_events,),
                                        dtype=dtype_PATHS)
    dset_angles = h5_file.create_dataset("angles",
                                         shape=(num_nonzero_events, 2),
                                         dtype=dtype_angles)

    # 22 -> gamma, 11 -> electron, 13 -> muon                                                                 
    # corresponds to labelling used in CNN with only barrel                                                   
    # IWCDmPMT_4pi_full_tank_gamma_E0to1000MeV_unif-pos-R371-y521cm_4pi-dir_3000evts_329.npz has an event     
    # with pid 11 though....                                                                                  
    # pid_to_label = {22:0, 11:1, 13:2}                                                                       

    offset = 0
    offset_next = 0
    mPMT_to_index = GenMapping(PMT_LABELS)
    # Loop over files                                                                                         
    for file_index, filename in enumerate(files):
        data = np.load(filename, allow_pickle=True)
        nonzero_events_in_file = len(nonzero_event_indexes[file_index])
        x_data = np.zeros((nonzero_events_in_file,) + IMAGE_SHAPE,
                          dtype=dtype_events)
        digi_hit_pmt = data['digi_hit_pmt']
        # digi_hit_charge = data['digi_hit_charge']                                                           
        # digi_hit_time = data['digi_hit_time']                                                               
        # digi_hit_trigger = data['digi_hit_trigger']                                                         
        # trigger_time = data['trigger_time']                                                                 
        delay = 0
        # Loop over events in file                                                                            
        # Loop over number of muons in each event                                                             
        event_id = np.array([], dtype=np.int32)
        root_file = np.array([], dtype=np.str)
        pid = np.array([])
        position = np.array([])
        direction = np.array([])
        energy = np.array([])
        labels = np.array([])

        # with open("ResultFile.txt", "w") as text_file:                                                       
        # sys.stdout = open("Result2.txt", "w")                                                                

        for i, nmu in enumerate(num_muons):
            # np.savetxt(text_file, i, nmu,fmt="%d")                                                           
            # text_file.write("processing output entry " + str(i) + " with " + nmu + " muons")                 
            print("processing output entry ", i, " with ", nmu, " muons")
            indices = np.random.randint(0, len(digi_hit_pmt), max(1, nmu))
            time_offs = [0.]
            if nmu > 1:
                time_offs = np.append(time_offs, np.random.normal(0., sigma_time_offset, nmu - 1))
            hit_pmts, charge, time = SumEvents(indices, time_offs, data, nmu == 0)
            hit_mpmts = hit_pmts // 19
            pmt_channels = hit_pmts % 19
            rows = mPMT_to_index[hit_mpmts, 0]
            cols = mPMT_to_index[hit_mpmts, 1]
            x_data[i - delay, rows, cols, pmt_channels] = charge
            x_data[i - delay, rows, cols, pmt_channels + 19] = time

            # fix below!!!                                                                                    
            idx0 = indices[0]
            event_id = np.append(event_id, data['event_id'][idx0])
            root_file = np.append(root_file, data['root_file'][idx0])
            pid = np.append(pid, data['pid'][idx0])
            position = np.append(position, data['position'][idx0])
            direction = np.append(direction, data['direction'][idx0])
            energy = np.append(energy, np.sum(data['energy'][indices]))
            labels = np.append(labels, nmu)

        offset_next += nonzero_events_in_file

        file_indices = nonzero_event_indexes[file_index]

        dset_IDX[offset:offset_next] = event_id[file_indices]
        dset_PATHS[offset:offset_next] = root_file[file_indices]
        dset_energies[offset:offset_next, :] = energy[file_indices].reshape(-1, 1)
        dset_positions[offset:offset_next, :, :] = position[file_indices].reshape(-1, 1, 3)
        dset_labels[offset:offset_next] = labels[file_indices]
        print(event_id)
        direction = direction[file_indices]
        polar = np.arccos(direction[:, 1])
        azimuth = np.arctan2(direction[:, 2], direction[:, 0])
        dset_angles[offset:offset_next, :] = np.hstack((polar.reshape(-1, 1), azimuth.reshape(-1, 1)))
        dset_event_data[offset:offset_next, :] = x_data

        offset = offset_next                                         
        print("Finished file: {}".format(filename))                  
                                                                     
    #sys.stdout.close()                                              
    print("Saving")                                                  
    #h5_file.close()                                                 
    print("Finished")                                                
                                                                     
                                                                     
# In[ ]:                                                             
                                                                     
                                                                     
GenerateMultiMuonSample_h5(avg_mu_per_ev=2.5, sigma_time_offset=21.2)
                                                                                                                            

追溯

Merging 1 files
2958
processing output entry  0  with  3  muons
processing output entry  1  with  1  muons
processing output entry  2  with  3  muons
processing output entry  3  with  3  muons

Traceback (most recent call last):
  File "C:/Users/abdul/OneDrive/Desktop/ISSP/ISSP-AA/TriumfCNN-AA/EventDisplay.py", line 1068, in <module>
    GenerateMultiMuonSample_h5(avg_mu_per_ev=2.5, sigma_time_offset=21.2)
  File "C:/Users/abdul/OneDrive/Desktop/ISSP/ISSP-AA/TriumfCNN-AA/EventDisplay.py", line 1044, in GenerateMultiMuonSample_h5
    dset_IDX[offset:offset_next] = event_id[file_indices]
IndexError: index 4 is out of bounds for axis 0 with size 4

【问题讨论】:

  • 请将错误堆栈跟踪添加到您的问题中
  • 帮助我们帮助您。尝试尽可能多地删除不必要的代码,并包括缺失的部分,以便我们了解发生了什么。例如,我看到违规行使用了未在提供的代码中描述的dset_IDXoffset,加上offset_next,它使用了此处未定义的nonzero_events_in_file。至少,您应该描述这些变量是什么。我们在这里不是读心者(尽管那样有用)。
  • stacktrace 提到了第 1044 行,但由于我们不知道行号,因此很难知道是哪一行。如果您只包括导致错误的行,可能会更容易为您提供帮助,并且可能会在其周围加上一些行。
  • @vasia 代码行显示在堆栈跟踪中,错误代码在第 2 段中。

标签: python numpy index-error


【解决方案1】:

提供的信息不多,但我理解的是, 该错误表明轴 0 的大小 = 4,并且您正在尝试访问索引 4,这对于大小 4 是不可能的,因为它以 0 开头并且最大索引可能是 3。

【讨论】:

  • 是的,这是正确的,对不起,我是 python 新手,这是我正在处理的任务。
  • 我认为问题出在这里,file_indices = nonzero_event_indexes[file_index] file_indices 像 event_id[4][x] 一样进入 event_id,您还没有发布 file_indices 的值,所以我们不知道大小是多少以及为什么它会超出 4.
  • 所以事件来自一个 npz 文件,该文件有 3000 多个事件,我只是这样做了,目前它只迭代最多 4 个事件来测试我的代码。但是,在给定的时刻,索引从 0 开始,我试图让它从 1 开始
  • python 中的索引从 0 开始,在列表/数组之前插入 0 或其他内容以保留数据并忽略第 0 个索引。或在将索引传递给 event_id 时从索引中减去 1,因为默认情况下列表将从 0 而不是 1 开始。这里另一种方式 [link] stackoverflow.com/questions/11726866/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-16
  • 2020-10-28
  • 2020-04-24
  • 2016-07-29
  • 2021-07-17
  • 2021-05-24
  • 2018-07-23
相关资源
最近更新 更多