IndexError:索引4超出了尺寸为4的轴0的范围

问题描述

嘿,我遇到这个索引错误,我尝试合成事件,但是我的索引从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",),dtype=dtype_labels)
    dset_energies = h5_file.create_dataset("energies",1),dtype=dtype_energies)
    dset_positions = h5_file.create_dataset("positions",1,3),dtype=dtype_positions)
    dset_IDX = h5_file.create_dataset("event_ids",dtype=dtype_IDX)
    dset_PATHS = h5_file.create_dataset("root_files",dtype=dtype_PATHS)
    dset_angles = h5_file.create_dataset("angles",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,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 "," with "," 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,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,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,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

解决方法

没有提供太多信息,但是我已经了解了, 该错误表明轴0的大小为4,并且您尝试访问索引4,而大小4则不可能,因为它以0开头,最大索引可能为3。