Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update using only Python's libs. Tested in Windows and Linux #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions extract_data.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
import os
import sys
import subprocess
import numpy as np
import shutil
import glob
import json
import pandas as pd
import tarfile

'''
This script creates a folder "Extracted_data" inside which it extracts all the wav files in the directories date-wise
'''

coswara_data_dir = os.path.abspath('.') # Local Path of iiscleap/Coswara-Data Repo
extracted_data_dir = os.path.join(coswara_data_dir, 'Extracted_data')
coswara_data_dir = os.path.abspath('.') # Local Path of iiscleap/Coswara-Data Repo
extracted_data_dir = os.path.join(coswara_data_dir, 'Extracted_data')

if not os.path.exists(coswara_data_dir):
raise("Check the Coswara dataset directory!")
raise "Check the Coswara dataset directory!"

if not os.path.exists(extracted_data_dir):
os.makedirs(extracted_data_dir) # Creates the Extracted_data folder if it doesn't exist
os.makedirs(extracted_data_dir) # Creates the Extracted_data folder if it doesn't exist

dirs_extracted = set(map(os.path.basename,glob.glob('{}/202*'.format(extracted_data_dir))))
dirs_all = set(map(os.path.basename,glob.glob('{}/202*'.format(coswara_data_dir))))
dirs_extracted = set(map(os.path.basename, glob.glob(f'{extracted_data_dir}/202*')))
dirs_all = set(map(os.path.basename, glob.glob(f'{coswara_data_dir}/202*')))

dirs_to_extract = list(set(dirs_all) - dirs_extracted)
all_file_temp = os.path.join(extracted_data_dir, "temp.tar.gz")

for d in dirs_to_extract:
p = subprocess.Popen('cat {}/{}/*.tar.gz.* |tar -xvz -C {}/'.format(coswara_data_dir, d, extracted_data_dir), shell=True)
p.wait()

def extract(infile: list):
print(f'Extracting {infile[0].split(".a")[0]}')
# concatenate all the *tar.gz* files
with open(all_file_temp, 'wb') as wfp:
infile.sort()
for fn in infile:
with open(fn, 'rb') as rfp:
shutil.copyfileobj(rfp, wfp)

# extract the all-in-one file
tar = tarfile.open(all_file_temp, "r:gz")
tar.extractall(path=extracted_data_dir)
tar.close()


for d in dirs_to_extract:
dir_ = os.path.join(coswara_data_dir, d)
part_files = [os.path.join(dir_, file) for file in os.listdir(dir_) if 'tar.gz' in file]
extract(part_files)
os.remove(os.path.join(extracted_data_dir, "temp.tar.gz"))

print("Extraction process complete!")