Home > other >  Stack 1D numpy arrays by a for loop
Stack 1D numpy arrays by a for loop

Time:07-26

I am reading 1D arrays from the .txt files in a folder and I want to put all of them in a 2D array. (each 1D array should be a row in the new 2D array). Each 1D array in this case has 30 elements. Before this, I was reading the txt files one by one and name them array1, array2 and ... then by using np.vstack, I could do the job. The final result is an intensity graph like this that each row is a 1D array. But, now I have 296 txt files and I should do it by a loop. I have made this code but it doesn't work (I have tried different things like vstack, stack, append, concatenate and the last one was creating an empty 2D array B and put the 1D arrays one by one but none of them work correctly, yet):

import os
  
# Folder Path
path = "C:\\2022-07-25 second result more iteration"
 
# Change the directory
os.chdir(path)

B = np.empty([296, 30], dtype=float)
 
 # iterate through all file
for file in os.listdir():
    # Check whether file is in text format or not
    if file.endswith(".txt"):
        file_path = f"{path}\{file}"
  
        
        # open the files
        A = open(f"{path}\{file}", "r")
        A = np.loadtxt(A, delimiter="\t")
        A = np.asarray(A)
        A = A[:, 0] # because of some reasons the txt files have two similar columns that I just take one of them
        A = np.subtract(A, n) # just some math (n is another 1D array same size as A)
        A = np.divide(A, n) # just some math
        
        #A = A.T
        #print("A = ", A)
        #B = np.vstack (A)
        #B = np.stack(A)
        #B = np.stack([A])
        #B = np.concatenate([A])
        # B = np.append(B, A, axis=0)
        # print("B in loop = ", B)
        B[i] = A
     
             
print("B= ", B)

im=plt.imshow(B, origin='lower', extent=[650, 850, 1, 296], aspect='auto', cmap=cm.seismic,  norm=colors.CenteredNorm(), interpolation='None')

This is one of the txt files (that I take just one of the columns, both columns are similar):

6563.64300386213    6563.64300386213
7627.5296220466 7627.5296220466
8922.36588941225    8922.36588941225
10515.1799846774    10515.1799846774
12493.117633046 12493.117633046
14970.0858566   14970.0858566
18090.6895050039    18090.6895050039
22026.1642649415    22026.1642649415
26946.5638244996    26946.5638244996
32926.5439947365    32926.5439947365
39699.096077692 39699.096077692
46279.3014208503    46279.3014208503
50755.8724722489    50755.8724722489
51032.9873391785    51032.9873391785
46565.0683371707    46565.0683371707
39022.807682766 39022.807682766
30795.2702323368    30795.2702323368
23449.1094708325    23449.1094708325
17518.9650853553    17518.9650853553
12968.5269428127    12968.5269428127
9591.79370475353    9591.79370475353
7215.20723743423    7215.20723743423
5791.11928730885    5791.11928730885
5452.1471216442 5452.1471216442
6480.10226555175    6480.10226555175
8910.98447577286    8910.98447577286
11691.5414159922    11691.5414159922
13052.3996945771    13052.3996945771
12588.0161703823    12588.0161703823
11198.2182945086    11198.2182945086

Could you help me with this?

CodePudding user response:

Is that what you want to get :

import os
import numpy as np

path = 'whatever/path/folder_data'

os.chdir(path)
my_arrays = []
for file_path in os.listdir():
    if file_path.endswith(".txt"):
        with open(file_path, 'r') as file:
            my_arrays.append(np.array([float(x.split()[0]) for x in file.readlines()]))
result = np.vstack(tuple(my_arrays))
  • Related