docstr: The bases essence of this test is to check if any one image format in any one
  dimension can be saved and read, as this is the only base requirement for using
  medpy.
  
  Additionally checks the basic expected behaviour of the load and save
  functionality.
  
  Since this usually does not make much sense, this implementation allows also to
  set a switch (verboose) which causes the test to print a comprehensive overview
  over which image formats with how many dimensions and which pixel data types
  can be read and written.
docstr_tok: ["The","bases","essence","of","this","test","is","to","check","if","any","one","image","format","in","any","one","dimension","can","be","saved","and","read",",","as","this","is","the","only","base","requirement","for","using","medpy",".","Additionally","checks","the","basic","expected","behaviour","of","the","load","and","save","functionality",".","Since","this","usually","does","not","make","much","sense",",","this","implementation","allows","also","to","set","a","switch","(","verboose",")","which","causes","the","test","to","print","a","comprehensive","overview","over","which","image","formats","with","how","many","dimensions","and","which","pixel","data","types","can","be","read","and","written","."]
code: def test_SaveLoad(self):
      """
      The bases essence of this test is to check if any one image format in any one
      dimension can be saved and read, as this is the only base requirement for using
      medpy.
  
      Additionally checks the basic expected behaviour of the load and save
      functionality.
  
      Since this usually does not make much sense, this implementation allows also to
      set a switch (verboose) which causes the test to print a comprehensive overview
      over which image formats with how many dimensions and which pixel data types
      can be read and written.
      """
      ####
      # VERBOOSE SETTINGS
      # The following are three variables that can be used to print some nicely
      # formatted additional output. When one of them is set to True, this unittest
      # should be run stand-alone.
      ####
      # Print a list of supported image types, dimensions and pixel data types
      supported = True
      # Print a list of image types that were tested but are not supported
      notsupported = False
      # Print a list of image type, dimensions and pixel data types configurations,
      # that seem to work but failed the consistency tests. These should be handled
      # with special care, as they might be the source of errors.
      inconsistent = False
  
      ####
      # OTHER SETTINGS
      ####
      # debug settings
      logger = Logger.getInstance()
      # logger.setLevel(logging.DEBUG)
  
      # run test either for most important formats or for all
      # __suffixes = self.__important # (choice 1)
      __suffixes = self.__important + self.__itk  # (choice 2)
  
      # dimensions and dtypes to check
      __suffixes = list(set(__suffixes))
      __ndims = [1, 2, 3, 4, 5]
      __dtypes = [
          scipy.bool_,
          scipy.int8,
          scipy.int16,
          scipy.int32,
          scipy.int64,
          scipy.uint8,
          scipy.uint16,
          scipy.uint32,
          scipy.uint64,
          scipy.float32,
          scipy.float64,
          scipy.complex64,
          scipy.complex128,
      ]
  
      # prepare struct to save settings that passed the test
      valid_types = dict.fromkeys(__suffixes)
      for k1 in valid_types:
          valid_types[k1] = dict.fromkeys(__ndims)
          for k2 in valid_types[k1]:
              valid_types[k1][k2] = []
  
      # prepare struct to save settings that did not
      unsupported_type = dict.fromkeys(__suffixes)
      for k1 in unsupported_type:
          unsupported_type[k1] = dict.fromkeys(__ndims)
          for k2 in unsupported_type[k1]:
              unsupported_type[k1][k2] = dict.fromkeys(__dtypes)
  
      # prepare struct to save settings that did not pass the data integrity test
      invalid_types = dict.fromkeys(__suffixes)
      for k1 in invalid_types:
          invalid_types[k1] = dict.fromkeys(__ndims)
          for k2 in invalid_types[k1]:
              invalid_types[k1][k2] = dict.fromkeys(__dtypes)
  
      # create artifical images, save them, load them again and compare them
      path = tempfile.mkdtemp()
      try:
          for ndim in __ndims:
              logger.debug("Testing for dimension {}...".format(ndim))
              arr_base = scipy.random.randint(
                  0, 10, list(range(10, ndim + 10))
              )
              for dtype in __dtypes:
                  arr_save = arr_base.astype(dtype)
                  for suffix in __suffixes:
                      # do not run test, if in avoid array
                      if (
                          ndim in self.__avoid
                          and suffix in self.__avoid[ndim]
                      ):
                          unsupported_type[suffix][ndim][
                              dtype
                          ] = "Test skipped, as combination in the tests __avoid array."
                          continue
  
                      image = "{}/img{}".format(path, suffix)
                      try:
                          # attempt to save the image
                          save(arr_save, image)
                          self.assertTrue(
                              os.path.exists(image),
                              "Image of type {} with shape={}/dtype={} has been saved without exception, but the file does not exist.".format(
                                  suffix, arr_save.shape, dtype
                              ),
                          )
  
                          # attempt to load the image
                          arr_load, header = load(image)
                          self.assertTrue(
                              header,
                              "Image of type {} with shape={}/dtype={} has been loaded without exception, but no header has been supplied (got: {})".format(
                                  suffix, arr_save.shape, dtype, header
                              ),
                          )
  
                          # check for data consistency
                          msg = self.__diff(arr_save, arr_load)
                          if msg:
                              invalid_types[suffix][ndim][dtype] = msg
                          # elif list == type(valid_types[suffix][ndim]):
                          else:
                              valid_types[suffix][ndim].append(dtype)
  
                          # remove image
                          if os.path.exists(image):
                              os.remove(image)
                      except Exception as e:  # clean up
                          try:
                              unsupported_type[suffix][ndim][
                                  dtype
                              ] = str(e.args)
                          except Exception as _:
                              unsupported_type[suffix][ndim][
                                  dtype
                              ] = e.message
                          if os.path.exists(image):
                              os.remove(image)
      except Exception:
          if not os.listdir(path):
              os.rmdir(path)
          else:
              logger.debug(
                  "Could not delete temporary directory {}. Is not empty.".format(
                      path
                  )
              )
          raise
  
      if supported:
          print(
              "\nsave() and load() support (at least) the following image configurations:"
          )
          print("type\tndim\tdtypes")
          for suffix in valid_types:
              for ndim, dtypes in list(valid_types[suffix].items()):
                  if list == type(dtypes) and not 0 == len(dtypes):
                      print(
                          (
                              "{}\t{}D\t{}".format(
                                  suffix,
                                  ndim,
                                  [
                                      str(x).split(".")[-1][:-2]
                                      for x in dtypes
                                  ],
                              )
                          )
                      )
      if notsupported:
          print("\nthe following configurations are not supported:")
          print("type\tndim\tdtype\t\terror")
          for suffix in unsupported_type:
              for ndim in unsupported_type[suffix]:
                  for dtype, msg in list(
                      unsupported_type[suffix][ndim].items()
                  ):
                      if msg:
                          print(
                              (
                                  "{}\t{}D\t{}\t\t{}".format(
                                      suffix,
                                      ndim,
                                      str(dtype).split(".")[-1][:-2],
                                      msg,
                                  )
                              )
                          )
  
      if inconsistent:
          print(
              "\nthe following configurations show inconsistent saving and loading behaviour:"
          )
          print("type\tndim\tdtype\t\terror")
          for suffix in invalid_types:
              for ndim in invalid_types[suffix]:
                  for dtype, msg in list(
                      invalid_types[suffix][ndim].items()
                  ):
                      if msg:
                          print(
                              (
                                  "{}\t{}D\t{}\t\t{}".format(
                                      suffix,
                                      ndim,
                                      str(dtype).split(".")[-1][:-2],
                                      msg,
                                  )
                              )
                          )
  
file_path: medpy/tests/io_/loadsave.py
imports: import unittest
  import tempfile
  import os
  import scipy
  from medpy.io import load, save
  from medpy.core.logger import Logger
label_desc: Use image node_id 1 for calling the TestIOFacilities obj's underlying member method code with example usage: obj.test_SaveLoad() without return types