mirix commited on
Commit
b0de7f5
1 Parent(s): 2f5721d

Upload parquet2csv_wav.py

Browse files
Files changed (1) hide show
  1. parquet2csv_wav.py +12 -8
parquet2csv_wav.py CHANGED
@@ -16,6 +16,10 @@ n_cores = str(os.cpu_count())
16
  os.environ['OMP_NUM_THREADS'] = n_cores
17
  os.environ['MKL_NUM_THREADS'] = n_cores
18
 
 
 
 
 
19
  # Create the wavs dir if it does not exist
20
  if not os.path.isdir('wavs'):
21
  os.makedirs('wavs')
@@ -23,13 +27,13 @@ if not os.path.isdir('wavs'):
23
  # All columns from the parquet file except the one with the audio numpy arrays (it is huge)
24
  columns = ['ytid', 'ytid_seg', 'start', 'end', 'sentiment', 'happiness', 'sadness', 'anger', 'fear', 'disgust', 'surprise']
25
 
26
- ### Add replace line here to change the paths from the ytid_seg column ###
27
- # https://pola-rs.github.io/polars/py-polars/html/reference/expressions/api/polars.Expr.str.replace.html
28
- # replace the subtring '/home/emoman/Downloads/mosei/samples' with the actual path
29
-
30
  # Read the parquet file with polars
31
  df = pl.read_parquet('sqe_messai.parquet', columns = columns)
32
 
 
 
 
 
33
  # Export the csv file (excluding the last column)
34
  df.write_csv('sqe_messai_nowav.csv')
35
  print(df)
@@ -40,15 +44,15 @@ columns2 = ['ytid_seg', 'wav2numpy']
40
  # Read the parquet file with polars (this will take a while)
41
  df2 = pl.read_parquet('sqe_messai.parquet', use_pyarrow=False, columns = columns2)
42
 
43
- ### Add replace line here to change the paths from the ytid_seg column ###
44
- # https://pola-rs.github.io/polars/py-polars/html/reference/expressions/api/polars.Expr.str.replace.html
45
- # replace the subtring '/home/emoman/Downloads/mosei/samples' with the actual path
46
 
47
  # Function to convert the numpy arrays to wav files stored in the wavs folders
48
  def numpy2wav(row):
49
  segment = os.path.splitext(os.path.basename(os.path.normpath(row[0])))[0]
50
  print('PROCESSED:', segment)
51
- write('wavs/' + segment + '.wav', 16000, np.array(row[1]))
52
  return segment
53
 
54
  # Apply the function (this will take a while)
 
16
  os.environ['OMP_NUM_THREADS'] = n_cores
17
  os.environ['MKL_NUM_THREADS'] = n_cores
18
 
19
+ # Define directory to store the samples
20
+ cwd = os.getcwd()
21
+ sample_dir = str(cwd) + '/wavs/'
22
+
23
  # Create the wavs dir if it does not exist
24
  if not os.path.isdir('wavs'):
25
  os.makedirs('wavs')
 
27
  # All columns from the parquet file except the one with the audio numpy arrays (it is huge)
28
  columns = ['ytid', 'ytid_seg', 'start', 'end', 'sentiment', 'happiness', 'sadness', 'anger', 'fear', 'disgust', 'surprise']
29
 
 
 
 
 
30
  # Read the parquet file with polars
31
  df = pl.read_parquet('sqe_messai.parquet', columns = columns)
32
 
33
+ # Replace the generic path with the actual path
34
+ bad_dir = df.row(0)[1].rsplit('/', 1)[0] + '/'
35
+ df = df.with_columns(pl.col('ytid_seg').str.replace_all(bad_dir, sample_dir))
36
+
37
  # Export the csv file (excluding the last column)
38
  df.write_csv('sqe_messai_nowav.csv')
39
  print(df)
 
44
  # Read the parquet file with polars (this will take a while)
45
  df2 = pl.read_parquet('sqe_messai.parquet', use_pyarrow=False, columns = columns2)
46
 
47
+ # Replace the generic path with the actual path
48
+ bad_dir = df2.row(0)[0].rsplit('/', 1)[0] + '/'
49
+ df2 = df2.with_columns(pl.col('ytid_seg').str.replace_all(bad_dir, sample_dir))
50
 
51
  # Function to convert the numpy arrays to wav files stored in the wavs folders
52
  def numpy2wav(row):
53
  segment = os.path.splitext(os.path.basename(os.path.normpath(row[0])))[0]
54
  print('PROCESSED:', segment)
55
+ write(sample_dir + segment + '.wav', 16000, np.array(row[1]))
56
  return segment
57
 
58
  # Apply the function (this will take a while)