docs: def process_stats(mv_districts,
                    n_of_districts,
                    source,
                    mode,
                    critical,
                    filename,
                    output):
      '''Generates stats dataframes for districts in mv_districts.
  
      If source=='ding0', then runned districts are saved to a pickle named
      filename+str(n_of_districts[0])+'_to_'+str(n_of_districts[-1])+'.pkl'
  
      Parameters
      ----------
      districts_list: list of int
          List with all districts to be run.
      n_of_districts: int
          Number of districts to be run in each cluster
      source: str
          If 'pkl', pickle files are read.
          If 'ding0', ding0 is run over the districts.
      mode: str
          If 'MV', medium voltage stats are calculated.
          If 'LV', low voltage stats are calculated.
          If empty, medium and low voltage stats are calculated.
      critical: bool
          If True, critical nodes and branches are returned
      filename: str
          filename prefix for saving pickles
      output:
          outer variable where the output is stored as a tuple of 6 lists::
  
          * mv_stats: MV stats DataFrames.
            If mode=='LV', then DataFrame is empty.
  
          * lv_stats: LV stats DataFrames.
            If mode=='MV', then DataFrame is empty.
  
          * mv_crit_nodes: MV critical nodes stats DataFrames.
            If mode=='LV', then DataFrame is empty.
            If critical==False, then DataFrame is empty.
  
          * mv_crit_edges: MV critical edges stats DataFrames.
            If mode=='LV', then DataFrame is empty.
            If critical==False, then DataFrame is empty.
  
          * lv_crit_nodes: LV critical nodes stats DataFrames.
            If mode=='MV', then DataFrame is empty.
            If critical==False, then DataFrame is empty.
  
          * lv_crit_edges: LV critical edges stats DataFrames.
            If mode=='MV', then DataFrame is empty.
            If critical==False, then DataFrame is empty.
      '''
      #######################################################################
      # decide what exactly to do with MV LV
      if mode == 'MV':
          calc_mv = True
          calc_lv = False
      elif mode == 'LV':
          calc_mv = False
          calc_lv = True
      else:
          calc_mv = True
          calc_lv = True
      #######################################################################
      clusters = [mv_districts[x:x + n_of_districts] for x in range(0, len(mv_districts), n_of_districts)]
  
      mv_stats      = []
      lv_stats      = []
      mv_crit_nodes = []
      mv_crit_edges = []
      lv_crit_nodes = []
      lv_crit_edges = []
      #######################################################################
      for cl in clusters:
          nw_name = filename + str(cl[0])
          if not cl[0] == cl[-1]:
              nw_name = nw_name + '_to_' + str(cl[-1])
  
          nw = NetworkDing0(name=nw_name)
          if source == 'pkl':
              print('\n########################################')
              print('  Reading data from pickle district', cl)
              print('########################################')
              try:
                  nw = load_nd_from_pickle(nw_name + '.pkl')
              except Exception:
                  continue
          else:
              # database connection/ session
              engine = db.connection(section='oedb')
              session = sessionmaker(bind=engine)()
  
              print('\n########################################')
              print('  Running ding0 for district', cl)
              print('########################################')
              try:
                  nw.run_ding0(session=session, mv_grid_districts_no=cl)
                  try:
                      save_nd_to_pickle(nw, filename=nw_name + '.pkl')
                  except Exception:
                      continue
              except Exception:
                  continue
  
              # Close database connection
          if calc_mv:
              stats = calculate_mvgd_stats(nw)
              mv_stats.append(stats)
          if calc_lv:
              stats = calculate_lvgd_stats(nw)
              lv_stats.append(stats)
          if critical and calc_mv:
              stats = calculate_mvgd_voltage_current_stats(nw)
              mv_crit_nodes.append(stats[0])
              mv_crit_edges.append(stats[1])
          if critical and calc_lv:
              stats = calculate_lvgd_voltage_current_stats(nw)
              lv_crit_nodes.append(stats[0])
              lv_crit_edges.append(stats[1])
      #######################################################################
      salida = (mv_stats, lv_stats, mv_crit_nodes, mv_crit_edges, lv_crit_nodes, lv_crit_edges)
      output.put(salida)