Python Forum
Create a 2-channel numpy file from two cvs file containing a 9x9 matrix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create a 2-channel numpy file from two cvs file containing a 9x9 matrix
#1
Hello everyone, I work in a CNN project for my thesis and I have problem with my Python code. I hope that somebody can help me. Thank you.

I would like to take two .cvs files with the same name (each is a 9x9 matrix) from a folder and create a unique file of the size 9x9x2 and save it in another folder.

This is my code, right now the problem is that the final file has been saved as a 9x18 instead of 9x9x2.

import os, glob
from collections import defaultdict

dirs = ['/content/drive/MyDrive/Colab Notebooks/Nuova cartella/mu1/real', '/content/drive/MyDrive/Colab Notebooks/Nuova cartella/mu1/imag']
file_pattrn = r'*.csv'
unique_files = defaultdict(list)

for d in dirs:
  for i in glob.iglob(os.path.join(d, file_pattrn)):
     unique_files[os.path.basename(i)].append(i)

destination = '/content/drive/MyDrive/Colab Notebooks/New_Dataset'
for unique_filename, copies in unique_files.items():
  with open(os.path.join(destination, unique_filename), 'w') as f:
      for copy in copies:
          with open(copy, 'r') as cp:
            for line in cp:
                f.write(line)
Reply
#2
Why not something like (untested)
import numpy as np
for unique_filename, copies in unique_files.items():
    a = np.array([np.loadtxt(copy) for copy in copies])
    np.savetxt(os.path.join(destination, unique_filename), a)
Well actually, the above code doesn't work because numpy.savetxt() cannot handle 3D arrays. You can save the 3D array under binary format by using numpy.save() with the allow_pickle=True option. Here is an example with 3x3 arrays
import numpy as np
import io

file1 = io.StringIO("""\
1.000000000000000000e+00 2.000000000000000000e+00 3.000000000000000000e+00
5.000000000000000000e+00 6.000000000000000000e+00 7.000000000000000000e+00
9.000000000000000000e+00 1.000000000000000000e+01 1.100000000000000000e+01
""")

file2 = io.StringIO("""\
-1.000000000000000000e+00 -2.000000000000000000e+00 -3.000000000000000000e+00
-5.000000000000000000e+00 -6.000000000000000000e+00 -7.000000000000000000e+00
-9.000000000000000000e+00 -1.000000000000000000e+01 -1.100000000000000000e+01
""")

out = io.BytesIO()
a = np.array([np.loadtxt(f) for f in (file1, file2)])
a = np.moveaxis(a, 0, -1)
np.save(out, a, allow_pickle=True)
out.seek(0)
print(np.load(out, allow_pickle=True))
print(a.shape)
Output:
[[[ 1. -1.] [ 2. -2.] [ 3. -3.]] [[ 5. -5.] [ 6. -6.] [ 7. -7.]] [[ 9. -9.] [ 10. -10.] [ 11. -11.]]] (3, 3, 2)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create Choices from .ods file columns cspower 3 578 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  file open "file not found error" shanoger 8 1,082 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Recommended way to read/create PDF file? Winfried 3 2,864 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  Use PM4PY and create working file thomaskissas33 0 640 Nov-14-2023, 06:53 AM
Last Post: thomaskissas33
  Create csv file with 4 columns for process mining thomaskissas33 3 741 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  Need to replace a string with a file (HTML file) tester_V 1 754 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 918 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Check if two matrix are equal and of not add the matrix to the list quest 3 817 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  create exe file for linux? korenron 2 962 Mar-22-2023, 01:42 PM
Last Post: korenron
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,088 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020