original_docstring: If merge_small is true then small groups are merged. Otherwise they
          are cut. For level>1, its probably best to cut them, for level==1 you
          should merge them for complete data coverage
docstring: If merge_small is true then small groups are merged. Otherwise they
  are cut. For level>1, its probably best to cut them, for level==1 you
  should merge them for complete data coverage
docstring_tokens: If merge_small is true then small groups are merged . Otherwise they are cut . For level > 1 its probably best to cut them for level == 1 you should merge them for complete data coverage
code: def _create_segments_for_level(self, labels: pd.DataFrame, level: int,
                                    merge_small=False):
          '''If merge_small is true then small groups are merged. Otherwise they
          are cut. For level>1, its probably best to cut them, for level==1 you
          should merge them for complete data coverage'''
          config = self.config
          min_pts = config['min_segment_size']
          seg_features = config['segmentation_features']
  
          features = [x['feature'] for x in seg_features]
          bins = [x['bins'] for x in seg_features]
          bin_types = [x['bin_type'] for x in seg_features]
  
          feat_grps = combinations(features, level)
          good_groups = []
          small_groups = []
          for fgrp in feat_grps:
              bin_grps = product(*[bins[features.index(x)] for x in fgrp])
              for bgrp in bin_grps:
                  tmp = {
                      'features': list(fgrp),
                      'bins': list(bgrp),
                      'bin_types': [bin_types[features.index(x)] for x in fgrp],
                      'level': int(level),
                  }
                  in_grp = labels.apply(
                      lambda x: is_in_segment(x, tmp), axis=1
                  )
                  tmp['n_pts'] = sum(in_grp)
                  if tmp['n_pts'] < min_pts:
                      small_groups.append(tmp)
                  else:
                      good_groups.append(tmp)
  
          # Merge small groups
          if merge_small:
              other_group = self.merge_groups(small_groups)
              good_groups.extend(other_group)
  
          # Name groups
          out_segments = {}
          for i, grp in enumerate(good_groups):
              group_name = f'lvl_{level}-group_{i}'
              out_segments[group_name] = grp
  
          return out_segments
code_tokens: def _create_segments_for_level ( self , labels : pd . DataFrame , level : int , merge_small = False ) : config = self . config min_pts = config [ 'min_segment_size' ] seg_features = config [ 'segmentation_features' ] features = [ x [ 'feature' ] for x in seg_features ] bins = [ x [ 'bins' ] for x in seg_features ] bin_types = [ x [ 'bin_type' ] for x in seg_features ] feat_grps = combinations ( features , level ) good_groups = [ ] small_groups = [ ] for fgrp in feat_grps : bin_grps = product ( * [ bins [ features . index ( x ) ] for x in fgrp ] ) for bgrp in bin_grps : tmp = { 'features' : list ( fgrp ) , 'bins' : list ( bgrp ) , 'bin_types' : [ bin_types [ features . index ( x ) ] for x in fgrp ] , 'level' : int ( level ) , } in_grp = labels . apply ( lambda x : is_in_segment ( x , tmp ) , axis = 1 ) tmp [ 'n_pts' ] = sum ( in_grp ) if tmp [ 'n_pts' ] < min_pts : small_groups . append ( tmp ) else : good_groups . append ( tmp ) if merge_small : other_group = self . merge_groups ( small_groups ) good_groups . extend ( other_group ) out_segments = { } for i , grp in enumerate ( good_groups ) : group_name = f'lvl_{level}-group_{i}' out_segments [ group_name ] = grp return out_segments
short_docstring: If merge_small is true then small groups are merged.
short_docstring_tokens: If merge_small is true then small groups are merged .
comment: '''If merge_small is true then small groups are merged. Otherwise they
          are cut. For level>1, its probably best to cut them, for level==1 you
          should merge them for complete data coverage'''$SEP$# Merge small groups$SEP$# Name groups