def _group_by_sample_metadata(collapsed_md, sample_id_field='SampleID'):
 new_index_to_group = {}
 old_index_to_new_index = {}
 for i in collapsed_md.index:
  old_indices = collapsed_md[sample_id_field][i]
  if isinstance(i, tuple):
   new_index = i
  else:
   new_index = (i,)
  new_index_to_group[new_index] = set(old_indices)
  for old_index in old_indices:
   old_index_to_new_index[old_index] = new_index
 return (new_index_to_group, old_index_to_new_index)

def _group_by_sample_metadata ( collapsed_md , sample_id_field = 'SampleID' ) : new_index_to_group = { } old_index_to_new_index = { } for i in collapsed_md . index : old_indices = collapsed_md [ sample_id_field ] [ i ] if isinstance ( i , tuple ) : new_index = i else : new_index = ( i , ) new_index_to_group [ new_index ] = set ( old_indices ) for old_index in old_indices : old_index_to_new_index [ old_index ] = new_index return ( new_index_to_group , old_index_to_new_index )
group sample identifiers by one or more metadata fields parameters collapsed_md : pd .