Search code examples
pythonnumpy-ndarray

Concatenate a sub part of a numpy 5 d array


I have a 5 d Array with shape of (2, 6, 6, 2, 1) containing differnt kinds of measurements that I feed over a loop. The first dimention (2) corresponds to a physical parameter (negatif / positive pressure), the second one to a certain x position (6 position in total) and the third certain y position (6 position in total) and the two last one corresponding of the measurement of a sensor vs time (2 is the dimension for signal / time vectors) and the 1 is corresponding of the number of sample (that I don't know and could change over the iterations)

For the first while loop I have measurement matrix for one parameter (for exemple Press positive, x=0, y=0) of my experiment and I would like to feed my big matrix over the for loop. I tried to use this function :

Mesures_Press[P_pos][S_plus][P_plus]= np.concatenate((Mesures_Press[P_pos][S_plus][P_plus],Mes_Press[0:2,:]),axis=1)

but this is failing because I concatenate only a sub part of my array, yielding this error : could not broadcast input array from shape (2,102) into shape (2,1)

Do you have any idea how should I feed my matrix over each step of my while loop ?

    # -*- coding: utf-8 -*-
"""
Created on Fri Dec 11 00:56:26 2020

@author: labodezao
"""
import sys
import time
from rshell import main
from rshell import pyboard
import pytta
import numpy as np
import time
import matplotlib.pylab as plt
import os



#device = pytta.get_device_from_user()

measurementParams = {
    'lengthDomain': 'time',
    'timeLength': 1,
    'samplingRate': 48000,
        'freqMin': 20,
        'freqMax': 20000,
        'device': 1,
        'inChannels': [1, 2],
        'comment': 'Testing; 1, 2.'
}

msr = pytta.generate.measurement('rec',
                                     **measurementParams)
path = 'data\\'
dir = os.path.dirname(path)
print(os.path.isdir(path))
if not os.path.isdir(path):
    os.mkdir(path)       

pyb = pyboard.Pyboard('COM4')
pyb.enter_raw_repl()
pyb.exec("from MesAnche import *")
pyb.exec("mes = MesAnches()")

Decoupe_Sections = 6
Decoupe_Pressions = 6
deplacement_screw_S_mm =15


#I2c : Pression débit température et surface
Mesures_Press= np.empty((2,Decoupe_Sections, Decoupe_Pressions,2,1))
MesuresTemperature=np.empty((2,Decoupe_Sections, Decoupe_Pressions,2,1))
Mesures_Debit=np.empty((2,Decoupe_Sections, Decoupe_Pressions,2,1))
Calculs_Surf=np.empty((2,Decoupe_Sections, Decoupe_Pressions,2,1))

#pytta
Mesures_Press_Acoustique=np.empty((2,Decoupe_Sections, Decoupe_Pressions,2,1))
Mesures_Accelerations = np.empty((2,Decoupe_Sections, Decoupe_Pressions,2,1))

#calculs
Mesures_Press_Pos=np.empty((2,Decoupe_Sections, Decoupe_Pressions,2,1))





#init script
Mes_bytes =0


try:
    # Boucle des pression positives
    
    for P_pos in range(2):
        print("Pressions Positive 1-Oui, 0-non : ", P_pos)
    
    # Boucle des sections
        pyb.exec("mes.Tempo_Vanne(1)")
        for S_plus in range(Decoupe_Sections):
            print("Section courante", S_plus)
            comm_s = f"mes.Move_to_Pos({(S_plus+1)*deplacement_screw_S_mm/Decoupe_Sections},500)"
            pyb.exec(comm_s)
            
    # Boucle des incréments de pression  
    
            for P_plus in range(Decoupe_Pressions):
                print("Pression courante", P_plus)
                # On as fixé une surface et on règle on pression :
                #on règle le ventilo
                comm_p=f"mes.Set_Press({int(4096/(6-P_plus))})" 
                pyb.exec(comm_p)
    
                #on ferme la vanne et on enclanche une tempo de 1 sec pui on l'ouvre et on fait les mesure dans la fonction Mesures_Full
                comm_mesure ="mes.Mes_Full_Mes()"
                pyb.exec_raw_no_follow(comm_mesure)
                #On démarre l'acquision de X sec
                med1 = msr.run()
                time.sleep(1)
                comm_grab ="print(mes.Grab_Full_Mes())"
                Mes_bytes_unformatted = pyb.exec(comm_grab)
                Mes_bytes = eval(str(Mes_bytes_unformatted, "ascii"))
    
                Mes_Press =np.frombuffer(Mes_bytes, dtype=np.float32)
                Mes_Press=Mes_Press.reshape(6,int(Mes_Press.size/6))
                
                #On récupère la valeur de la Surface
    
                Calc_Surf = float(pyb.exec("print(mes.Calc_Surf())"))
                print(Calc_Surf)
    
                #On sauvegarde les mesures
                name = f'PPos{P_pos}Sec{S_plus}PPlus{P_plus}'
                filename = f'{path}{name}'
                pytta.save(filename, med1)
                #h5f.create_dataset('dataset_1', data=a)
                
                print(S_plus,P_plus)
                Mesures_Press[P_pos][S_plus][P_plus]= np.concatenate((Mesures_Press[P_pos][S_plus][P_plus],Mes_Press[0:2,:]),axis=1)
                MesuresTemperature=np.append(MesuresTemperature,Mes_Press[2:3,:],axis=0)
                Mesures_Debit=np.append(Mesures_Debit,Mes_Press[4:5,:],axis=0)
                Mesures_Press_Pos=np.append(Mesures_Press_Pos,float((S_plus+1)*deplacement_screw_S_mm/Decoupe_Sections),axis=0)                             #1
                Calculs_Surf=np.append(Calculs_Surf,Calc_Surf,axis=0)                      #5     
                Mesures_Accelerations=np.append(Mesures_Accelerations,[med1.timeVector,med1.timeSignal[:,0]],axis=0)    #L est l'accéléromètre
                Mesures_Press_Acoustique=np.append(Mesures_Press_Acoustique,[med1.timeVector,med1.timeSignal[:,1]],axis=0)    #R est le microphone
            
            
            #Mesures_Press= np.append(Mesures_Press,Temp_Mesures_Press,axis=1)
            
            print(Mesures_Press_Pos)
            print(Calculs_Surf)
            print(Mesures_Accelerations)
            plt.plot(Mesures_Press_Acoustique[0][0][0][0],Mesures_Press_Acoustique[0][0][0][1]) # du type mat[pression_pos][Pos_section][Val_Pression][]
              
except Exception as e:
    exc_type, exc_obj, exc_tb = sys.exc_info()
    fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
    print("Error :",fname, exc_tb.tb_lineno,e)
    pass

pyb.exit_raw_repl()
pyb.close()

Solution

  • Okay. So, the problem is the size mismatch which occurs because np expects us to have values which fill all the dimensions. A quick fix is making the values in your 1st-3rd dimensions, for Mes_Press as 0 (Default).

    A sample code is as follows:

    import numpy as np
    
    P_pos = 0
    S_plus = 0
    P_plus = 0
    # Add some default values to test the code.
    Mesures_Press = np.ones((2, 6, 6, 2, 1))
    Mes_Press = 3*np.ones((2, 10))
    ############################################
    # Get the number of samples to be added.
    m = Mes_Press[0:2,:].shape[1]
    
    # Create a temporary variable with zeros of 
    # size (2, 6, 6, 2, #numsamples)
    temp =  np.zeros((2, 6, 6, 2, m))
    # Assign the values.
    temp[:, :, :, 0:2, 0:m] = Mes_Press[0:2,:]
    
    # Now append to the main array
    Mesures_Press = np.append(Mesures_Press, temp, axis=4)
    
    # Check the size
    print(Mesures_Press.shape)
    # (2, 6, 6, 2, 11)