code: # Copyright 2020 - 2021 MONAI Consortium
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #     http://www.apache.org/licenses/LICENSE-2.0
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  import csv
  import os
  import tempfile
  import unittest
  
  import torch
  
  from monai.handlers.utils import write_metrics_reports
  
  
  class TestWriteMetricsReports(unittest.TestCase):
      def test_content(self):
          with tempfile.TemporaryDirectory() as tempdir:
              write_metrics_reports(
                  save_dir=tempdir,
                  images=["filepath1", "filepath2"],
                  metrics={"metric1": 1, "metric2": 2},
                  metric_details={"metric3": torch.tensor([[1, 2], [2, 3]]), "metric4": torch.tensor([[5, 6], [7, 8]])},
                  summary_ops=["mean", "median", "max", "90percentile"],
                  deli="\t",
                  output_type="csv",
              )
  
              # check the metrics.csv and content
              self.assertTrue(os.path.exists(os.path.join(tempdir, "metrics.csv")))
              with open(os.path.join(tempdir, "metrics.csv")) as f:
                  f_csv = csv.reader(f)
                  for i, row in enumerate(f_csv):
                      self.assertEqual(row, [f"metric{i + 1}\t{i + 1}"])
              self.assertTrue(os.path.exists(os.path.join(tempdir, "metric3_raw.csv")))
              # check the metric_raw.csv and content
              with open(os.path.join(tempdir, "metric3_raw.csv")) as f:
                  f_csv = csv.reader(f)
                  for i, row in enumerate(f_csv):
                      if i > 0:
                          self.assertEqual(row, [f"filepath{i}\t{float(i)}\t{float(i + 1)}\t{i + 0.5}"])
              self.assertTrue(os.path.exists(os.path.join(tempdir, "metric3_summary.csv")))
              # check the metric_summary.csv and content
              with open(os.path.join(tempdir, "metric3_summary.csv")) as f:
                  f_csv = csv.reader(f)
                  for i, row in enumerate(f_csv):
                      if i == 1:
                          self.assertEqual(row, ["class0\t1.5000\t1.5000\t2.0000\t1.9000"])
                      elif i == 2:
                          self.assertEqual(row, ["class1\t2.5000\t2.5000\t3.0000\t2.9000"])
                      elif i == 3:
                          self.assertEqual(row, ["mean\t2.0000\t2.0000\t2.5000\t2.4000"])
              self.assertTrue(os.path.exists(os.path.join(tempdir, "metric4_raw.csv")))
              self.assertTrue(os.path.exists(os.path.join(tempdir, "metric4_summary.csv")))
  
  
  if __name__ == "__main__":
      unittest.main()
   # Copyright 2020 - 2021 MONAI Consortium
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #     http://www.apache.org/licenses/LICENSE-2.0
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  import unittest
  
  import numpy as np
  from parameterized import parameterized
  
  from monai.transforms import RandScaleCropd
  from tests.utils import TEST_NDARRAYS, assert_allclose
  
  TEST_CASE_1 = [
      {"keys": "img", "roi_scale": [1.0, 1.0, -1.0], "random_center": True},
      {"img": np.random.randint(0, 2, size=[3, 3, 3, 4])},
      (3, 3, 3, 4),
  ]
  
  TEST_CASE_2 = [
      {"keys": "img", "roi_scale": [1.0, 1.0, 1.0], "random_center": False},
      {"img": np.random.randint(0, 2, size=[3, 3, 3, 3])},
      (3, 3, 3, 3),
  ]
  
  TEST_CASE_3 = [
      {"keys": "img", "roi_scale": [0.6, 0.6], "random_center": False},
      {"img": np.array([[[0, 0, 0, 0, 0], [0, 1, 2, 1, 0], [0, 2, 3, 2, 0], [0, 1, 2, 1, 0], [0, 0, 0, 0, 0]]])},
  ]
  
  TEST_CASE_4 = [
      {
          "keys": "img",
          "roi_scale": [0.75, 0.6, 0.5],
          "max_roi_scale": [1.0, -1.0, 0.6],
          "random_center": True,
          "random_size": True,
      },
      {"img": np.random.randint(0, 2, size=[1, 4, 5, 6])},
      (1, 3, 4, 3),
  ]
  
  TEST_CASE_5 = [
      {"keys": "img", "roi_scale": 0.6, "max_roi_scale": 0.8, "random_center": True, "random_size": True},
      {"img": np.random.randint(0, 2, size=[1, 4, 5, 6])},
      (1, 3, 4, 4),
  ]
  
  TEST_CASE_6 = [
      {"keys": "img", "roi_scale": 0.2, "max_roi_scale": 0.8, "random_center": True, "random_size": True},
      {"img": np.random.randint(0, 2, size=[1, 4, 5, 6])},
      (1, 3, 2, 4),
  ]
  
  
  class TestRandScaleCropd(unittest.TestCase):
      @parameterized.expand([TEST_CASE_1, TEST_CASE_2])
      def test_shape(self, input_param, input_data, expected_shape):
          result = RandScaleCropd(**input_param)(input_data)
          self.assertTupleEqual(result["img"].shape, expected_shape)
  
      @parameterized.expand([TEST_CASE_3])
      def test_value(self, input_param, input_data):
          for p in TEST_NDARRAYS:
              cropper = RandScaleCropd(**input_param)
              input_data["img"] = p(input_data["img"])
              result = cropper(input_data)
              roi = [(2 - i // 2, 2 + i - i // 2) for i in cropper._size]
              assert_allclose(
                  result["img"], input_data["img"][:, roi[0][0] : roi[0][1], roi[1][0] : roi[1][1]], type_test=False
              )
  
      @parameterized.expand([TEST_CASE_4, TEST_CASE_5, TEST_CASE_6])
      def test_random_shape(self, input_param, input_data, expected_shape):
          cropper = RandScaleCropd(**input_param)
          cropper.set_random_state(seed=123)
          result = cropper(input_data)
          self.assertTupleEqual(result["img"].shape, expected_shape)
  
  
  if __name__ == "__main__":
      unittest.main()
  
apis: torch.tensor
apis: numpy.array numpy.random.randint