Commit b67fb466 authored by Bharath Ramsundar's avatar Bharath Ramsundar
Browse files

Fixes

parent be2dae47
Loading
Loading
Loading
Loading
+49 −9
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ from deepchem.data import DiskDataset
from deepchem.data import NumpyDataset
from scipy import misc
import zipfile
from PIL import Image


def convert_df_to_numpy(df, tasks, verbose=False):
@@ -335,6 +336,11 @@ class FASTALoader(DataLoader):
class ImageLoader(DataLoader):
  """
  Handles loading of image files.

  This class allows for loading of images in various formats. For user
  convenience, also accepts zip-files and directories of images and uses some
  limited intelligence to attempt to traverse subdirectories which contain
  images.
  """

  def __init__(self, tasks=None):
@@ -343,38 +349,72 @@ class ImageLoader(DataLoader):
      tasks = []
    self.tasks = tasks

  def featurize(self, input_files):
  def featurize(self, input_files, in_memory=True):
    """Featurizes image files.

    Parameters
    ----------
    input_files: list
      Each file in this list should either be of a supported image format (.png
      only for now) or of a compressed folder of image files (only .zip for now).
      Each file in this list should either be of a supported image format
      (.png, .tif only for now) or of a compressed folder of image files
      (only .zip for now).
    in_memory: bool
      If true, return in-memory NumpyDataset. Else return DiskDataset.
    """
    if not isinstance(input_files, list):
      input_files = [input_files]

    images = []
    image_files = []
    # Sometimes zip files contain directories within. Traverse directories
    while len(input_files) > 0:
      print("ITERATION!!")
      remainder = []
      for input_file in input_files:
        filename, extension = os.path.splitext(input_file)
        # TODO(rbharath): Add support for more extensions
      if extension == ".zip":
        if os.path.isdir(input_file):
          print("DIRECTORY!")
          dirfiles = [os.path.join(input_file, subfile) for subfile in os.listdir(input_file)]
          remainder += dirfiles
        elif extension == ".zip":
          zip_dir = tempfile.mkdtemp()
          zip_ref = zipfile.ZipFile(input_file, 'r')
          zip_ref.extractall(path=zip_dir)
          zip_ref.close()
        image_files += [
            os.path.join(zip_dir, name) for name in zip_ref.namelist()
        ]
      elif extension == ".png":
          zip_files = [os.path.join(zip_dir, name) for name in zip_ref.namelist()]
          for zip_file in zip_files:
            if os.path.isdir(zip_file):
              remainder.append(zip_file)
            else:
              image_files.append(zip_file)
        elif extension in [".png", ".tif"]:
          image_files.append(input_file)
        else:
          raise ValueError("Unsupported file format")
      input_files = remainder
      print("remainder")
      print(remainder)

    images = []
    print("image_files")
    print(image_files)
    print("len(image_files)")
    print(len(image_files))
    for image_file in image_files:
      _, extension = os.path.splitext(image_file) 
      if extension == ".png":
        image = misc.imread(image_file)
        images.append(image)
      elif extension == ".tif":
        im = Image.open(image_file)
        imarray = np.array(im)
        images.append(imarray)
      else:
        raise ValueError("Unsupported image filetype for %s" % image_file)
    images = np.array(images)
    if in_memory:
      return NumpyDataset(images)
    else:
      # from_numpy currently requires labels. Make dummy labels
      labels = np.zeros(len(images))
      return DiskDataset.from_numpy(images, labels)
+27 −4
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ class TestImageLoader(unittest.TestCase):

  def setUp(self):
    super(TestImageLoader, self).setUp()
    self.current_dir = os.path.dirname(os.path.abspath(__file__))
    self.tif_image_path = os.path.join(self.current_dir, "a_image.tif")

    # Create image file
    self.data_dir = tempfile.mkdtemp()
@@ -42,23 +44,44 @@ class TestImageLoader(unittest.TestCase):
    zipf.write(self.face_copy_path)
    zipf.close()

  def test_simple_load(self):
    # Create zip of multiple image files, multiple_types
    self.multitype_zip_path = os.path.join(self.data_dir, "multitype_face.zip")
    zipf = zipfile.ZipFile(self.multitype_zip_path, "w", zipfile.ZIP_DEFLATED)
    zipf.write(self.face_path)
    zipf.write(self.tif_image_path)
    zipf.close()

  def test_png_simple_load(self):
    loader = dc.data.ImageLoader()
    dataset = loader.featurize(self.face_path)
    # These are the known dimensions of face.png
    assert dataset.X.shape == (1, 768, 1024, 3)

  def test_multi_load(self):
  def test_tif_simple_load(self):
    loader = dc.data.ImageLoader()
    dataset = loader.featurize(self.tif_image_path)
    # TODO(rbharath): Where are the color channels?
    assert dataset.X.shape == (1, 44, 330)

  def test_png_multi_load(self):
    loader = dc.data.ImageLoader()
    dataset = loader.featurize([self.face_path, self.face_copy_path])
    assert dataset.X.shape == (2, 768, 1024, 3)

  def test_zip_load(self):
  def test_png_zip_load(self):
    loader = dc.data.ImageLoader()
    dataset = loader.featurize(self.zip_path)
    assert dataset.X.shape == (1, 768, 1024, 3)

  def test_multi_zip_load(self):
  def test_png_multi_zip_load(self):
    loader = dc.data.ImageLoader()
    dataset = loader.featurize(self.multi_zip_path)
    print("dataset.X.shape")
    print(dataset.X.shape)
    assert dataset.X.shape == (2, 768, 1024, 3)

  def test_multitype_zip_load(self):
    loader = dc.data.ImageLoader()
    dataset = loader.featurize(self.multitype_zip_path)
    # Since the different files have different shapes, makes an object array
    assert dataset.X.shape == (2,)
+1 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ from __future__ import division
from __future__ import unicode_literals

from deepchem.molnet.load_function.bace_datasets import load_bace_classification, load_bace_regression
from deepchem.molnet.load_function.bbbc_datasets import load_bbbc001
from deepchem.molnet.load_function.bbbp_datasets import load_bbbp
from deepchem.molnet.load_function.cell_counting_datasets import load_cell_counting
from deepchem.molnet.load_function.chembl_datasets import load_chembl
+1 −0
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ def load_cell_counting(split=None, reload=True):
  }
  if split not in splitters:
    raise ValueError("Only index and random splits supported.")
  splitter = splitters[split]

  train, valid, test = splitter.train_valid_test_split(dataset)
  all_dataset = (train, valid, test)