Skip to content

Commit

Permalink
Edited test
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrockmenezes committed Dec 17, 2023
1 parent 3d21a0b commit 4073b1b
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions deepchem/data/tests/test_image_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ def setUp(self):
self.face_copy_path = os.path.join(self.data_dir, "face_copy.png")
Image.fromarray(self.face).save(self.face_copy_path)

# Create directory of multiple image files
self.order_path = os.path.join(self.data_dir, "order_check")
os.mkdir(self.order_path)
self.face_path = os.path.join(self.order_path, "face.png")
Image.fromarray(self.face).save(self.face_path)
self.face_copy_path = os.path.join(self.order_path, "face_copy.png")
Image.fromarray(self.face).save(self.face_copy_path)

# Zip directory of multiple image files
self.order_zip_path = os.path.join(self.data_dir, "order_check.zip")
with zipfile.ZipFile(self.order_zip_path, 'w') as zip_file:
for foldername, subfolders, filenames in os.walk(self.order_path):
for filename in filenames:
file_path = os.path.join(foldername, filename)
arcname = os.path.relpath(file_path, self.order_path)
zip_file.write(file_path, arcname=arcname)

# Create zip of image file
self.zip_path = os.path.join(self.data_dir, "face.zip")
zipf = zipfile.ZipFile(self.zip_path, "w", zipfile.ZIP_DEFLATED)
Expand Down Expand Up @@ -127,33 +144,11 @@ def test_directory_load(self):

def test_zip_order(self):
# Test that the order of the contents of an unzipped file is preserved.
with tempfile.TemporaryDirectory() as sample_temp_dir:
file_contents = ['file1.txt', 'file2.txt', 'file3.txt']
for file_name in file_contents:
with open(os.path.join(sample_temp_dir, file_name),
'w') as file:
file.write(f"This is {file_name}.")

# Zip the sample directory
zip_file_path = 'sample_directory.zip'
with zipfile.ZipFile(zip_file_path, 'w') as zip_file:
for foldername, subfolders, filenames in os.walk(
sample_temp_dir):
for filename in filenames:
file_path = os.path.join(foldername, filename)
arcname = os.path.relpath(file_path, sample_temp_dir)
zip_file.write(file_path, arcname=arcname)

# Unzip the contents to a temporary directory
with tempfile.TemporaryDirectory() as temp_dir:
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
zip_ref.extractall(temp_dir)

# Check if the contents' order is the same
original_contents = os.listdir(sample_temp_dir)
unzipped_contents = os.listdir(temp_dir)

assert original_contents == unzipped_contents

# No need to manually remove temporary files; they will be cleaned up automatically
os.remove(zip_file_path)
# Load the zip file
loader = dc.data.ImageLoader()
dataset_dir = loader.create_dataset(self.order_path)
# Load multi_path directly
loader = dc.data.ImageLoader()
dataset_zipped = loader.create_dataset(self.order_zip_path)
# Check that the order of the files is the same
assert np.all(dataset_dir.X == dataset_zipped.X)

0 comments on commit 4073b1b

Please sign in to comment.