Repository: Project-MONAI/MONAI

Issue:
notebook tests error: ./2d_registration/registration_mednist.ipynb
**Describe the bug**

```
Running ./2d_registration/registration_mednist.ipynb
Checking PEP8 compliance...
Running notebook...
Before:
    "max_epochs = 200\n",
After:
    "max_epochs = 1\n",

Executing:   0%|          | 0/20 [00:00<?, ?cell/s]
Executing:   5%|▌         | 1/20 [00:01<00:24,  1.27s/cell]
Executing:  20%|██        | 4/20 [00:04<00:18,  1.14s/cell]
Executing:  30%|███       | 6/20 [00:06<00:15,  1.14s/cell]
Executing:  40%|████      | 8/20 [00:16<00:30,  2.54s/cell]
Executing:  55%|█████▌    | 11/20 [00:16<00:12,  1.43s/cell]
Executing:  55%|█████▌    | 11/20 [00:17<00:14,  1.57s/cell]
Traceback (most recent call last):
  File "/opt/conda/bin/papermill", line 8, in <module>
    sys.exit(papermill())
  File "/opt/conda/lib/python3.8/site-packages/click/core.py", line 1137, in __call__
    return self.main(*args, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/click/core.py", line 1062, in main
    rv = self.invoke(ctx)
  File "/opt/conda/lib/python3.8/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/conda/lib/python3.8/site-packages/click/core.py", line 763, in invoke
    return __callback(*args, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/papermill/cli.py", line 250, in papermill
    execute_notebook(
  File "/opt/conda/lib/python3.8/site-packages/papermill/execute.py", line 122, in execute_notebook
    raise_for_execution_errors(nb, output_path)
  File "/opt/conda/lib/python3.8/site-packages/papermill/execute.py", line 234, in raise_for_execution_errors
    raise error
papermill.exceptions.PapermillExecutionError: 
---------------------------------------------------------------------------
Exception encountered at "In [6]":
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_2509/823736021.py in <module>
      1 check_ds = Dataset(data=training_datadict, transform=train_transforms)
----> 2 check_loader = DataLoader(check_ds, batch_size=1, shuffle=True)
      3 check_data = first(check_loader)
      4 fixed_image = check_data["fixed_hand"][0][0]
      5 moving_image = check_data["moving_hand"][0][0]

/home/jenkins/agent/workspace/Monai-notebooks/MONAI/monai/data/dataloader.py in __init__(self, dataset, num_workers, **kwargs)
     82             kwargs.update({"worker_init_fn": worker_init_fn})
     83 
---> 84         super().__init__(dataset=dataset, num_workers=num_workers, **kwargs)  # type: ignore[call-overload]

/opt/conda/lib/python3.8/site-packages/torch/utils/data/dataloader.py in __init__(self, dataset, batch_size, shuffle, sampler, batch_sampler, num_workers, collate_fn, pin_memory, drop_last, timeout, worker_init_fn, multiprocessing_context, generator, prefetch_factor, persistent_workers)
    266             else:  # map-style
    267                 if shuffle:
--> 268                     sampler = RandomSampler(dataset, generator=generator)
    269                 else:
    270                     sampler = SequentialSampler(dataset)

/opt/conda/lib/python3.8/site-packages/torch/utils/data/sampler.py in __init__(self, data_source, replacement, num_samples, generator)
    100 
    101         if not isinstance(self.num_samples, int) or self.num_samples <= 0:
--> 102             raise ValueError("num_samples should be a positive integer "
    103                              "value, but got num_samples={}".format(self.num_samples))
    104 

ValueError: num_samples should be a positive integer value, but got num_samples=0
```

introduced since https://github.com/Project-MONAI/MONAI/pull/3332


Discussion:

Resolving patch:
diff --git a/monai/apps/datasets.py b/monai/apps/datasets.py
--- a/monai/apps/datasets.py
+++ b/monai/apps/datasets.py
@@ -114,12 +114,9 @@ def _generate_data_list(self, dataset_dir: PathLike) -> List[Dict]:
 
         """
         dataset_dir = Path(dataset_dir)
-        class_names = sorted(f"{x}" for x in dataset_dir.iterdir() if (dataset_dir / x).is_dir())
+        class_names = sorted(f"{x.name}" for x in dataset_dir.iterdir() if x.is_dir())  # folder name as the class name
         self.num_class = len(class_names)
-        image_files = [
-            [f"{dataset_dir.joinpath(class_names[i], x)}" for x in (dataset_dir / class_names[i]).iterdir()]
-            for i in range(self.num_class)
-        ]
+        image_files = [[f"{x}" for x in (dataset_dir / class_names[i]).iterdir()] for i in range(self.num_class)]
         num_each = [len(image_files[i]) for i in range(self.num_class)]
         image_files_list = []
         image_class = []
@@ -145,7 +142,6 @@ def _generate_data_list(self, dataset_dir: PathLike) -> List[Dict]:
             raise ValueError(
                 f'Unsupported section: {self.section}, available options are ["training", "validation", "test"].'
             )
-
         # the types of label and class name should be compatible with the pytorch dataloader
         return [
             {"image": image_files_list[i], "label": image_class[i], "class_name": class_name[i]}
diff --git a/monai/apps/utils.py b/monai/apps/utils.py
--- a/monai/apps/utils.py
+++ b/monai/apps/utils.py
@@ -241,7 +241,7 @@ def extractall(
         cache_dir = Path(output_dir, _basename(filepath).split(".")[0])
     else:
         cache_dir = Path(output_dir)
-    if cache_dir.exists() and len(list(cache_dir.iterdir())) > 0:
+    if cache_dir.exists() and next(cache_dir.iterdir(), None) is not None:
         logger.info(f"Non-empty folder exists in {cache_dir}, skipped extracting.")
         return
     filepath = Path(filepath)


Test patch:
diff --git a/tests/test_decathlondataset.py b/tests/test_decathlondataset.py
--- a/tests/test_decathlondataset.py
+++ b/tests/test_decathlondataset.py
@@ -60,6 +60,8 @@ def _test_dataset(dataset):
             root_dir=testing_dir, task="Task04_Hippocampus", transform=transform, section="validation", download=False
         )
         _test_dataset(data)
+        self.assertTrue(data[0]["image_meta_dict"]["filename_or_obj"].endswith("hippocampus_163.nii.gz"))
+        self.assertTrue(data[0]["label_meta_dict"]["filename_or_obj"].endswith("hippocampus_163.nii.gz"))
         # test validation without transforms
         data = DecathlonDataset(root_dir=testing_dir, task="Task04_Hippocampus", section="validation", download=False)
         self.assertTupleEqual(data[0]["image"].shape, (36, 47, 44))
@@ -77,7 +79,7 @@ def _test_dataset(dataset):
 
         shutil.rmtree(os.path.join(testing_dir, "Task04_Hippocampus"))
         try:
-            data = DecathlonDataset(
+            DecathlonDataset(
                 root_dir=testing_dir,
                 task="Task04_Hippocampus",
                 transform=transform,
diff --git a/tests/test_mednistdataset.py b/tests/test_mednistdataset.py
--- a/tests/test_mednistdataset.py
+++ b/tests/test_mednistdataset.py
@@ -55,16 +55,18 @@ def _test_dataset(dataset):
 
         # testing from
         data = MedNISTDataset(root_dir=Path(testing_dir), transform=transform, section="test", download=False)
-        data.get_num_classes()
+        self.assertEqual(data.get_num_classes(), 6)
         _test_dataset(data)
         data = MedNISTDataset(root_dir=testing_dir, section="test", download=False)
         self.assertTupleEqual(data[0]["image"].shape, (64, 64))
         # test same dataset length with different random seed
         data = MedNISTDataset(root_dir=testing_dir, transform=transform, section="test", download=False, seed=42)
         _test_dataset(data)
+        self.assertEqual(data[0]["class_name"], "AbdomenCT")
+        self.assertEqual(data[0]["label"].cpu().item(), 0)
         shutil.rmtree(os.path.join(testing_dir, "MedNIST"))
         try:
-            data = MedNISTDataset(root_dir=testing_dir, transform=transform, section="test", download=False)
+            MedNISTDataset(root_dir=testing_dir, transform=transform, section="test", download=False)
         except RuntimeError as e:
             print(str(e))
             self.assertTrue(str(e).startswith("Cannot find dataset directory"))