code: def _format_instances(instances, namespace=None):
      """Format a list of instances to a list of named estimator tuples."""
      nested = isinstance(instances, dict)
      if nested:
          # Need to flatten, but record hierarchy
          instances_dict = instances
          vacuous = list()
          case_map = dict()
          instances = list()
          for case, instance_list in sorted(instances_dict.items()):
              case = "-".join(case.lower().split())
  
              if not instance_list:
                  vacuous.append((case, instance_list))
                  continue
  
              for val in instance_list:
                  instances.append(val)
                  if isinstance(val, (list, tuple, set)):
                      val = val[1]  # Grab estimator
                  case_map[val] = case
  
      named_instances = list()
      for val in instances:
          # Check that the instance appears correctly specified
          if not isinstance(val, (list, tuple, set)):
              # val is the instance
              instance = val
          else:
              # val is a list-like object. Assume instance is the last entry
              instance = val[-1]
  
          # Check if it appears to be an estimator
          assert_valid_estimator(instance)
  
          try:
              # Format instance names
  
              # We keep the instance as a list to change possible duplicate names
              # exploiting that lists are mutable, before switching to tuple
              if instance is val:
                  tup = [instance.__class__.__name__.lower(), instance]
              else:
                  tup = ["-".join(val[0].split()).lower(), val[-1]]
  
              named_instances.append(tup)
  
          except Exception as e:
              msg = (
                  "Could not format instance %s. Check that passed instance "
                  "iterables follow correct syntax:\n"
                  "- if multiple preprocessing cases, pass a dictionary with "
                  "instance lists as values and case name as key.\n"
                  "- else, pass list of (named) instances.\n"
                  "See documentation for further information.\n"
                  "Error details: %r"
              )
              raise LayerSpecificationError(msg % (instance, e))
  
      # Check and correct duplicate names
      names = [tup[0] for tup in named_instances]
      if namespace:
          names.extend(namespace)
      duplicates = Counter(names)
      duplicates = {
          key: val for key, val in duplicates.items() if val > 1
      }
  
      out = list()  # final named_instances list
      name_count = {key: 1 for key in duplicates}
      for name, instance in named_instances:
          if name in duplicates:
              current_name_count = name_count[name]  # fix before update
              name_count[name] += 1
              name += "-%d" % current_name_count  # rename
          out.append((name, instance))
      out = sorted(out)
  
      if nested:
          # Rebuild hierarchy
          nested_out = dict()
          for name, instance in out:
              case = case_map[instance]
              if case not in nested_out:
                  nested_out[case] = list()
              nested_out[case].append((name, instance))
  
          for k, v in vacuous:
              nested_out[k] = v
  
          out = nested_out
  
      return out
  
file_path: mlens/mlens/utils/formatting.py
imports: from __future__ import division, print_function
  from collections import Counter
  from .checks import assert_valid_estimator, assert_correct_format
  from .exceptions import LayerSpecificationError
  from ..externals.sklearn.base import clone
label_desc: Use image node_id 2 for calling a global function with example usage: _format_instances(instances, namespace) and returns: out