def _filter_sample_ids_from_category_state_coverage(metadata_map, sample_ids, coverage_category, subject_category, consider_state_fn, min_num_states=None, required_states=None):
 subjects = defaultdict(list)
 for samp_id in sample_ids:
  subject = metadata_map.getCategoryValue(samp_id, subject_category)
  subjects[subject].append(samp_id)
 samp_ids_to_keep = []
 num_subjects_kept = 0
 states_kept = []
 for (subject, samp_ids) in subjects.items():
  subject_covered_states = set(metadata_map.getCategoryValues(samp_ids, coverage_category))
  keep_subject = True
  if (min_num_states is not None):
   if (sum([consider_state_fn(s) for s in subject_covered_states]) < min_num_states):
    keep_subject = False
  if (keep_subject and (required_states is not None)):
   if (len((subject_covered_states & required_states)) != len(required_states)):
    keep_subject = False
  if keep_subject:
   samp_ids_to_keep.extend(samp_ids)
   states_kept.extend(subject_covered_states)
   num_subjects_kept += 1
 return (set(samp_ids_to_keep), num_subjects_kept, set(states_kept))

def _filter_sample_ids_from_category_state_coverage ( metadata_map , sample_ids , coverage_category , subject_category , consider_state_fn , min_num_states = None , required_states = None ) : subjects = defaultdict ( list ) for samp_id in sample_ids : subject = metadata_map . getCategoryValue ( samp_id , subject_category ) subjects [ subject ] . append ( samp_id ) samp_ids_to_keep = [ ] num_subjects_kept = 0 states_kept = [ ] for ( subject , samp_ids ) in subjects . items ( ) : subject_covered_states = set ( metadata_map . getCategoryValues ( samp_ids , coverage_category ) ) keep_subject = True if ( min_num_states is not None ) : if ( sum ( [ consider_state_fn ( s ) for s in subject_covered_states ] ) < min_num_states ) : keep_subject = False if ( keep_subject and ( required_states is not None ) ) : if ( len ( ( subject_covered_states & required_states ) ) != len ( required_states ) ) : keep_subject = False if keep_subject : samp_ids_to_keep . extend ( samp_ids ) states_kept . extend ( subject_covered_states ) num_subjects_kept += 1 return ( set ( samp_ids_to_keep ) , num_subjects_kept , set ( states_kept ) )
helper function to perform filtering based on category state coverage .